44.20 test: Testing Files and StringsUNIX has a command called test that does a lot of useful tests. For instance, test can check whether a file is writable before your script tries to write to it. It can treat the string in a shell variable as a number and do comparisons ("is that number less than 1000?"). You can combine tests, too ("if the file exists and it's readable and the message number is more than 500..."). Some versions of test have more tests than others. For a complete list, read your shell's manual page (if your shell has test built in (1.10 ) ) or the online test (1) manual page. The test command returns a zero status (44.7 ) if the test was true or a non-zero status otherwise. So people usually use test with if , while , or until . Here's a way your program could check to see if the user has a readable file named .signature in the home directory:
The test
command also lets you test for something that
isn't
true. Add an exclamation point ( if test ! -r $HOME/.signature then echo "$myname: Can't read your '.signature'. Quitting." 1>&2 exit 1 fi
UNIX also has a version of test
(a link to the same
program, actually) named if [ ! -r $HOME/.signature ] then echo "$myname: Can't read your '.signature'. Quitting." 1>&2 exit 1 fi
- |
|