Presentation is loading. Please wait.

Presentation is loading. Please wait.

Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.

Similar presentations


Presentation on theme: "Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison."— Presentation transcript:

1 Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison between two values.“ test or [ expr ] statements or even exist status can be also used. Expreession is defined as: "An expression is nothing but combination of values, relational operator (such as >, etc) and mathematical operators (such as +, -, / etc )."

2 Information Networking Security and Assurance Lab National Chung Cheng University 2 Command Line Arguments Here $# (built in shell variable ) will be 2 (Since foo and bar only two Arguments),

3 Information Networking Security and Assurance Lab National Chung Cheng University 3 The Example $ sum 11 20 $ math 4 - 7 $ d $ bp -5 myf +20 $ Ls * $ cal $ findBS 4 8 24 BIG

4 Information Networking Security and Assurance Lab National Chung Cheng University 4 The Test Program #!/bin/sh # # Script that demos, command line args # echo "Total number of command line argument are $#" echo "$0 is script name" echo "$1 is first argument" echo "$2 is second argument" echo "All of them are :- $* or $@" $* (which expand to `$1,$2...$9`): Note that $1..$9 i.e command line arguments to shell script is know as "positional parameters". can't assign the new value to command line arguments $1 = 5 $2 = "My Name"

5 Information Networking Security and Assurance Lab National Chung Cheng University 5 Some Notes The special string “$*”  It is replaced by all of the positional parameters, i.e. all of the arguments. The special string “$@”  Acts in the same way as $*  If you want to pass the arguments to another program, other_program "$@" is a good way to do it.

6 Information Networking Security and Assurance Lab National Chung Cheng University 6 Example 1 #!/bin/sh # #Script to print file # if cat $1 then echo -e "\n\nFile $1, found and successfully echoed" fi # # Script to test rm command and exist status # if rm $1 then echo "$1 file deleted" fi

7 Information Networking Security and Assurance Lab National Chung Cheng University 7 Example 2 #!/bin/sh # # Script to see whether argument is positive # if test $1 -gt 0 then echo "$1 number is positive" fi

8 Information Networking Security and Assurance Lab National Chung Cheng University 8 Logical Operator (I) - Mathematics

9 Information Networking Security and Assurance Lab National Chung Cheng University 9 Logical Operator (II) – String & File

10 Information Networking Security and Assurance Lab National Chung Cheng University 10 if...else...fi #!/bin/sh # # 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 -gt 0 then echo "$1 number is positive" else echo "$1 number is negative" fi

11 Information Networking Security and Assurance Lab National Chung Cheng University 11 Nested if-else-fi

12 Information Networking Security and Assurance Lab National Chung Cheng University 12 For Loop (I) for i in 1 2 3 4 5 do echo "Welcome $i times" done if [ $# -eq 0 ] then echo "Error - Number missing form command line argument" echo "Syntax : $0 number" echo "Use to print multiplication table for given number" exit 1 fi n=$1 for i in 1 2 3 4 5 6 7 8 9 10 do echo "$n * $i = `expr $i \* $n`" done

13 Information Networking Security and Assurance Lab National Chung Cheng University 13 For Loop (II) for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done

14 Information Networking Security and Assurance Lab National Chung Cheng University 14 While Loop if [ $# -eq 0 ] then echo "Error - Number missing form command line argument" echo "Syntax : $0 number" echo " Use to print multiplication table for given number" exit 1 fi n=$1 i=1 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1` done

15 Information Networking Security and Assurance Lab National Chung Cheng University 15 Case Statement if [ -z $1 ] then rental="*** Unknown vehicle ***" elif [ -n $1 ] then # otherwise make first arg as rental rental=$1 fi case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not gat a $rental for you";; esac

16 Information Networking Security and Assurance Lab National Chung Cheng University 16 Export command $ vech=Bus $ echo $vech Bus $ export vech $ /bin/bash $ echo $vech Bus $ exit $ echo $vech Bus $ vech=Bus $ echo $vech Bus $ /bin/bash $ echo $vech NOTE:-Empty line printed $ vech=Car $ echo $vech Car $ exit $ echo $vech Bus

17 Information Networking Security and Assurance Lab National Chung Cheng University 17 Functions Type SayHello() at $ prompt as follows $ SayHello() { echo "Hello $LOGNAME, Have nice computing" return } To execute this SayHello() function just type it name as follows: $ SayHello Hello vivek, Have nice computing. This way you can call function. Note that after restarting your computer you will loss this SayHello() function, since its created for current session only. To overcome this problem and to add you own function to /etc/bashrc file (for all) or ~/.bashrc

18 Information Networking Security and Assurance Lab National Chung Cheng University 18 Why to write function? Saves lot of time. Avoids rewriting of same code again and again Program is easier to write. Program maintains is very easy.

19 Information Networking Security and Assurance Lab National Chung Cheng University 19 The $ Symbol Variables $$ = The PID number of the process executing the shell. $? = Exit status variable. $0 = The name of the command you used to call a program. $1 = The first argument on the command line. $2 = The second argument on the command line. $n = The nth argument on the command line. $* = All the arguments on the command line. $# The number of command line arguments.

20 Information Networking Security and Assurance Lab National Chung Cheng University 20 http://www.freeos.com/guides/lsst/ch08.html


Download ppt "Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison."

Similar presentations


Ads by Google