Bash script for beginners
June 25, 2012 in Bash script
Bash Scripting for beginners Part 8
While and Sleep
There are times, that you have a folder that the contents of that folder are changing really fast and you don’t really have time to execute the backup every let’s say 30 seconds. Or your boss comes over your head and said “You know, I want backup of a folder every 5 seconds for the next 8 hours”. You will say ok, but what if you have to do something else in the mean time? Think Bash! That is just a story that made up and the persons of that story is completely out of my mind and that never happened to me and my boss never yell over my head! (Ok Boss? Do I still have my job?). I can promise you that after that you will have plenty of time! To do so let’s create a new script and name it backup7.sh
gedit ~/Desktop/bash/code/backup7.sh
write the following to the gedit :
#!/bin/bash # Bash with allaboutlinux.eu DATETIME="$(date +%d.%m.%Y_%H:%M:%S)" BKFILE=~/Desktop/bash/backup/backup$DATETIME.tar.gz BKFOLDER=~/Desktop/bash/code/ BKRESULT=~/Desktop/bash/backup/backup$DATETIME.txt BKDESTINATION=~/Desktop/bash/backup/ LIST="Backup Quit" LIST2="Date-Time User-Difine" LIST3="Run-Once Run-multiple" select OPT in $LIST3; do if [ $OPT = "Run-Once" ]; then select OPT in $LIST; do if [ $OPT = "Backup" ]; then echo please chose the filename of the backup: select OPT in $LIST2; do if [ $OPT = "Date-Time" ]; then tar -zcvf $BKFILE $BKFOLDER > $BKRESULT clear echo backup saved to: echo $BKFILE echo and the results writed to: echo $BKRESULT exit elif [ $OPT = "User-Difine" ]; then echo "Please specify a name for the backup" read FILENAME tar -zcvf $BKDESTINATION$FILENAME.tar.gz $BKFOLDER > $BKDESTINATION$FILENAME.txt clear echo backup saved to: echo $BKDESTINATION$FILENAME.tar.gz echo and the results writed to: echo $BKDESTINATION$FILENAME.txt exit else clear echo bad selection exit fi done elif [ $OPT = "Quit" ]; then clear echo Backup aborded by user exit else clear echo bad selection exit fi done elif [ $OPT = "Run-multiple" ]; then echo "How many times would you like to backup $BKFOLDER ?" read TIMES echo "What is the time gap between the backups(in seconds)"? read DELAY while [ $TIMES -ne 0 ]; do tar -zcvf $BKFILE$TIMES $BKFOLDER > $BKRESULT$TIMES clear echo backup saved to: echo $BKFILE echo and the results writed to: echo $BKRESULT echo truew $TIMES sleep $DELAY let TIMES=$TIMES-1 done exit else clear echo bad selection exit fi done
save and close. Now make the file executable
chmod +x backup7.sh
execute that file with
./backup7.sh
Just a quick explanation of the script:
Now I have to add 2 more “read” commands to let the user decide how many times and how often to create backups. You already know about read so let’s go on to “While” statement. So let’s take a look first on the syntax of the “while” statement
while [ condition ]; do code to execute, as long as the "condition" is true done
most of the time in the “condition” we have an arithmetic variable and we increase or decrease the value of that variable until the “condition” in not true. We use While for example when we want to execute a set of commands for more than one time in a raw. You can also use a variable in the condition inside the while statement. If that is too complicated with the following example everything will be cleared.
Create a new script and name it while.sh
gedit ~/Desktop/bash/code/while.sh
write the following to gedit :
#!/bin/bash counter=10 while [ $counter -ne 0 ]; do echo $counter". Allaboutlinux.eu" let counter=counter-1 done
save and close. Now make the file executable
chmod +x while.sh
after the execution of that script you will get something like the following:
10. Allaboutlinux.eu
9. Allaboutlinux.eu
…..
1. Allaboutlinux.eu
And now I will explain to you how to create conditions. The condition in the previous example is:
[ $counter -ne 0 ]
$counter is the variable defined in the first line of that example and contains the number “10”, “-ne” stands for “Not Equal” and the “0” is just a number. So the while is checking if the variable “counter” is not equal to “0” and executes the code within “do-done”.
NOTE: It’s really important to increase or decrease the value of the variable that is compared in the condition inside the “loop”. (by loop I mean the code that is executed more than one time inside the “do” and the “done” commands of the previous example). Otherwise the loop will never end!
You can also use some other conditions like:
-eq = Equal to
-gt = Greater Than
-ge = Greater than or Equal to
-lt = Less Than
-le = Less than or Equal to.
I hope that in this point everything is clear. I know that this maybe a little confusing at the begining but try to read the script again and again and you will understand it.
The other new command that I use in that script is the “sleep” command. It actually does what is says it “sleeps” (freeze the script at that point) as long as you want.
So the command “sleep 5” will freeze the script for 5 seconds and after that the execution will continue normally.
But that’s enough for a tutorial named “Bash script for beginners” if I go any further I should change the Title.
Hope you enjoyed that. Please leave comments if something is not that clear, or if you want to create scripts that will help others to understand better this tutorial.
in part 6 when i am trying to execute it ,it is showing…command not found at line no 5’Quit’
Hello Gulfam wani
Thank you very much for this comment!!!
This is an encoding problem with the quotation mark.
Check the quotation mark in line 6 (LIST=”Backup Quit”)and the different quotation mark in line 8(if [ $OPT = “Backup” ]; then).
The quotation mark in line 8 is correct
The quotation mark in line 6 is NOT correct
please manually change this and then everything will work!
Thank you again!
Best Regards
Allaboutlinux.eu
Thanks it is working now…..
very interesting tutorial