Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while...

21
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

Transcript of Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while...

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

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

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

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)

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

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

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

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 betweencondition & brackets!

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

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)...

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

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 filetest -d file = true if "file" is a directorytest -r file = true if "file" is readabletest -w file = true if "file" is writeabletest -x file = true if "file" is executabletest -s file = true if "file" is not emptytest -z string = true if length of string is 0test -n string = true if length of "string" is

non-zeroNOTE: test string = true if "string" is non-null

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

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 actionfi

Notice no then command after “else” statement was issued!

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

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 action3fi

Notice then command appears after “elif” statement was issued!

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

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.

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

for The for command uses a loop-

index to take on the value of each argument:

for varname in arg1 arg2 arg3 … arg11do echo $varname

done

Task will repeat until all arguments “used-up”. Variable “varname” is takes on value of each argument

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

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

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

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

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

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 spacebetween

mathematical operator!

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

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” ]

doecho “Enter Password”read name

done

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

case The case command provides a

multiple-branch decision structure based on matching or non-matching patterns. See example below on next slide.

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

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!

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

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.

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

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:

123

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

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

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

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 1fi

The script will stop if it was run with no arguments after the script name

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

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 $?