Performing Tests
There will be times when your scripts will need to test for a certain condition before making a decision on which action to take. You might need to check for the existence of a certain file or directory, whether certain permissions are set on a file or directory, or for a myriad of other things. There are three ways to perform a test, which are:
- Use the keyword
test
followed by a test condition, with another command joined to it with either a&&
or a||
construct. - Enclose the test condition within a set of square brackets.
- Use an if. . .then construct
Let’s start by looking at the test
keyword.
Using the test Keyword
For our first example, let’s test to see whether a certain directory exists, and then create it if it doesn’t. Here’s how that works:
[donnie@fedora ~]$ test -d graphics || mkdir graphics
[donnie@fedora ~]$ ls -ld graphics/
drwxr-xr-x. 1 donnie donnie 0 Sep 26 15:41 graphics...