Condition Evaluation in UNIX Scripts

One of our previous blog post showed you how a decision is made using the result of evaluated condition in if statement.
This post will provide a handy list of useful conditions.

Evaluating Numbers

-eq returns true for equality. Ex:  [ $ANSWER -eq 10 ]
-ne returns true for inequality
-lt returns true if the variable is less than other variable/number.
-gt returns true if the variable is greater than other variable/number.
-le returns true if the variable is less than or equal to other variable/number.
-ge returns true if the variable is greater than or equal to other variable/number.

Evaluating Strings

string1 = string2 returns true if both strings are the same
string1 != string2 returns true if both strings are the not the same
-z string returns true if the string is null
-n string returns true if the string is not null
string returns true if the string is not null
string1 < string2 returns true if ASCII value of string1 is less than that of string2
string1 > string2 returns true if ASCII value of string1 is greater than that of string2

Logical Operators

! Negates the expression Ex: [ ! string1 < string2 ] evaluates for greater than equal to check on ASCII values
-a And operator
-o Or operator

Evaluating files

-d directory_name returns true if directory directory_name exists
-f file_name returns true if (regular) file file_name exists
-s file_name returns true if (regular) file file_name exists and is more than 0 bytes
-r file_name returns true if (readable) file file_name exists
-w file_name returns true if (writable) file file_name exist
-x file_name returns true if (executable) file file_name exists
-L file_name returns true if symbolic link file_name exists
-k file_name returns true if sticky bit is set
-S file_name returns true if file_name is a socket
file1 -nt file2 returns true if file1 is newer than file2
file1 -ot file2 returns true if file1 is older than file2
-b file_name returns true if file_name is a block device
-c file_name returns true if file_name is a char device
-p file_name returns true if file_name is a pipe

Comments

Popular posts from this blog

OS/DB Migration - CMD. STR, TOC, EXT, R3load, DDLDBS.TPL and more

Fixing Inconsistent Table - Table activation fails due to inconsistency between DD and DB

301 Redirect Using SAP Web Dispatcher