Bash Scripting
Bash Scripting
Introduction
• Bash is a command-line interface shell program
• A command language interpreter which allows you to directly control a
computer's operating system (OS) with command-line interface (CLI)
• In simple words script are recipe for the food, series of instructions
• A Bash script is a plain text file which contains a series of commands
• Anything you can run normally on the command line can be put into a script
and it will do exactly the same thing
• Similarly, anything you can put into a script can also be run normally on the
command line and it will do exactly the same thing
Importance of Bash Scripting
• Bash makes automating tasks much easier
• It is easier to manage multiple computing resources from one script
• Need little to zero programming knowledge
• Less resource-intensive
• Auditing and Debugging is easier
How to identify A Bash Script
• File extension is .sh
• Scripts start with a shebang which is combination of bash (#) and bang (!)
followed by bash shell path
#! /bin/bash
• This file will contain shebang and will print hello world using echo command.
#! /bin/bash
echo "Hello World!!!"
How to Run a basic Bash Script
• To actually run the bash script first you need to change permission of file and
make it executable
chmod u+x hello_world.sh
• And to run it
./hello_world.sh or
bash hello_world.sh
Define Variables
• Define a variable by using the syntax variable_name=value
• To get the value of the variable, add $ before the variable
• Any text after a hash sign (#) indicates the start of a comment
#! /bin/bash
# A simple variable example
greeting=Hello
name=NSEIT
echo $greeting $name
Arithmatic Expressions
Operator Usage
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
% Modulus
#! /bin/bash
var=$((3+9))
echo $var
How to read user input
• We can take user input using the "read" command
• To prompt the user with a custom message, use the -p flag.
#! /bin/bash
echo "Enter a number"
read a
read -p "Enter a another number" b
var=$((a+b))
echo $var
Conditional Statements
• Conditions are expressions that evaluate to boolean expression (true or false)
• To check conditions, we can use if, if-else, if-elif-else and nested conditionals
• To create meaningful comparisons, we can use AND -a and OR -o as well
If Statement
#!/bin/bash
number=101
if [ $number -gt 100 ]
then
echo “Hey that\'s a large number.”
fi
If Else Statement
#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi
If Elif Else Statement
#!/bin/bash
read x
if [ $x == 0 ]; then
echo "Number is Zero"
elif [ $((x%2)) == 0 ]; then
echo "Number is Even"
else
echo "Number is Odd"
fi
Nested If Statement
#!/bin/bash
if [ $1 -gt 100 ]
then
echo “Hey that's a large number”
if (( $1 % 2 == 0 ))
then
echo “And is also an even number”
fi
fi
Case Statements
• Used when a decision has to be made against multiple choices
• Used as the alternative of if-elif-else statement
• Case statement has an edge over if-statements because it improves the
readability of our code and they are easier to be maintained
Case Statement Example
#!/bin/bash
echo "Enter your lucky number"
read n
case $n in
101)
echo echo "You got 1st prize" ;;
510)
echo "You got 2nd prize" ;;
999)
echo "You got 3rd prize" ;;
*)
echo "Sorry, try for the next time" ;;
esac
Numeric Comparison logical
operators
• Comparison is used to check if statements evaluate to true or false
Operation Syntax Explanation
Equality num1 -eq num2 is num1 equal to num2
Greater than equal to num1 -ge num2 is num1 greater than
equal to num2
Greater than num1 -gt num2 is num1 greater than
num2
Less than equal to num1 -le num2 is num1 less than equal
to num2
Less than num1 -lt num2 is num1 less than num2
Not Equal to num1 -ne num2 is num1 not equal to
num2
Example
#! /bin/bash
read x
read y
if [ $x -gt $y ]
then
echo X is greater than Y
elif [ $x -lt $y ]
then
echo X is less than Y
elif [ $x -eq $y ]
then
echo X is equal to Y
fi
While Loop Example
#!/bin/bash
counter=1
while [ $counter -le 10 ]
do
echo $counter
((counter++))
done
echo All done
Until Loop Example
#!/bin/bash
counter=1
until [ $counter -gt 10 ]
do
echo $counter
((counter++))
done
echo All done
For Loop Example
#!/bin/bash
names=‘Nadeem Ahmad Khan'
for name in $names
do
echo $name
done
echo All done
Ranges Example
#!/bin/bash
for value in {10..0..2}
do
echo $value
done
echo All done
Continue Example
#!/bin/bash
i=0
while [[ $i -lt 11 ]]
do
if [[ "$i" == '2' ]]
then
echo "Number $i!"
break
fi
echo $i
((i++))
done
echo "Done!"
Function
• Think of a function as a small script within a script
• It's a small chunk of code which you may call multiple times within your script
#!/bin/bash
#it is a function
myFunction () {
echo “Hello World”
}
#function call
myFunction