Conditionals Execution (Decision Making)
Bash structured language constructs
You can use the if command to test a condition. For example, shell script may need to execute tar command only if a certain condition exists (such as backup only on Friday night).
If today is Friday execute tar command otherwise print an error message on screen.
More About Logic
- So far, the script you've used followed sequential flow:
#!/bin/bash echo "Today is $(date)" echo "Current directory : $PWD" echo "What Users Are Doing:" w
- Each command and/or statement is executed once, in order in above script.
- With sequential flow scripts, you cannot write complex applications (intelligent Linux scripts).
- However, with if command you will be able to selectively run certain commands (or part) of your script.
- You can create a warning message and run script more interactively using if command to execute code based on a condition.
But What Is A Condition?
- A condition is nothing but an expression that evaluates to a boolean value (true or false).
- In other words condition can be either true or false.
- A condition is used in shell script loops and if statements.
So, How Do I Make One?
A condition is mainly a comparison between two values. Open a shell prompt (console) and type the following command:
echo $(( 5 + 2 ))
Addition is 7. But,
echo $(( 5 < 2 ))
Answer is zero (0). Shell simple compared two number and returned result as true or false. Is 5 is less than 2? No. So 0 is returned. The Boolean (logical data) type is a primitive data type having one of two values
- 0 value indicates false.
- 1 or non-zero value indicate true.
|Operator||Example||Description||True / False||Evaluates To|
|5 > 12||echo $(( 5 > 12 ))||Is 5 greater than 12?||No (false)||0|
|5 == 10||echo $(( 5 == 10 ))||Is 5 equal to 10?||No (false)||0|
|5 != 2||echo $(( 5 != 2 ))||5 is not equal to 2?||Yes (true)||1|
|1 < 2||echo $(( 1 < 2 ))||Is 1 less than 2?||Yes (true)||1|
|5 == 5||echo $(( 5 == 5 ))||Is 5 equal to 5?||Yes (true)||1|
Now, it makes no sense to use echo command for comparisons. But, when you compare it with some value it becomes very useful. For example:
if [ file exists /etc/resolv.conf ] then make a copy else print an error on screen fi The test command is used to check file types and compare values. Test is used in conditional execution. It is used for: File attributes comparisons Perform string comparisons. Basic arithmetic comparisons. test command syntax test condition OR test condition && true-command OR test condition || false-command OR test condition && true-command || false-command Type the following command at a shell prompt (is 5 greater than 2? ): test 5 -gt 2 && echo "Yes" test 1 -lt 2 && echo "Yes" Sample Output: Yes Yes You need to use the test command while making decisions. Try the following examples and note down its output: test 5 -eq 5 && echo Yes || echo No test 5 -eq 15 && echo Yes || echo No test 5 -ne 10 && echo Yes || echo No test -f /etc/resolv.conf && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found." test -f /etc/resolv1.conf && echo "File /etc/resolv1.conf found." || echo "File /etc/resolv1.conf not found." Now, you can use the if statement to test a condition. if command The general syntax is as follows: if condition then command1 command2 ... commandN fi OR if test var == value then command1 command2 ... commandN fi OR if test -f /file/exists then command1 command2 ... commandN fi OR if [ condition ] then command1 command2 .... .. fi If given condition is true than the command1, command2..commandN are executed. Otherwise script continues directly to the next statement following the if structure. Open a text editor and create the script called verify.sh: #!/bin/bash read -p "Enter a password" pass if test "$pass" == "jerry" then echo "Password verified." fi Save and close the file. Run it as follows: chmod +x verify.sh ./verify.sh Sample Outputs: Enter a password : jerry Password verified. Run it again: ./verify.sh Sample Output: Enter a password : tom The if structure is pretty straightforward. The read command will read the password and store it to variable called pass. If $pass (i.e. password) is equal to "jerry", then "Password verified." is displayed. However, if it is not equal to "jerry", the script does not print any message and script will go to the next statement. Here is another example (number.sh): #!/bin/bash read -p "Enter # 5 : " number if test $number == 5 then echo "Thanks for entering # 5" fi if test $number != 5 then echo "I told you to enter # 5. Please try again." fi Enter # 5 : 5 Thanks for entering # 5 Save and close the file. Run it as follows: chmod +x number.sh ./number.sh Sample Outputs: Enter # 5 : 5 Thanks for entering # 5 Try it again: ./number.sh Sample Outputs: En ter # 5 : 11