Top Banner
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook http://www.freeos.com/guides/lsst/index.html
27

Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

Dec 24, 2015

Download

Documents

Theodore Boyd
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

Lab 8

Shell Script

Reference: Linux Shell Scripting Tutorial v1.05r3A Beginner's handbook http://www.freeos.com/guides/lsst/index.html

Page 2: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

myscript $0foo $1Bar $2

Special shell variables $0…$9

$# tells you how many parameter your script was given

Positional parameters or command line arguments

myscript foo bar

Page 3: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

if condition if condition which is used for decision making in shell script, If given

condition is true then command1 is executed.

Syntax:if condition then

command1... fi

Page 4: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

test expression or [ expr ] - -True return zero(0)Is used to see if an expression is true - -False returns nonzero Syntax:

test expression OR [ expression ]

$ vi ispositive # Script to see whether argument is positive#if test $1 -gt 0thenecho "$1 number is positive"fi

Run it as follows:

$ chmod 755 ispositive $ ./ispositive 5

Page 5: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 6: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 7: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 8: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

test expression or [ expr ]

For test statement with if commandFor [ expr ] statement

with if command

For test statement with if commandFor [ expr ]

statement with if command

Mathematical Operator in  Shell Script Meaning

Mathematical Operator in  Shell

Script Meaning

if [ 5 -eq 6 ] if test 5 -eq 6 is equal to -eq

if [ 5 -ne 6 ] if test 5 -ne 6 is not equal to -ne

if [ 5 -lt 6 ] if test 5 -lt 6 is less than -lt

if [ 5 -le 6 ] if test 5 -le 6 is less than or

equal to -le

if [ 5 -gt 6 ] if test 5 -gt 6 is greater than -gt

if [ 5 -ge 6 ] if test 5 -ge 6 is greater than or

equal to -ge

Page 9: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

test expression or [ expr ]

String Comparisons

Meaning Operator           

string1 is equal to string2 string1 = string2

string1 is NOT equal to string2 string1 != string2

string1 is NOT NULL or not defined  string1

string1 is NOT NULL and does exist -n string1

string1 is NULL and does exist -z string1

Page 10: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

test expression or [ expr ]

Logical Operators

Meaning Operator           

Logical NOT ! expression

Logical AND expression1  -a  expression2

Logical OR expression1  -o  expression2

Page 11: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

if …else…fi If given condition is true then command1 is executed

otherwise command2 is executed.

Syntax:

if condition then

command1 else

if condition is not true then execute all commands up to fi fi

Page 12: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

Exercise ( if..else with parameters)

if [ $# -eq 0 ]then echo " Enter one argument “ exit 1 else   echo " The first argument is $1 " fi

Page 13: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 14: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

if …else…fi $ vi isnump_n

# Script to see whether argument is positive or negative

if [ $# -eq 0 ]thenecho "$0 : You must give/supply one integers"exit 1fi if test $1 -ge 0thenecho "$1 number is positive"elseecho "$1 number is negative"fi

$ chmod 755 isnump_n

$ ./isnump_n 5

Page 15: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 16: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

if …else…fi Syntax:

if condition Then

if condition then

.....

.. do this

else .... .. do this

fi else

... ..... do this

fi

Page 17: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

Nested if-else-fi $ vi nestedif

osch=0echo "1. Unix (Sun Os)"echo "2. Linux (Red Hat)"echo –n "Select your os choice [1 or 2]? "read oschif [ $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"       fifi

$ chmod 755 nestedif

$ ./ nestedif

Page 18: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 19: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 20: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

Multilevel if-then-elseSyntax:

if condition then

condition is zero (true - 0) execute all commands up to elif statement

elif condition1 then

condition1 is zero (true - 0) execute all commands up to elif statement

elif condition2 then

condition2 is zero (true - 0) execute all commands up to elif statement

else None of the above condtion,condtion1,condtion2 are true (i.e. all of the above nonzero or false) execute all commands up to fi

fi

Page 21: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

$ 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

Page 22: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

for Loop

$ vi testfor

for NUMBER in 1 2 3 4 5doecho "Welcome $NUMBER times"done

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 for2 LIMIT = 10for ((  i = 1 ;  i <= LIMIT;  i++  ))do  echo "Welcome $i times"done

Page 23: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .
Page 24: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

for Loop$ vi nestedfor for (( 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

Page 25: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

while loop Syntax:

while [ condition ] do

command1 command2 command3 .. ....

done Write script to print numbers as 5,4,3,2,1, using while loop. The biggest number is supplied as command line argument.

i=$1while test $i != 0do echo –n "$i ," i=`expr $i - 1`doneecho " "

Page 26: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .

Exercise (1)

Write shell script using for loop to print the following patterns.

for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n "$i" done echo ”“ done

122333444455555

Page 27: Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook .