Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]

Similar presentations


Presentation on theme: "Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]"— Presentation transcript:

1 Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]

2 Terminating Programs: The exit Exit will end the program and when a number is included, the exit code will be the number you assigned. Otherwise, a 0 exit code will reflect a successful exit and a non zero will mean a failed script or command. Use echo $? To display the last exit code.

3 Conditions and Tests The if-then construct The if-then-else Construct The if-then-elif Construct

4 Structure of the if-then construct if [condition] then commands … … last – command fi

5 Rules of Conditions and Tests 1. The if statement ends with the reserved word fi (if typed backward). 2. The indentation is not necessary, but it certainly makes the code more readable. 3. If the condition is true, then all the commands between the then and fi, what is called the body of the if, are executed. If the condition is false, the body of the if is skipped, and the line after fi is executed.

6 if – then – else Construct if [condition] then true – commands … last – true – command else false – commands … last – false – command fi

7 Control Structures case.. in..esac case expression in pattern { |pattern }) list :: esac

8 Example #! /bin/sh echo menu test program stop=0 # reset loop-termination flag. while test $stop -eq 0 # loop until done do cat << ENDOFMENU # display menu 1 : print the date. 2, 3 : print the current working directory. 4. : exit ENDOFMENU echo echo 'your chioce? \c' # prompt read reply # read response. echo case $reply in # process response. "1") date ;; "2"|"3") pwd # display working directory ;; "4") stop=1 # set loop termination flag. ;; *) # default. echo illegal chioce # error. ;; esac done #week14 “case_in_esac”

9 Example $ cat > simple # Sample # A simple script to test the read command # echo "Give me a long sentence:\c" read Word1 Word2 Rest echo " $Word1 \n $Word2 \n $Rest" echo " End of my act“ (week14 simple, simple2 – what is the differences?)

10 Example #!/bin/ksh ## The largest of three# # # This program accepts three numbers and shows the largest of them# echo -e "Enter three numbers and I will show you the largest of them>> \c" read num1 num2 num3 if test "$num1" -gt "$num2" -a "$num1" -gt "$num3" then echo "The largest number is: $num1" elif test "$num2" -gt "$num1" -a "$num2" -gt "$num3" then echo "The largest number is: $num2" else echo "The largest number is: $num3" fi exit 0

11 True or false: The test command # test command example echo "Are you okay?" echo "input Y for yes or N for no:\c" read answer if test "$answer" = Y then echo "Glad to hear that" else echo "Go home or the hospital!" fi (week14 test1 and improved test2)

12

13 Logical “and” “or” “not” -a ---- Logical AND -o ---- Logical OR ! ---- Logical NOT

14 Test command numeric test operators -eq --- number1 –eq number2 Is number1 equal number2? -ne --- -gt --- -ge --- -lt --- -le ---

15 String Values Testing Test command can also be used in string coma rations. However, different set of operators will be used. $ STRING5 [Enter] $ test –z “$STRING5” [Enter] Use echo $? To see the testing result. 0--- True Non-zero number --- False Make R=12345 then test –z $R echo $? You will get a non zero exit code

16 Test Operator for string = ---string1 = string2 ; Does string1 match string2? != ---- -n ---- Does string contain characters(nonzero length?) -z ---- Is string an empty string (zero length)

17 Examples $DATE1=`date` $DATE2=`date` $if test “$DATE1”=“$DATE2” > then >echo “Stop! The computer clock is dead.” >else >echo “Everything is fine.” >fi (week14 dead_clock, alive_clock)

18 Test file characteristics -r ---test -r fileName ; Does file exist,and is it readable? -w --- ………………is it writeable? -s --- Does file exist, and a nonzero length? -f --- Does file exist, but is not a directory file? -d --- Does file exist, and is it a directory file?

19 Example #File testing script FILE=xyz if test -r "$FILE" then echo "READABLE" elif test -w "$FILE" then echo "WRITEABLE" else echo "Read and Write Access Denied" Fi (week14 fileTest We can run the program again after chmod 000 xyz

20 More about expr command $ x=100 echo $x x=$x+11 echo $x expr 123 \* 456 expr 10\%3 expr 10/2

21 let command (ksh) X=100 let X=X+1 echo $X let y=x*9 echo $y *** Pay attention the fact that no \ is needed

22 Redirecting the standard error 1. In C program under the Unix, it is not as easy as in Codewarrior. 2. Sometimes, we need to store the errors in a file to analyze it (this is especially true when dealing a large program). 3. Use cc fileName.c –o fileName 2> error to direct the errors to the file called error in your current directory. **1 > is the default and it is the same as >. 2 > is redirection of the error output.

23 Example For the bubble.c we saw before, we intentionally created some errors. cc bubble.c –o bb 2>error Will redirect the error message to the file called error in your current directory.

24 Debugging shell programs –sh It is easy to make mistakes when you write long and complex script files. 1. Because you do not compile script files. hence you do not have the luxury of compiler error checking. 2. Normally, for short script, one can run the script to try to decipher the error messages. 3. For long and complex script, sh command with some options will help.

25 sh command and its options sh –n --- Read the commands but does not execute them. sh –v --- Shows the shell input lines as they are read. sh –x --- Shows the commands and their arguments as they are executed.

26 Examples $sh –x BOX1 [Enter] (BOX1 week14) $sh –x BOX1 [Enter]


Download ppt "Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]"

Similar presentations


Ads by Google