O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required