Download presentation
Presentation is loading. Please wait.
Published byYohanes Tedja Modified over 6 years ago
1
Copyright © 2016-2017 – Curt Hill
Bash Flow of Control Copyright © – Curt Hill
2
Copyright © 2016-2017 – Curt Hill
Introduction We have seen some of the basics of scripting now onto flow of control Everyone in this class knows C++ and one or more additional programming languages Covering flow of control should be comparatively easy Copyright © – Curt Hill
3
Copyright © 2016-2017 – Curt Hill
Aside As scripting languages go the shells are comparatively primitive They were originally conceived to make running systems of programs They were very early Almost every construct is an executable Thus the syntax is command syntax Copyright © – Curt Hill
4
Copyright © 2016-2017 – Curt Hill
Exit Condition Recall the form of main function: int main(int argc, char * argv[]) Every program returns an integer that indicates its action In general zero means success and anything else indicates some issues Each program determines its results It may also give an error message We may see the return code by echoing the expression $? Copyright © – Curt Hill
5
Copyright © 2016-2017 – Curt Hill
Example ls -l total 4 -rw-rw-r-- 1 curthill curthill 4 Nov 20 11:44 test.sh cd nowhere -bash: cd: nowhere: No such file or directory echo $? 1 ls -l -a total 12 drwxrwxr-x 2 curthill curthill 4096 Nov 20 11:44 . drwxr-xr-x 12 curthill curthill 4096 Nov 20 11:44 .. -rw-rw-r-- 1 curthill curthill 4 Nov 20 11:44 test.sh Copyright © – Curt Hill
6
Copyright © 2016-2017 – Curt Hill
Exit The exit statement may be used in a script to return a value Such as: exit 0 Copyright © – Curt Hill
7
Copyright © 2016-2017 – Curt Hill
Test This will often be the condition of an if statement It is merely an executable with parameters When executed it will produce a 0 or 1 that is used by an if It may be executed outside of an if The parameters determine what it tests First we will determine if a file exists Copyright © – Curt Hill
8
Copyright © 2016-2017 – Curt Hill
File Exists $ test -f test.sh $ echo $? $ test -f test.x 1 Copyright © – Curt Hill
9
Copyright © 2016-2017 – Curt Hill
The If The form of the if: if condition then statements fi Or if condition then statements else statements fi Copyright © – Curt Hill
10
Copyright © 2016-2017 – Curt Hill
Notes The condition is often a test statement but can be other things The statements then else fi are just markers There is no need for a block since the else and/or fi show the boundaries You may put multiple things on a line if you separate with semicolons Copyright © – Curt Hill
11
Copyright © 2016-2017 – Curt Hill
Example $ cat test.sh #!/bin/bash ls -a if test -f test.sh then echo "All good" else echo "where did it go?" fi if test -f missing.file ; then echo "I found it but I don't believe it" echo "As I thought - it is not here" $ ./test.sh test.sh test.sh~ All good As I thought - it is not here Copyright © – Curt Hill
12
Copyright © 2016-2017 – Curt Hill
Tracing – Apply -x $ ./test.sh + ls -a junk.sh test.sh test.sh~ + test -f test.sh + echo 'All good' All good + test -f missing.file + echo 'As I thought - it is not here' As I thought - it is not here Copyright © – Curt Hill
13
Copyright © 2016-2017 – Curt Hill
Back to Test Test comes in two forms test parms [ parms ] Thus the if test -f test.sh could have been if [ -f test.sh ] Many find this second form easier to read There must be blanks around things Copyright © – Curt Hill
14
Copyright © 2016-2017 – Curt Hill
Test Parameters DOS only gives us exist but test many more options -d file True if file is a directory. -e file True if file exists. -f file True if file exists and is a regular file. -L file True if file is a symbolic link. -r file True if file is a file readable by you. Copyright © – Curt Hill
15
Copyright © 2016-2017 – Curt Hill
More Options -w file True if file is a file writable by you. -x file True if file is a file executable by you. file1 -nt file2 True if file1 is newer than (according to modification time) file2 file1 -ot file2 True if file1 is older than file2 -z string True if string is empty. Copyright © – Curt Hill
16
Copyright © 2016-2017 – Curt Hill
Last Set of Options -n string True if string is not empty. string1 = string2 True if string1 equals string2. string1 != string2 True if string1 does not equal string2. Copyright © – Curt Hill
17
Copyright © 2016-2017 – Curt Hill
Null String Variables that are not defined have the empty string as a value Thus if [$v = $q] This could give a syntax error if q is empty if [ $v = ] Compare variables enclosed in quotes to avoid a syntax error Which is a run-time error in script languages Use if [ “$v” = “$q” ] Copyright © – Curt Hill
18
Copyright © 2016-2017 – Curt Hill
Elif Each if need a fi to end In the case of nested ifs this can be rather tedious and error prone In the case where we have an else if we can replace with an elif and not need another fi Copyright © – Curt Hill
19
Copyright © 2016-2017 – Curt Hill
Example if [ "$character" = "1" ]; then echo "You entered one.“ elif [ "$character" = "2" ]; then echo "You entered two." elif [ "$character" = "3" ]; then echo "You entered three." else echo “Oops." fi Copyright © – Curt Hill
20
Copyright © 2016-2017 – Curt Hill
The For Similar to the for all of Java Form: for arg in [list] do statements done List is a blank separated list Copyright © – Curt Hill
21
Copyright © 2016-2017 – Curt Hill
For Example $ ./c3.sh 1 3 5 7 9 11 $ cat c3.sh #!/bin/bash #count count=1 for count in ; do echo $count done exit 0 Copyright © – Curt Hill
22
Copyright © 2016-2017 – Curt Hill
The While Form is: while condition do statements done The while, do and done are reserved The condition is the same as in an if Copyright © – Curt Hill
23
Copyright © 2016-2017 – Curt Hill
Example $ ./counter.sh 1 2 3 4 5 6 7 8 9 $ cat counter.sh #!/bin/bash #count count=1 while [ $count -lt 10 ] ; do echo $count let count=$count+1 done exit 0 Copyright © – Curt Hill
24
Copyright © 2016-2017 – Curt Hill
Finally There are a more things to know that have to wait for a subsequent presentation Copyright © – Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.