Fundamental of Shell Programming
-
Upload
rahul-hada -
Category
Technology
-
view
859 -
download
4
description
Transcript of Fundamental of Shell Programming
18/12/13 OpenLab 2
RoadMap● Basic of shell programming
– Creation of shell program
– Using variables
– Pipes
– Performing math
– Handling user input
● Control Structure
– if-then-else
– Loops● For , while , until
● Creating Function
18/12/13 OpenLab 3
Basic of Shell Programming● Creating a shell program
– Specify the shell you are using in the first line– #!/bin/bash , #!/bin/ksh etc.– Ex: Shell program to print something
#!/bin/bash
echo “Welcome to the world of Shell Programming”
● Execution of program– Shell script never compile
– First change mode then execute – cs@poornima$ chmod 775 hello.sh or chmod u+x– cs@poornima$ ./hello.sh
18/12/13 OpenLab 4
Basic of Shell Programming
● Variables – we can access the value of variable using $ sign.– Types of variables
● Environment Variables – stores specific system information. ex. $HOME,$SHELL,$LANGUAGE, $HOSTNAME,$HOSTTYPE etc
● User Variables – stores our own values in a variable.● Ex:
#!/bin/bash | #!/bin/basha=5 | read aecho “Value of a=$a” | echo “Value of a=$a”
18/12/13 OpenLab 5
Basic of Shell Programming
● Backtick (` `)– It allow you to assign the output of a shell
command to a variable
– Ex:-
#!/bin/bash | #!/bin/bash | #!/bin/bash
date | d=`date` | echo date
| echo $d |
18/12/13 OpenLab 6
Basic of Shell Programming
● Pipes (|)– The output of one command is the input of the
other command.
– Command1 | command2
– grep poornima history.txt | wc -l
– Ex:
#!/bin/bash
a=`grep poornima history | wc -l`echo “Number of Lines Containing Pattern=$a”
18/12/13 OpenLab 7
Basic of Shell Programming
● Evaluating expression– Two ways to evaluate
● Using expr expression● Using $[ expression]● They work differently for multiplication ( * )
– #!/bin/bash | #!/bin/bash
b=5 | b=5
c=6 | c=6
a=`expr $b + $c` | a=$[$b +$c]
echo “Sum =$a” | echo “Sum=$a”
18/12/13 OpenLab 8
Basic of Shell Programming
● Exiting the script
Every command runs in the shell uses an exit status.
● Whose value vary from 0-255 ● Value stores on ? Or $?● Explicit exit status from script● cs@poornima$ echo $?
#!/bin/bash | #!/bin/bash | #!/bin/bash
hello | ls -ytr | ls -l
echo $? | echo $? | echo $?
18/12/13 OpenLab 9
Basic of Shell Programming
● Exit Status Codes– 0 -------- successful completion of the command
– 2 -------- misuse of shell commands
– 127 -------- command not found
– 130 -------- command terminated with Clt-C
example using program
18/12/13 OpenLab 10
Basic of Shell Programming
● Command line Parameters– It allow you to add data values to the command
line when you execute the script.
– Ex : cs@poornima$./sum.sh 23 56
– Shell uses special variable, called positional parameters.
– Represented from 0 – 9 i.e $0 - $9
#!/bin/bash
s=`expr $1 + $2`
echo “Sum=$s”
18/12/13 OpenLab 11
Basic of Shell Programming
● Some special variables releated to the command line parameters– $* , $@ , $#
#!/bin/bash | #!/bin/bash | #!/bin/bash
echo $# | echo $* | echo $@
– Shift command – it downgrades each parameter variable one position by default.
#!/bin/bash ./cli1.sh 3 4 5
shift
shift
echo $1
18/12/13 OpenLab 12
Control Structure
● If-then-else
testing of if condition using test & [ ]
Structure Example
if command | #!/bin/bash
then | if test $1 -gt $2
command | then
fi | echo “First CL is greater”
| else | echo “Second CL is greater”
18/12/13 OpenLab 13
Control Structure
● Test of numeric comparisons– n1 -gt n2 -- check if n1 is greater then n2
– n1 -eq n2 -- check if n1 is equal to n2
– n1 -lt n2 -- check if n1 is lesser then n2
– n1 -le n2 -- check if n1 is lesser or equal to n2
– n1 -ge n2 -- check if n1 is greater or equal to n2
– n1 -ne n2 -- check if n1 is not equal to n2
18/12/13 OpenLab 14
Control Structure
● Test of string comparisons– str1 = srt2 -- check if str1 is the same as str2
– str1 != str2 – check if str1 is not equal to str2
– str1 > str2 – check if str1 is greater then str2
– str1 < str2 – check is str1 is lesser then str2
– -n str1--check if str1 has a length greater then zero
– -z str1-- check if str1 has length is zero
Note : Must use escape (\) symbol while using > or <
18/12/13 OpenLab 15
Control Structure
● Test file comparisons– -d file -- check if file exist and is a directory
– -e file -- check if file exist
– -f file -- check if file exist and is a file
– -r file -- check if file exist and is readable
– -w file – check if file exist and is writable
– -x file – check if file exist and is executable
– and, few more
18/12/13 OpenLab 16
Control Structure
● Case Statementcase variable in | #!/bin/bash
pattern1 | pattern2) | read ch
command;; | case $ch in
pattern3) | a|e|i|o|u)
command ;; | echo “char. is vowel”
*) | ;;
command;; | *)
esac | echo ”char is not vowel”
| ;;
| esac
18/12/13 OpenLab 17
Control Structure
● Loops types in shell for , while , until
for var in listdo commanddone
● Different ways to represent for loop● Can redirect the output of for loop in file● For loop can take input from file
18/12/13 OpenLab 18
Control Structure
● While Loop
while test condition
do
commands
done
18/12/13 OpenLab 19
Control Structure
● Untile loop – works opposite way of while loop
until test commands
do
commands
done
18/12/13 OpenLab 20
Guess output ?
#!/bin/bash | #!/bin/bash
for var in” $*” | for var in “$@”
do | do
echo “Output=$var” | echo “Output=$var”
done |done
18/12/13 OpenLab 21
Creation of function
● Function – reuse of same shell code● In shell function is a mini-script● Creation of function by two ways
function name { | name () {
commands | commands
} | }
|
18/12/13 OpenLab 22
Creation of Function
● Passing parameters in function● Returing values from function can be three
types :-– By Default it return exit status of the last
executed command in the function
– We can also use return to modify the exit status as per our own requirement
– Using echo to return values
18/12/13 OpenLab 23
Creation of Function
● Passing array in function● Decleration of array
Ex:- myarray = (1 2 3 4 5)
access values of array -- ${myarray[$1]} index 1
${myarray[*]} all array
– Example FunArrVar.sh
18/12/13 OpenLab 24
Thank You