bash Execution Control COMP2101 Winter 2019
Bash Execution Control Scripts commonly can evaluate situations and make simple decisions about actions to take Simple evaluations and actions can be accomplished using && and Complex evaluations and multi-step actions are better handled using more sophisticated execution control commands Regardless of the complexity of a test, the resulting action command(s) may need to be run more than once, in a loop We may have multiple data items on which we need to perform one or more commands, also in a loop
Basic Topics Testing data if and case while and for
Test Command The test command evaluates an expression and sets its exit status based on whether the expression evaluates as true or false The test command can also be run using the [ alias, which uses a ] to mark the end of the expression on the command line Multiple expressions can be evaluated by putting -a (and) or -o (or) between expressions test -d /etc && echo "/etc exists" [ -d /etc ] && echo "/etc exists" [ -d /etc -a -r /etc ] && echo "/etc exists and is readable"
File Testing The following are commonly used file tests, although there are more not included here -e filename : filename exists as any kind of filesystem object -f filename : filename exists and is a regular file (can hold data) -d filename : filename exists and is a directory -r filename : filename exists and is readable by whoever is doing the test -w filename : filename exists and is writable by whoever is doing the test -h filename : filename exists and is a symbolic link -s filename : filename exists and is not empty Putting! in front of the test operator ( the letter with a dash in front of it ) inverts the test
Lab 3 -filetests.sh
Test Expressions Test is generally used to either test data (usually in variables), or to test file attributes Test expressions may test single things for some characteristic, or attribute - this is known as a unary test Test expressions may compare two things - this is known as a binary test Unary test expressions take the form -x thing, where -x is the test operator specifying what kind of test to perform Binary test expressions take the form thing1 operator thing2 where operator tells the test command what kind of comparison to perform Putting! in front of the test operator inverts the test
Unary Operators The only unary operator that checks a variable is -v variablename which is true if the variable exists and false if it doesn't Text strings can be tested to see if they have no text in them with -z "sometext" or have some text with -n "sometext" Since it would make no sense to do such a test on literal text, it is easy to see that we would use some kind of dynamic data with the -n or -z tests
Binary Operators For Text Text strings can be compared using the following operators "string1" = "string2" is true if the two strings are identical "string1"!= "string2" is true if the two strings are not identical Strings consisting of digits are compared as text by these operators, not as numbers
Lab 3 - stringtests.sh
Binary Operators For Numbers To compare numbers, there are several binary test operators available X -eq Y is true if X and Y are the same number X -ne Y is true if X and Y are not the same number X -lt Y is true if X is numerically less than Y X -gt Y is true if X is numerically more than Y X -le Y is true if X is numerically less than Y or X is equal to Y X -ge Y is true if X is numerically more than Y or X is equal to Y
Lab 3 - numtests.sh
Conditional Script Block Execution A list of action commands can be run, or not run, based on the success or failure of a testing command list The test command can evaluate expressions, so it is the most common command for the testlist An alternate testlist can be specified using elif, and you can use as many alternate testlist/ actionlist elifs as necessary A default actionlist to run if no testlists are successful can be included by using else if [ expr ]; then actionlist fi if testlist; then actionlist elif testlist; then actionlist else actionlist fi
Lab 3 - passwordguesser.sh
Script Block Looping An action list can be executed repeatedly (known as a loop) based on the success or failure of a testlist using the while command The break or continue commands can be used in the action list to get out of a loop early while testlist; break jumps to the done command and continues the script past the loop do done actionlist continue jumps back to the while command to redo the testing list
Lab 3 - guessinggame.sh
Advanced Topics Operating a fixed number of times on something or on a list of data items Multiple reference value tests for a single variable Lists of data require arrays to hold the entire list as a single variable with a dynamic size
For Command The for command allows repeated execution of a list with each word from a word list The for command specifies a variable name The first word in the list is put into the variable and the action list is run Then the next word in the list is put into the variable and the action list is run, until there are no more words to get from the list for varname in wordlist; do list done
Lab 3 - improved-systemid.sh
For Command The for command allows repeated execution of an action list controlled by evaluating 2 commands and a test expression, and is commonly used for loops that use a counter The initial command is run once The test expression is tested before the action list executes The loop command is run once after the action list After the loop command runs, the test is done to see whether to execute the action list and loop command again for (( initial command; test expression; loop command )); do list done
Lab 3-4guessesgame.sh
Case Command The case command allows execution of a list based on the value of a variable or word Very commonly used to process the special variables $1, $2, $3, etc. It allows comparing a variable's contents to multiple reference values or patterns case $var in pattern ) list ;; pattern pattern ) list ;; * ) list ;; esac
COMMAND LINE ARGUMENTS Any command may have options and arguments The command line to run a script is accessible by the script, using the special variables $0, $1, $2, etc. $0 holds the command itself and $# holds a count of how many words are on the command line other than the command itself $1, $2, $3, etc. hold each of the remaining words from the command line On a bash command line, words are space-separated sequences of characters Quoting and the escape character can be used to create user-desired word boundaries on the command line (e.g. "My File" becomes a single word)
COMMAND LINE PROCESSING A loop can be used to cycle through the available command line arguments and interpret what is there We can use shift to renumber the command line variables each time through the loop Requiring arguments of the form -x or --option-name is known as using named arguments The case statement is better than the if statement for this while [ $# -gt 0 ]; do case "$1" in -h --help ) echo "Usage: $(basename $0) [-h --help]" exit ;; * ) echo "Unrecognized argument '$1'" exit 1 ;; esac shift done # Command line processed # Named arguments recognized and saved as needed
Lab 3 - cmdargs.sh
Show me your work to get the marks for it