Fundamental of Shell Programming

1,177 views

Published on

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.

Published in: Technology
3 Comments
1 Like
Statistics
Notes
  • Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming --- http://amzn.to/1WzJYZm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Linux Command Line and Shell Scripting Bible --- http://amzn.to/1LyurZd
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Classic Shell Scripting --- http://amzn.to/1R98HlK
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,177
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
45
Comments
3
Likes
1
Embeds 0
No embeds

No notes for slide

Fundamental of Shell Programming

  1. 1. Fundamental of Linux & Shell Programming Presented By:-Rahul Hada hada.rahul@gmail.com Mob:- 9001806370
  2. 2. RoadMap ● Basic of shell programming – – Using variables – Pipes – Performing math – ● Creation of shell program Handling user input Control Structure – – if-then-else Loops ● ● For , while , until Creating Function 18/12/13 OpenLab 2
  3. 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 3
  4. 4. Basic of Shell Programming ● Variables – we can access the value of variable using $ sign. – Types of variables ● ● ● 18/12/13 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/bash a=5 | read a echo “Value of a=$a” | echo “Value of a=$a” OpenLab 4
  5. 5. Basic of Shell Programming ● Backtick (` `) – It allow you to assign the output of a shell command to a variable – Ex:#!/bin/bash 18/12/13 | #!/bin/bash | d=`date` | echo date | echo $d date | #!/bin/bash | OpenLab 5
  6. 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 6
  7. 7. Basic of Shell Programming ● Evaluating expression – Two ways to evaluate ● ● ● Using expr expression Using $[ expression] They work differently for multiplication ( * ) | #!/bin/bash | b=5 c=6 | c=6 a=`expr $b + $c` | a=$[$b +$c] echo “Sum =$a” 18/12/13 #!/bin/bash b=5 – | echo “Sum=$a” OpenLab 7
  8. 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 $? 18/12/13 | | echo $? | echo $? OpenLab 8
  9. 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 9
  10. 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 10
  11. 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 18/12/13 echo $1 OpenLab 11
  12. 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 fi | then | echo “First CL is greater” | else | echo “Second CL is greater” 18/12/13 OpenLab 12
  13. 13. Control Structure ● Test of numeric comparisons – n1 -gt 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 -- check if n1 is greater then n2 OpenLab 13
  14. 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 14
  15. 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 15
  16. 16. Control Structure ● Case Statement case variable in | | pattern3) command ;; case $ch in | command;; read ch | pattern1 | pattern2) #!/bin/bash a|e|i|o|u) | | *) command;; echo “char. is vowel” ;; | esac *) echo ”char is not vowel” | 18/12/13 | ;; | esac OpenLab 16
  17. 17. Control Structure ● Loops types in shell for , while , until for var in list do command done ● 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 17
  18. 18. Control Structure ● While Loop while test condition do commands done 18/12/13 OpenLab 18
  19. 19. Control Structure ● Untile loop – works opposite way of while loop until test commands do commands done 18/12/13 OpenLab 19
  20. 20. Guess output ? #!/bin/bash | #!/bin/bash for var in” $*” | for var in “$@” do | do echo “Output=$var” | echo “Output=$var” done 18/12/13 |done OpenLab 20
  21. 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 { commands } | name () { | commands | } | 18/12/13 OpenLab 21
  22. 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 22
  23. 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 – 18/12/13 Example FunArrVar.sh OpenLab 23
  24. 24. Thank You 18/12/13 OpenLab 24

×