Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Control Flow Statements Purpose test statement

Similar presentations


Presentation on theme: "Agenda Control Flow Statements Purpose test statement"— Presentation transcript:

1 Agenda Control Flow Statements Purpose test statement
if / elif / else Statements for loops while vs. until statements case statement break vs. continue statements exit statement

2 Control - Flow Statements
Control-Flow Statements (commands) alter the course of executions within a script. This lesson will look at the following control-flow commands test / if / else / elif / case (logic statements) until / while / for (loops) break / continue / exit (termination / skipping) We will also look at exit status (a condition code)

3 test Many logic statements perform a test to see if the condition is true or false and take appropriate action based on that outcome. The test utility is used to assign a status 0 if the condition is true or (not zero) if the condition is false. The variable $? Is used to display the status of the most recent test. See example below: set one two three test $# = 0 echo $? 1 Three arguments are stored as positional parameters. The test statement is testing the condition that there are zero arguments. The $? Variable (exit status) is displayed as non-zero (1) thus the condition is false.

4 if Used to test the status of a condition and proceed with an action of the status of the condition is true Examples: if test condition or if [ condition ] then then action action fi fi Note: fi marks end of if block. Square brackets [ ] are used as a “shortcut” for test command! Note space between condition & brackets!

5 Advanced Use of Test Command
You can also use square brackets to represent the “test” command For example: if test -f “$filename” or if [ -f “$filename” ] if [ ! -f “$filename” ] Refer to examples of advanced test command in /home/msaul/spr720/advanced_test Tests to see if contents of filename variable is a file in current directory This tests for opposite. (not a file)...

6 Advanced Use of Test Command
The Test command can be used with options to determine status of files: test -f file = true if "file" is a regular file test -d file = true if "file" is a directory test -r file = true if "file" is readable test -w file = true if "file" is writeable test -x file = true if "file" is executable test -s file = true if "file" is not empty test -z string = true if length of string is 0 test -n string = true if length of "string" is non-zero NOTE: test string = true if "string" is non-null

7 else Creates a two-way branch to perform one action if the status of the condition is true and perform another action if the status of the condition is false. See example below: if [ condition ] then action else other action fi Notice no then command after “else” statement was issued!

8 elif (i.e.“else if”) Creates a nested set of “if-then-else” structures. See example below: if [ condition ] then action elif [ condition ] then action else action3 fi Notice then command appears after “elif” statement was issued!

9 case The case command provides a multiple-branch decision structure based on matching or non-matching patterns. See example below on next slide.

10 case case $name in pattern1) commands ;; pattern2) commands ;;
esac An asterisk “*” can be used as a pattern to match all other situations. Also notice two semicolons ;; instead of one!

11 for The for statement is used to repeat operation for known or “fixed” values Unlike C programming, the for loop usually repeats tasks for “arguments” that are either issued from script, or stated directory after for statement.

12 for The for command uses a loop-index to take on the value of each argument: for varname in arg1 arg2 arg3 … arg11 do echo $varname done Task will repeat until all arguments “used-up”. Variable “varname” is takes on value of each argument

13 for The for statement can be used to assign a value to a variable within the loop that takes on values from positional parameters that were issued as arguments from a script or set by “command substitution: for varname do echo “Your name is: $varname” done

14 while / until statements
While and until statements are useful for “looping” until a particular condition occurs. This method allows “flexibility” if the number of tasks repeated (such as a for loop) is uncertain. The next two slides explain the difference between the while and until statements

15 Notice space between mathematical operator!
while The while command tests and executes a series of commands as long as a condition is true. See example below: num=0 read name while [ $num -lt 7 ] do echo “number is $num” num=$(($num + 1)) done Notice space between mathematical operator!

16 until statement The until command is similar to the while command. The until command differs by continuing the loop until a true condition exists. See example below: password=today name=nobody until [ “$name” = “password” ] do echo “Enter Password” read name done

17 break statement The break command is used to break the execution of the loop such as a for, while or until loop. The break command transfers control to the next line after the “done” statement. Although you can “break” a loop, it is not always considered to be a good programming practice.

18 Output of this script would be: 1 2 3
break statement Example: for num in do if [ $num = 4 ] then break fi echo $num done Output of this script would be: 1 2 3

19 continue statement The continue command is used to return control to the done statement which essentially “skips” the execution of the commands between “do” and “done” Therefore, you can use an if statement to with a continue statement to “skip” certain comands within a for, while and until statement

20 exit statement The exit statement is used to “terminate” a script that is running. Although your C programming instructor may indicate that this is “poor programming practice”, it can be used in shell scripting in many different ways. if [ $# = 0 ] then echo “Redo Command!” exit 1 fi The script will stop if it was run with no arguments after the script name

21 exit status Note : exit command by itself represents the exit status of the last command executed in the script exit command followed by a zero represents a true exit status that can be recalled as a variable $? exit command by a non-zero number (such as 1) will terminate a script (even within logic statements) and store false exit variable in $?


Download ppt "Agenda Control Flow Statements Purpose test statement"

Similar presentations


Ads by Google