Download presentation
Presentation is loading. Please wait.
Published byMichael Marshall Modified over 8 years ago
1
Shell script – part 2 CS 302
2
Special shell variable $0.. $9 Positional parameters or command line arguments For example, a script myscript take 2 arguments, foo and bar Using special variable, you can access your script and any parameters it has. myscript $0 foo $1 Bar $2 $# tells you how many parameter your script was given myscript foo bar
3
If condition if condition is used for decision making in shell script, If given condition is true then command1 is executed. Syntax: if condition then command1... fi
4
test expression or [ expr ] Is used to see if an expression is true Syntax: test expression OR [ expression ] Script.txt # Script to see whether argument is positive if test $1 -gt 0 then echo "$1 number is positive" fi Run it as follows: sh script.txt 3
5
test expression or [ expr ] For test statement with if commandFor [ expr ] statement with if command 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
6
test expression or [ expr ] String Comparisons MeaningOperator 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
7
test expression or [ expr ] Logical Operators MeaningOperator Logical NOT ! expression Logical AND expression1 -a expression2 Logical OR expression1 -o expression2
8
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
9
Example (1) if [ $# -eq 0 ] then echo " Enter one argument “ exit 1 else echo " The first argument is $1 " fi
10
Class exercise (1) Write a Script to see whether argument is positive or negative. First, you should ensure that a user enter an argument, if not exist and print this message “You must give/supply one integers”
11
# Script to see whether argument is positive or negative if [ $# -eq 0 ] then echo “You must give/supply one integer" exit 1 fi if test $1 -ge 0 then echo "$1 number is positive" else echo "$1 number is negative" fi
12
Nested if-else-fi Script.txt choice=0 echo "1. for coffee" echo "2. for tea" echo -n "select your drink choice [1 or 2] ?" read choice if test $choice -eq 1 then echo "you chosen coffee" else if test $choice -eq 2 then echo "you chosen tea" else echo "invalid input enter 1 or 2" fi
13
Multilevel if-then-else Syntax: if condition then condition is zero (true - 0) execute all commands up to elif statement 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 execute all commands up to elif statement else else None of the above condtion,condtion1,condtion2 are true (i.e. all of the above nonzero or false) (i.e. all of the above nonzero or false) execute all commands up to fi execute all commands up to fifi
14
Example : Script.txt if test $1 -gt 0 then echo "positive" elif test $1 -lt 0 then echo "negative" elif test $1 -eq 0 then echo "Zero" else echo "Not a number !" fi
15
For loop Syntax: for { variable name } in { list } Do execute one for each item in the list until the list is not finished execute one for each item in the list until the list is not finisheddone or: for ((initial vale ; condition ;)) Do execute one for each item in the list until the list is not finished execute one for each item in the list until the list is not finisheddone
16
Example Script.txt for NUMBER in 1 2 3 4 5 do echo "welcome ! $NUMBER" done Script.txt LIMIT = 10 for (( i = 1 ; i <= LIMIT; i++ )) do echo "Welcome $i times" done
17
While loop Syntax : while [ condition ] do do command1 command1 command2 command2 command3 command3............done
18
Example Script.txt i=$1 while test $i != 0 do echo –n "$i," i=`expr $i - 1` done echo " "
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.