Bash Scripting for Beginners (Part 4— Decision Making)
Let’s continue with if/else and case/esac statements.
If Statements
If statements allow us to make decisions in our Bash scripts. They allow us to decide whether or not to run a piece of code based upon conditions that we may set.
Format:
if [ <condition> ]
then
<do something>
fi
if…else statement
Here is a simple if statement that check to see if the condition is true or not. Depending on the result, it will do one of two things. If the condition is true than if block will run. Otherwise, else block will run. If we would like to check an expression then we may use the double brackets.
#!/bin/bashAGE=36
if [[ $AGE -gt 10 ]]
then
echo "You are greater than 10."
else
echo "You are smaller than 10."
fi
Output:
You are greater than 10.
if…elif…else statement
You can have one or more elif
clauses in the statement. The else
clause is optional. The conditions are evaluated sequentially. Once a condition returns True
, the remaining conditions are not performed.
if [ <condition> ]
then
<do something>
elif
then
<do something>
else
<do something>
fi
Example:
#!/bin/bash
echo -n "Enter a number: "
read NUM
if [[ $NUM -gt 10 ]]
then
echo "The variable is greater than 10."
elif [[ $NUM -eq 10 ]]
then
echo "The number is equal to 10."
else
echo "The number is less than 10."
fi
Output:
$ ./test.sh 5
The number is less than 10.
Nested if statements
It is possible to place an if
statement inside yet another if
statement. This is called nesting. Indenting is an important part of writing good, clean code. But not necessary. The aim is to improve readability.
#!/bin/bashnum=120if [ $num -gt 100 ]
then
echo Hey that\'s a large number.
if (( $num % 2 == 0 ))
then
echo And is also an even number.
fi
fi
Output:
Hey that's a large number.
And is also an even number.
Arithmetic Comparison
Below is the list of parameters used for numeric comparisons.
- num1 -eq num2 check if 1st number is equal to 2nd number
- num1 -ge num2 checks if 1st number is greater than or equal to 2nd number
- num1 -gt num2 checks if 1st number is greater than 2nd number
- num1 -le num2 checks if 1st number is less than or equal to 2nd number
- num1 -lt num2 checks if 1st number is less than 2nd number
- num1 -ne num2 checks if 1st number is not equal to 2nd number
String Comparison
Below is the list of parameters used for string comparisons.
- var1 = var2 checks if var1 is the same as string var2
- var1 != var2 checks if var1 is not the same as var2
- var1 < var2 checks if var1 is less than var2
- var1 > var2 checks if var1 is greater than var2
- -n var1 checks if var1 has a length greater than zero
- -z var1 checks if var1 has a length of zero
Note: When using > or < symbols in scripts, they should be used with escape character as “/>” or “/<“.
Example:
#!/bin/bash
name=aoyilmaz
if [ $USER = $name ]
then
echo "User exists"
else
echo "User not found"
fi
Output:
$ ./test.sh aoyilmaz
User exists
File Comparison
Below is the list of parameters used for file comparisons.
- -b filename Block special file
- -c filename Special character file
- -d directoryname Check for directory existence
- -e filename Check for file existence
- -f filename Check for regular file existence not a directory
- -G filename Check if file exists and is owned by effective group ID
- -g filename true if file exists and is set-group-id
- -k filename Sticky bit
- -L filename Symbolic link
- -O filename True if file exists and is owned by the effective user id
- -r filename Check if file is a readable
- -S filename Check if file is socket
- -s filename Check if file is nonzero size
- -u filename Check if file set-ser-id bit is set
- -w filename Check if file is writable
- -x filename Check if file is executable
- file A -nt file B checks if file A is newer than file B
- file A -ot file B checks if file A is older than file B
This script checks if the directory exists, if not create the directory.
#!/bin/bash
directory="./aoy"
if [ -d $directory ]
then
echo "Directory exists"
else
mkdir ./aoy
fi
Boolean Operators
If you need to compare two things and you don’t want to use nested if statements, you can use boolean operators instead.
- and — &&
- or — ||
#!/bin/bashif [ $1 == 'aoyilmaz' ] || [ $2 == '123' ]
then
echo Login confirmed.
else
echo Login failed
fi
Output:
$ ./test.sh aoyilmaz 123
Login confirmed.
Case Statement
The bash case
statement is generally used to simplify complex conditionals when you have multiple different choices.
Format:
case EXPRESSION inPATTERN_1)
STATEMENTS
;;PATTERN_2)
STATEMENTS
;;PATTERN_N)
STATEMENTS
;;*)
STATEMENTS
;;
esac
Example:
#!/bin/bashcase choice in
1)
echo You chose 1.
;;
2)
echo You chose 2.
;;
3)
echo You chose 3.
;;
*)
echo You did not choose anything.
;;
esac
Output:
$ ./test.sh 3
You chose 3.
I will continue in the next article.