Lab 8 Shell Script Reference Linux Shell Scripting

Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v 1. 05 r 3 A Beginner's handbook http: //www. freeos. com/guides/lsst/index. html

Special shell variables $0…$9 Positional parameters or command line arguments myscript foo bar myscript foo Bar $0 $1 $2 $# tells you how many parameter your script was given

if condition which is used for decision making in shell script, If given condition is true then command 1 is executed. Syntax: if condition then command 1. . . fi $ vi showfile #Script to print file # if cat $1 then echo -e "nn. File $1, found and successfully echoed" fi $ chmod 755 showfile $. /showfile-name

if condition $ vi trmif # Script to test rm command exist status # if rm $1 then echo "$1 file deleted" fi $ chmod 755 trmif $. /trmif file-name
![test command or [ expr ] Is used to see if an expression is test command or [ expr ] Is used to see if an expression is](http://slidetodoc.com/presentation_image_h2/5ca81906cee762fa06b97543ade3bfaf/image-5.jpg)
test command or [ expr ] Is used to see if an expression is true Syntax: - -True return zero(0) - -False returns nonzero test expression OR [ expression ] $ vi ispositive # Script to see whether argument is positive # if test $1 -gt 0 then echo "$1 number is positive" fi Run it as follows: $ chmod 755 ispositive $. /ispositive 5
![test command or [ expr ] Mathematical Operator in Shell Meaning Script Mathematical Operator test command or [ expr ] Mathematical Operator in Shell Meaning Script Mathematical Operator](http://slidetodoc.com/presentation_image_h2/5ca81906cee762fa06b97543ade3bfaf/image-6.jpg)
test command or [ expr ] Mathematical Operator in Shell Meaning Script Mathematical Operator in Meaning Shell Script -eq is equal to -ne is not equal to -lt is less than -le is less than or equal to -gt is greater than -ge is greater than or equal to For test statement with if command. For [ expr ] statement with if command if test 5 -eq 6 if [ 5 -eq 6 ] if test 5 -ne 6 if [ 5 -ne 6 ] if test 5 -lt 6 if [ 5 -lt 6 ] if test 5 -le 6 if [ 5 -le 6 ] if test 5 -gt 6 if [ 5 -gt 6 ] if test 5 -ge 6 if [ 5 -ge 6 ]
![test command or [ expr ] String Comparisons Operator Meaning tring 1 = string test command or [ expr ] String Comparisons Operator Meaning tring 1 = string](http://slidetodoc.com/presentation_image_h2/5ca81906cee762fa06b97543ade3bfaf/image-7.jpg)
test command or [ expr ] String Comparisons Operator Meaning tring 1 = string 2 string 1 is equal to string 2 string 1 != string 2 string 1 is NOT equal to string 2 string 1 is NOT NULL or not defined -n string 1 is NOT NULL and does exist -z string 1 is NULL and does exist
![test command or [ expr ] Logical Operators Operator Meaning ! expression Logical NOT test command or [ expr ] Logical Operators Operator Meaning ! expression Logical NOT](http://slidetodoc.com/presentation_image_h2/5ca81906cee762fa06b97543ade3bfaf/image-8.jpg)
test command or [ expr ] Logical Operators Operator Meaning ! expression Logical NOT expression 1 -a expression 2 Logical AND expression 1 -o expression 2 Logical OR

if …else…fi If given condition is true then command 1 is executed otherwise command 2 is executed. Syntax: if condition then condition else if condition is not true then execute all commands up to fi fi

if …else…fi $ vi isnump_n # Script to see whether argument is positive or negative if [ $# -eq 0 ] then echo "$0 : You must give/supply one integers" exit 1 fi if test $1 -ge 0 then echo "$1 number is positive" else echo "$1 number is negative" fi $ chmod 755 isnump_n $. /isnump_n 5

if …else…fi Syntax: if condition Then if condition then. . . . do this else. . . do this fi else. . . . do this fi

Nested if-else-fi $ vi nestedif osch=0 echo "1. Unix (Sun Os)" echo "2. Linux (Red Hat)" echo -n "Select your os choice [1 or 2]? " read osch if [ $osch -eq 1 ] ; then echo "You Pick up Unix (Sun Os)" else if [ $osch -eq 2 ] ; then echo "You Pick up Linux (Red Hat)" else echo "error only 1 or 2" fi fi $ chmod 755 nestedif $. / nestedif

Multilevel if-then-else Syntax: if condition then condition is zero (true - 0) execute all commands up to elif statement elif condition 1 then condition 1 is zero (true - 0) execute all commands up to elif statement elif condition 2 then condition 2 is zero (true - 0) execute all commands up to elif statement else None of the above condtion, condtion 1, condtion 2 are true (i. e. all of the above nonzero or false) execute all commands up to fi fi

$ vi elf # Script to test if. . elif. . . else # if [ $1 -gt 0 ]; then echo "$1 is positive" elif [ $1 -lt 0 ] then echo "$1 is negative" elif [ $1 -eq 0 ] then echo "$1 is zero" else echo "Opps! $1 is not number, give number" fi $ chmod 755 elf $. / elf 8 $. / elf -3 $. / elf 0 $. / elf a

for Loop Syntax: for { variable name } in { list } Do execute one for each item in the list until the list is not finished (And repeat all statement between do and done) done $ vi testfor NUMBER in 1 2 3 4 5 do echo "Welcome $NUMBER times" done $ vi for 2 LIMIT = 10 for (( i = 1 ; i <= LIMIT; i++ )) do echo "Welcome $i times" done

for Loop $ vi nestedfor (( i = 1; i <= 5; i++ )) ### Outer for loop ### do for (( j = 1 ; j <= 5; j++ )) ### Inner for loop ### do echo -n "$i " done echo "" #### print the new line ### done
- Slides: 16