17.2. Using logical operators with tests

Testing for a file’s status is OK, but in some instances you’ll want to compare one file’s status against another. The shell offers three types of logical operators for you to accomplish this:

-a Logical AND, true, if both sides of the operator are true
-o Logical OR, true, if either sides of the operator can be true
! Logical NOT, true, if the condition is false

Let’s now compare using these files.

-rw-r--r--  1 root  root    0 May 15 11:29 scores.txt -rwxr-xr--  1 root  root    0 May 15 11:49 results.txt 

The following example tests whether both files are readable.

						$ [ -w results.txt -a -w scores.txt ] 
$ echo $?
0
					

It finds out that they are.

To test if either of the files is executable use the logical ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.