ch5 condition execution
spaces around the [ and ] are required echo $? return non-zero for failure and zero for success --------- #!/bin/bash # Test for failure cat “$1” if [ “$?” -ne “0” ]; then echo “Error: Reading $1 failed.” fi --------- -r test to see if a file exists and is readable #!/bin/bash # Test for likely causes of failure if [ ! -r “$1” ]; then echo “Error: $1 is not a readable file.” echo “Quitting.” exit 1 fi cat "$1" ---------- if [ -r “$1” ]; then cat “$1”; fi if [ ! -r “$1” ]; then echo “File $1 is not readable – skipping. “; fi ---------- In the shell, [ is a program, and ] is simply an argument to it, which is required, but discarded. It has no significance to the shell whatsoever. ---------- The ability to execute a directory is treated as the right to change into that directory ---------- This adds ~/bin to the PATH environment, but only if that directory exists: if [ -d ~/bin ]; then PATH=$PATH:~/bin fi ---------- cat rwx #!/bin/bash whi...