fundamental of shell programming
DESCRIPTION
presentation will make you aware about the programming techniques in shell , and will help you to build more strong foundation into the world of shell programming.TRANSCRIPT
![Page 2: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/2.jpg)
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
![Page 3: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/3.jpg)
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
![Page 4: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/4.jpg)
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”
![Page 5: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/5.jpg)
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 |
![Page 6: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/6.jpg)
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”
![Page 7: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/7.jpg)
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”
![Page 8: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/8.jpg)
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 $?
![Page 9: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/9.jpg)
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
![Page 10: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/10.jpg)
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”
![Page 11: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/11.jpg)
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
![Page 12: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/12.jpg)
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”
![Page 13: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/13.jpg)
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
![Page 14: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/14.jpg)
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 <
![Page 15: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/15.jpg)
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
![Page 16: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/16.jpg)
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
![Page 17: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/17.jpg)
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
![Page 18: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/18.jpg)
18/12/13 OpenLab 18
Control Structure
● While Loop
while test condition
do
commands
done
![Page 19: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/19.jpg)
18/12/13 OpenLab 19
Control Structure
● Untile loop – works opposite way of while loop
until test commands
do
commands
done
![Page 20: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/20.jpg)
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
![Page 21: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/21.jpg)
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
} | }
|
![Page 22: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/22.jpg)
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
![Page 23: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/23.jpg)
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
![Page 24: Fundamental of Shell Programming](https://reader034.vdocuments.us/reader034/viewer/2022051322/546fb09bb4af9f350b8b463f/html5/thumbnails/24.jpg)
18/12/13 OpenLab 24
Thank You