Pages

Monday, June 19, 2017

If Command Options - Complete List

Here is the complete list of if command options 

a) For file related operations

-a FILE : True if FILE exists.
-b FILE : True if FILE exists and is a block-special file.
-c FILE : True if FILE exists and is a character-special 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 its SGID bit is set.
-h FILE : True if FILE exists and is a symbolic link.
-k FILE : True if FILE exists and its sticky bit is set.
-p FILE : True if FILE exists and is a named pipe (FIFO).
-r FILE : True if FILE exists and is readable.
-s FILE : True if FILE exists and has a size greater than zero.
-t FD : True if file descriptor FD is open and refers to a terminal.
-u FILE : True if FILE exists and its SUID (set user ID) bit is set.
-w FILE : True if FILE exists and is writable.
-x FILE : True if FILE exists and is executable.
-O 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.
-N FILE : True if FILE exists and has been modified since it was last read.
-S FILE : True if FILE exists and is a socket.

b) For String Comparison

-z STRING : True of the length if "STRING" is zero.
-n STRING : or  STRING : True if the length of "STRING" is non-zero.
STRING1 == STRING2 : True if the strings are equal. 
STRING1 != STRING2 : True if the strings are not equal.
STRING1 < STRING2 : True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
STRING1 > STRING2 : True if "STRING1" sorts after "STRING2" lexicographically in the current locale.

c) For Integer Comparison

Hope below examples clarify how to do integer comparison

if [ "$a" -eq "$b" ]
if [ "$a" -ne "$b" ]
if [ "$a" -gt "$b" ]
if [ "$a" -ge "$b" ]
if [ "$a" -lt "$b" ]
if [ "$a" -le "$b" ]

d) For Logical Operations

EXPR1 -a EXPR2 : True if both EXPR1 and EXPR2 are true.
EXPR1 -o EXPR2 : True if either EXPR1 or EXPR2 is true.

No comments:

Post a Comment