Korn Shell conditional expressions

A conditional expression is an expression placed between double brackets (‘[[‘ and ‘]]’). These expressions are used in conjuction with the test command to test the truth of a comparison. Their use is most beneficial in shell scripts. Word splitting and file name substitution are not performed in the expressions. The table below contains the unary and binary expressions allowed in these statements.

-a File
True, if the specified file is a symbolic link that points to another file that does exist.

-b File
True, if the specified file exists and is a block special file.

-c File
True, if the specified file exists and is a character special file.

-d File
True, if the specified file exists and is a directory.

-e File
True, if the specified file exists.

-f File
True, if the specified file exists and is an ordinary file.

-g File
True, if the specified file exists and its setgid bit is set.

-h File
True, if the specified file exists and is a symbolic link.

-k File
True, if the specified file exists and its sticky bit is set.

-n String
True, if the length of the specified string is nonzero.

-o Option
True, if the specified option is on.

-p File
True, if the specified file exists and is a FIFO special file or a pipe.

-r File
True, if the specified file exists and is readable by the current process.

-s File
True, if the specified file exists and has a size greater than 0.

-t FileDescriptor
True, if specified file descriptor number is open and associated with a terminal device.

-u File
True, if the specified file exists and its setuid bit is set.

-w File
True, if the specified file exists and the write bit is on. However, the file will not be writable on a read-only file system even if this test indicates true.

-x File
True, if the specified file exists and the execute flag is on. If the specified file exists and is a directory, then the current process has permission to search in the directory.

-z String
True, if length of the specified string is 0.

-L File
True, if the specified file exists and is a symbolic link.

-O File
True, if the specified file exists and is owned by the effective user ID of this process.

-G File
True, if the specified file exists and its group matches the effective group ID of this process.

-S File
True, if the specified file exists and is a socket.

File1 -nt File2
True, if File1 exists and is newer than File2.

File1 -ot File2
True, if File1 exists and is older than File2.

File1 -ef File2
True, if File1 and File2 exist and refer to the same file.

String1 = String2
True, if String1 is equal to String2.

String1 != String2
True, if String1 is not equal to String2.

String = Pattern
True, if the specified string matches the specified pattern.

String != Pattern
True, if the specified string does not match the specified pattern.

String1 < String2
True, if String1 comes before String2 based on the ASCII value of their characters.

String1 > String2
True, if String1 comes after String2 based on the ASCII value of their characters.

Expression1 -eq Expression2
True, if Expression1 is equal to Expression2.

Expression1 -ne Expression2
True, if Expression1 is not equal to Expression2.

Expression1 -lt Expression2
True, if Expression1 is less than Expression2.

Expression1 -gt Expression2
True, if Expression1 is greater than Expression2.

Expression1 -le Expression2
True, if Expression1 is less than or equal to Expression2.

Expression1 -ge Expression2
True, if Expression1 is greater than or equal to Expression2.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: