Bash script for beginners
June 25, 2012 in Bash script
Bash scripting for beginners Part 6
Select and IF statement
Let’s try to add a simple “menu” in our script. Now we will give the option to the user to execute the backup or quit the program. So if you run that script and you decide that you don’t really want to run it, you will have the option to cancel that script. To do so let’s create a new script and name it backup5.sh
gedit ~/Desktop/bash/code/backup5.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 LIST="Backup Quit" select OPT in $LIST; do if [ $OPT = "Backup" ]; then tar -zcvf $BKFILE $BKFOLDER > $BKRESULT clear echo backup saved to: echo $BKFILE echo and the results writed to: echo $BKRESULT exit elif [ $OPT = "Quit" ]; then clear echo Backup aborded by user exit else clear echo bad selection exit fi done
save and close. Now make the file executable
chmod +x backup5.sh
execute that file with
./backup5.sh
Just a quick explanation of the script:
Here we have add a new variable named “LIST” that contains 2 words “Backup” and “Quit”. When you run:
“select OPT in $LIST; do”
That wiil create a list that you can choose between the words that were contained in variable “LIST” so in that case it will be something like:
1) Backup 2) Quit #?
If you have more words in the “LIST” you will get more options. So the script is now waiting for your input. Type “1” and press enter in order to select backup of your folder or type “2” and press enter in order to Quit without backup. When you make your selection the variable “OPT” will get the value of the text that is next to the number of your selection. Now the script has to check your selection and decide what to do next. In order to do that we will need an “IF statement”. The “IF statement” is defined like the following example:
if [ condition1 ]; then the code to execute if the condition 1 is true elif [ condition2 ]; then the code to execute if the condition 2 is true else the code to execute if none of the conditions 1,2 are true fi
So if you can understand the syntax above you will be able to understand the script backup5.sh. I have also added the command “clear” in this script that is just clearing the terminal from previous texts.
If you have any problems understanding this please leave a comment and I will try to answer.
Let’s go to the next part.
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