Lesson 2.1: Conditionally execute code (use of: if, test, [], etc.)
#!/bin/sh
“#!” is an operator called shebang which directs the script to the interpreter location. So, if we use #! /bin/sh
the script gets directed to the bourne-shell.
[sanjeeb@server Script]$ vim script1.sh
[sanjeeb@server Script]$ chmod +x script1.sh
[sanjeeb@server Script]$ ls -lh
total 4.0K
-rwxr-xr-x. 1 sanjeeb sanjeeb 15 Apr 7 07:44 script1.sh
[sanjeeb@server Script]$ cat script1.sh
#!/bin/bash
ls
[sanjeeb@server Script]$ bash script1.sh
script1.sh
Shell Variables
cat script1.sh
#!/bin/bash
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
[sanjeeb@server Script]$ bash script1.sh
what is your name?
sanjeeb
How do you do, sanjeeb?
All good
I am All good too!
Comparision Operators
Integer comparison
- -eq is equal to
- -ne is not equal to
- -gt is greater than
- -ge is greater than or equal to
- -lt is less than
- -le is less than or equal to
String Comparison
- == is equal to
- != is not equal to
- \< is less than, in ASCII alphabetical order
- \> is greater than, in ASCII alphabetical order
If statement
[sanjeeb@server Script]$ cat script2.sh
#!/bin/bash
a=10
b=10
if [ $a -ne $b ]
then
echo "Not Equal"
else
echo "Equal"
fi
[sanjeeb@server Script]$ bash script2.sh
Equal
Example : If Else Statement
[sanjeeb@client scripts]$ cat ifelse.sh
read -p "What is your username : " name
if [ $name = "sanjeeb" ]
then
echo "Welcome $name"
else
echo "Unknown user"
fi
# Output 1
[sanjeeb@client scripts]$ ifelse.sh
What is your username : sanjeeb
Welcome sanjeeb
# Output 2
[sanjeeb@client scripts]$ ifelse.sh
What is your username : ribik
Unknown user
Example : Nested If Statement
[sanjeeb@client scripts]$ cat ifelse.sh
read -p "What is your username : " name
if [ $name = "sanjeeb" ]
then
echo "Welcome $name"
else
if [ $name = "ribik" ]
then
echo "Welcome sanjeeb's friend $name"
else
echo "Unknown user"
fi
fi
# Output 1
[sanjeeb@client scripts]$ ifelse.sh
What is your username : sanjeeb
Welcome sanjeeb
# Output 2
[sanjeeb@client scripts]$ ifelse.sh
What is your username : ribik
Welcome sanjeebs friend ribik
# Output 3
[sanjeeb@client scripts]$ ifelse.sh
What is your username : ram
Unknown user
Using the ‘test’ Command
Flags for files and directories:
- test -e filename: Checks whether the file exists or not. And return 1 if file exists and returns 0 if file does not exist.
- test -d filename: Checks whether the file is a directory or not. And returns 0 if the file is a directory and returns 1 if the file is not a directory.
- test -f filename: Checks whether the file is a regular file or not. And returns 0 if the file is a regular file and returns 1 if the file is not a regular file.
- test -s filename: Checks whether the file is empty or not. And returns 0 if the file is not empty and returns 1 if the file is empty.
- test -r filename: Checks whether the file is readable or not. And returns 0 if the file is readable and returns 1 if the file is not readable.
- test -w filename: Checks whether the file is writable or not. And returns 0 if the file is writable and returns 1 if the file is not writable.
- test -x filename: Checks whether the file is executable or not. And returns 0 if the file is executable and returns 1 if the file is not executable.
Flags for text strings
- string1 = string2: Checks whether the two strings are equal or not. And returns 0 if the two strings are equal and returns 1 if the two strings are not equal.
- string1 != string2: Checks whether the two strings are not equal or not. And returns 0 if the two strings are not equal and returns 1 if the two strings are equal.
- -n string: Checks whether the string is empty or not. And returns 1 if the string is empty and returns 0 if the string is not empty.
- -z string: Checks whether the string is empty or not. And returns 0 if the string is empty and returns 1 if the string is not empty.
Flags for comparison of numbers
- num1 -eq num2: Checks whether the two numbers are equal or not. And returns 0 if the two numbers are equal and returns 1 if the two numbers are not equal.
- num1 -ne num2: Checks whether the two numbers are not equal or not. And returns 0 if the two numbers are not equal and returns 1 if the two numbers are equal.
- num1 -gt num2: Checks whether the first number is greater than the second number or not. And returns 0 if the first number is greater than the second number and returns 1 if the first number is not greater than the second number.
- num1 -ge num2: Checks whether the first number is greater than or equal to the second number or not. And returns 0 if the first number is greater than or equal to the second number and returns 1 if the first number is not greater than or equal to the second number.
- num1 -lt num2: Checks whether the first number is less than the second number or not. And returns 0 if the first number is less than the second number and returns 1 if the first number is not less than the second number.
- num1 -le num2: Checks whether the first number is less than or equal to the second number or not. And returns 0 if the first number is less than or equal to the second number and returns 1 if the first number is not less than or equal to the second number- .
[sanjeeb@server Script]$ cat script3.sh
#!/bin/bash
a=10
b=10
if test "$a" -eq "$b"
then
echo "Equal"
else
echo "Not equal"
fi
[sanjeeb@server Script]$ bash script3.sh
Equal
Example 1: Create a shell script called student.sh in the Script directory such that when the script executes with the argument “Linux”, it displays message “You are Linux Student”, when the script executes with the argument “Oracle”, it displays message “You are Oracle Student” and when the script executes with the argument “DevOps”, it displays message “You are DevOps Student” and when the script executes with other any parameter, it should display “You are Not Our Student”.
[sanjeeb@server Script]$ cat student.sh
#!/bin/bash
echo $1
if test "$1" = "Linux"
then
echo "You are Linux Student"
elif test "$1" = "DevOps"
then
echo "You are DevOps Student"
elif test "$1" = "Oracle"
then
echo " You are Oracle Student"
else
echo "You are not student"
fi
[sanjeeb@server Script]$ vim student.sh
[sanjeeb@server Script]$ student.sh DevOps
DevOps
You are DevOps Student
[sanjeeb@server Script]$ student.sh Linux
Linux
You are Linux Student
[sanjeeb@server Script]$ student.sh Oracle
Oracle
You are Oracle Student
[sanjeeb@server Script]$ student.sh Physics
Physics
You are not student
Example 2: Doing the same as example 1 using CASE
[sanjeeb@client scripts]$ cat case.sh
read -p "Enter your subject : " subject
case $subject in
Linux)
echo " You are Linux Student";;
Oracle)
echo " You are Oracle Student";;
Devops)
echo " You are Devops Student";;
*)
echo " You are not our student";;
esac
[sanjeeb@client scripts]$ case.sh
Enter your subject : Oracle
You are Oracle Student
[sanjeeb@client scripts]$ case.sh
Enter your subject : Devops
You are Devops Student
[sanjeeb@client scripts]$ case.sh
Enter your subject : Linux
You are Linux Student
[sanjeeb@client scripts]$ case.sh
Enter your subject : Science
Testing if previous command worked
# Result is 0 if worked
[sanjeeb@client scripts]$ echo $?
0
# Result is not zero if not worked
[sanjeeb@client scripts]$ echo $?
2
# Testing using a shell with exit
[sanjeeb@client scripts]$ cat case.sh
read -p "Enter your subject : " subject
case $subject in
Linux)
echo " You are Linux Student"
exit 0 ;;
Oracle)
echo " You are Oracle Student"
exit 1 ;;
Devops)
echo " You are Devops Student"
exit 2 ;;
*)
echo " You are not our student"
exit 3 ;;
esac
[sanjeeb@client scripts]$ case.sh
Enter your subject : Science
You are not our student
[sanjeeb@client scripts]$ echo $?
3
[sanjeeb@client scripts]$ case.sh
Enter your subject : Devops
You are Devops Student
[sanjeeb@client scripts]$ echo $?
2
[sanjeeb@client scripts]$ case.sh
Enter your subject : Linux
You are Linux Student
[sanjeeb@client scripts]$ echo $?
0
Usecase of Positional Parameters
[sanjeeb@client scripts]$ cat name.sh
echo "My first name is : $1"
echo "My middle name is : $2"
echo "My last name is : $3"
echo "My word count is : $#"
echo "My Full name is : $*"
[sanjeeb@client scripts]$ name.sh Sanjeeb Kumar KC
My first name is : Sanjeeb
My middle name is : Kumar
My last name is : KC
My word count is : 3
My Full name is : Sanjeeb Kumar KC
WHILE Loop
Example : Calculate the Simple Interest using while loop
[sanjeeb@client scripts]$ cat si.sh
num=0
while [ $num -le $1 ]
do
read -p "Enter P,N,I seperated by space: " P N R
SI=$[($P*$N*$R)/100]
echo "SI is : $SI"
num=$[$num+1]
done
[sanjeeb@client scripts]$ si.sh 2
Enter P,N,I seperated by space: 2000 10 10
SI is : 2000
Enter P,N,I seperated by space: 3000 10 5
SI is : 1500
Enter P,N,I seperated by space: 4000 100 20
SI is : 80000