Using Conditional Expressions

A conditional expression is an expression that evaluates to true or false. For instance, an expression might test whether a file exists or whether two variables are equal to each other. These expressions are encased in square brackets ([ ]) and are used by several commands described in the next few sections, including if, while, and until. The use of conditional expressions enables shell scripts to perform actions only when a condition exists or perform actions multiple times until an ending condition exists.

Table 4.6 summarizes conditional expressions. Because these expressions aren't useful except in conjunction with other commands, specific examples of their use will follow in upcoming sections.

Table 4.6: Conditional Expressions and Their Meanings

Conditional Expression

Meaning

-a file

True if file exists.

-b file

True if file exists and is a block file.

-c file

True if file exists and is a character file.

-d file

True if file exists and is a directory.

-e file

True if file exists.

-f file

True if file exists and is a regular file.

-g file

True if file exists and has its SGID bit set.

-h file

True if file exists and is a symbolic link.

-kfile

True if file exists and its "sticky" bit is set.

-p file

True if file exists and is a named pipe.

-r file

True if file exists and can be read.

-s file

True if file exists and is not empty.

-t fd

True if file descriptor^/refers to a terminal and is open.

-u file

True if file exists and has its SU ID bit set.

-w file

True if file exists and can be written.

-x file

True if file exists and can be executed.

-0 file

True if file exists and is owned by the effective user ID.

-G file

True if file exists and is owned by the effective group ID.

-L file

True if file exists and is a symbolic link.

-S file

True if file exists and is a socket.

-N file

True if file exists and has been modified since its last access.

filel -nt Hle2

True if /7/e7 has a more recent modification date thanf/'/e2.

filel -ot Hle2

True if /7/e 7 has a less recent modification date than///e2.

filel -ef Hle2

True if filel and file2 have the same inode and device numbers.

Conditional Expression

Meaning

-o optname

True if the option optname is enabled in the shell.

Conditional Expression

Meaning

-z string

True if string's length is zero.

-n string

True if string's length is not zero.

stringl = sthng2

True if the strings are equal.

stringl != sthng2

True if the strings are not equal.

stringl < sthng2

True tistringl comes before string2 lexicographically.

stringl > sthng2

True tistringl comes aftersthng2 lexicographically.

arg1 -eq arg2

True \iarg1 is equal toarg2. Both must be integers.

arg1 -ne arg2

True if arg1 is not equal \oarg2.

arg1 -It arg2

True \iarg1 is less than arg2.

arg1 -le arg2

True if arg1 is less than or equal toarg2.

arg1 -gt arg2

True if arg1 is greater than arg2.

arg1 -ge arg2

True if arg1 is greater than or equal toarg2.

0 0

Post a comment