CST8177 - Linux II Midterm Solution more shell Todd Kelley kelleyt@algonquincollege.com CST8207 - Todd Kelley 1 .midterm solution .debugging shell scripts .exit .case .is stdin a terminal? .the command that does nothing .integer arithmetic 2 Today's Topics .-v option for bash/sh .sh -v myscript .shell will print each line as its read .loop statements are printed once .-x option for bash/sh .sh -x myscript .shell will display $PS4 prompt and the expanded command before executing it .each loop iteration is shown individually Debugging shell scripts CST8177 - Todd Kelley 3 .exit causes the shell to exit with the exit status of the last command that was run .exit N causes the shell to exit with exit status N exit command CST8177 - Todd Kelley 4 case test-string in pattern-1) command1 command2 ;; pattern-2) command3 command4 ;; *) command5 ;; esac case statement CST8177 - Todd Kelley 5 .the patterns are globbing patterns matched to the test-string .So we tend to use the * pattern as a catchall, if all other matches fail, but that's not required .case statement exit status is the exit status of the last command in the matching block, or 0 if no blocks match case statement continued CST8177 - Todd Kelley 6 .We can use the vertical bar to specify alternative patterns: case "$character" in a|A) echo "The character is A" ;; [bB]) echo "the character is B" ;; *) echo "The character is not A or B" ;; esac case statement continued CST8177 - Todd Kelley 7 .A script can test whether or not standard input is a terminal [ -t 0 ] .What about standard output, and standard error? Is stdin a terminal? CST8177 - Todd Kelley 8 .Occasionally you'll see a command called : .: arguments .That command expands its arguments and does nothing with them, resulting in a 0 exit status The command that does nothing CST8177 - Todd Kelley 9 .examples of using expr command: a=`expr 3 + 4` a=`expr 3 - 4` a=`expr 3 * 4` a=`expr 13 / 5` # integer division: 2 a=`expr 13 % 5` # remainder: 3 .increment the integer in variable a a=`expr $a + 1` Doing integer arithmetic CST8177 - Todd Kelley 10