Presentation on theme: "Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue."— Presentation transcript:
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
Control - Flow Commands Control-Flow commands alter the course of executions within a script. This lesson will look at the following control- flow commands test if / else / elif until / while / for case / break / continue / exit We will also look at exit status (a condition code)
test 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 last test. See example below: if test $# = 0 then echo “You must provide at least 1 argument” exit fi
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 conditionorif [ condition ] then then action action fifi Note: fi marks end of if block. Square brackets [ ] are used as a “shortcut” for test command! Note space between condition & brackets!
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/ops224/advanced_test Tests to see if contents of filename variable is a file in current directory This tests for opposite. (not a file)...
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
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!
elif (i.e.“else if”) Creates a nested set of “if-then-else” structures. See example below: if [ condition ] then action 1 elif [ condition ] then action2 else action3 fi Notice then command appears after “elif” statement was issued!
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.
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
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
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
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=`expr $num + 1` done Notice space between mathematical operator!
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
case The case command provides a multiple- branch decision structure based on matching or non-matching patterns. See example below on next slide.
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!
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.
break statement Example: for num in 1 2 3 4 5 do if [ $num = 4 ] then break fi echo $num done Output of this script would be: 1 2 3
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
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
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 $?