Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani
Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani
PRESENTED BY –
MUKESH HALWAN
PREMKANTH MENGANI
SHELL SCRIPTING
Linux has a variety of different shells:
Bourne shell (sh), C shell (csh), Korn
shell (ksh), TC shell (tcsh), Bourne
Again shell (bash).
Certainly the most popular shell is
“bash”. Bash is an sh-compatible shell
that incorporates useful features from
the Korn shell (ksh) and C shell (csh).
It offers functional improvements over
sh for both programming and
To make a file
Syntax -> vi filename.sh
Change permission
Syntax -> chmod u+x filename.sh
$ var=“test string”
$ newvar=“Value of var is $var”
$ echo $newvar
Value of var is test string
Using single quotes to show a string of characters will not allow variable
resolution
$ var=’test string’
$ newvar=’Value of var is $var’
$ echo $newvar
Value of var is $var
THE EXPORT COMMAND
The export command puts a variable into the environment so it will be accessible to
child processes. For instance:
$ x=hello
$ bash # Run a child shell.
$ echo $x # Nothing in x.
$ exit # Return to parent.
$ export x
$ bash
$ echo $x
hello # It's there.
If the child modifies x, it will not modify the parent’s original value. Verify this by
changing x in the following way:
$ x=ciao
$ exit
$ echo $x
hello
READ COMMAND
The read command allows you to prompt for input and store it
in a variable.
Example:
#!/bin/bash
echo -n “Enter name of file to delete: ”
read file
echo “Type 'y' to remove it, 'n' to change your mind ... ”
rm -i $file
echo "That was YOUR decision!”
$ let X=10+2*7
$ echo $X
24
$ let Y=X+2*4
$ echo $Y
32
$ echo “$((123+20))”
143
$ VALORE=$[123+20]
$ echo “$[123*$VALORE]”
17589
ARITHMETIC EVALUATION
Available operators: +, -, /, *, %
Example
$ cat arithmetic.sh
#!/bin/bash
echo -n “Enter the first number: ”; read x
echo -n “Enter the second number: ”; read y
add=$(($x + $y))
sub=$(($x - $y))
mul=$(($x * $y))
div=$(($x / $y))
mod=$(($x % $y))
# print out the answers:
echo “Sum: $add”
echo “Difference: $sub”
echo “Product: $mul”
echo “Quotient: $div”
echo “Remainder: $mod”
CONDITIONAL STATEMENTS
Conditionals let us decide whether to perform an action or not, this
decision is taken by evaluating an expression. The most basic form is:
if [ expression ];
then
statements
elif [ expression ];
then
statements
else
statements
fi
Put spaces after [ and before ], and around the operators and operands.
EXPRESSIONS
An expression can be: String comparison, Numeric comparison, File
operators and Logical operators and it is represented by [expression]:
String Comparisons:
Examples:
Examples:
Examples:
Example:
#!/bin/bash
echo “Enter a number 1 < x < 10:”
read num
if [ $num -gt 1 –a $num -lt 10 ];
then
echo “$num*$num=$(($num*$num))”
else
echo “Wrong insertion !”
fi
EXPRESSIONS
Logical operators:
Example:
#!/bin/bash
echo "Enter a number 1 < x < 10: "
read num
if [ $number -gt 1 ] && [ $number -lt 10 ];
then
echo “$num*$num=$(($num*$num))”
else
echo “Wrong insertion !”
fi
CASE STATEMENT
Used to execute statements based on specific values. Often used
in place of an if statement if there are a large number of
conditions.
case $var in
val1)
statements;;
val2)
statements;;
*)
statements;;
esac
EXAMPLE CASE.SH
#!/bin/bash
echo “Enter a number 1 < x < 10: ”
read x
case $x in
1) echo “Value of x is 1.”;;
2) echo “Value of x is 2.”;;
3) echo “Value of x is 3.”;;
4) echo “Value of x is 4.”;;
5) echo “Value of x is 5.”;;
6) echo “Value of x is 6.”;;
7) echo “Value of x is 7.”;;
8) echo “Value of x is 8.”;;
9) echo “Value of x is 9.”;;
0 | 10) echo “wrong number.”;;
*) echo “Unrecognized value.”;;
esac
ITERATION STATEMENTS & FOR LOOP
The for structure is used when you are looping through a range of variables.
statements are executed with var set to each value in the list.
Example
#!/bin/bash
let sum=0
for num in 1 2 3 4 5
do
let “sum = $sum + $num”
done
echo $sum
WHILE LOOP
syntax for bash shell
while [ condition ]
do
command1
command2
commandN
done
BASH WHILE LOOP EXAMPLE
#!/bin/bash
c=1
while [ $c -le 5 ]
do
#!/bin/bash
hello()
{
echo “You are in function hello()”
}