Monday, April 12, 2010

if flags in UNIX shell scripting

/bin/[ is another name for /bin/test. It evaluates its arguments as a boolean expression, and exits with an exit code of 0 if it is true, or 1 if it is false.
If test is invoked as [, then it requires a closing bracket ] as its last argument. Otherwise, there must be no closing bracket.
test understands the following expressions, among others:
-e filename
True if filename exists.
-d filename
True if filename exists and is a directory.
-f filename
True if filename exists and is a plain file.
-h filename
True if filename exists and is a symbolic link.
-r filename
True if filename exists and is readable.
-w filename
True if filename exists and is writable.
-n string
True if the length of string is non-zero.
-z string
True if the length of string is zero.
string
True if string is not the empty string.
s1 = s2
True if the strings s1 and s2 are identical.
s1 != s2
True if the strings s1 and s2 are not identical.
n1 -eq n2
True if the numbers n1 and n2 are equal.
n1 -ne n2
True if the numbers n1 and n2 are not equal.
n1 -gt n2
True if the number n1 is greater than n2.
n1 -ge n2
True if the number n1 is greater than or equal to n2.
n1 -lt n2
True if the number n1 is less than n2.
n1 -le n2
True if the number n1 is less than or equal to n2.
! expression
Negates expression, that is, returns true iff expression is false.
expr1 -a expr2
True if both expressions, expr1 and expr2 are true.
expr1 -o expr2
True if either expression, expr1 or expr2 is true.
( expression )
True if expression is true. This allows one to nest expressions.

1 comment:

  1. Useful blog admin, keep sharing more like this. Excellent blog admin, these shell script concepts are really helpful and you have explained it clearly. Share more like this.
    Unix course in Chennai | Unix shell scripting training in Chennai

    ReplyDelete