Thursday, November 18, 2010

If Else in Shell Scripting

Primary expressions
Primary
Meaning
[ -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.
[ FILE1 -nt FILE2 ]
True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ]
True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ]
True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ]
True if shell option "OPTIONNAME" is enabled.
[ -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. "=" may be used instead of "==" for strict POSIX compliance.
[ 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.
[ ARG1 OP ARG2 ]
"OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.


Expressions may be combined using the following operators, listed in decreasing order of precedence:
Combining expressions
Operation
Effect
[ ! EXPR ]
True if EXPR is false.
[ ( EXPR ) ]
Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ]
True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]
True if either EXPR1 or EXPR2 is true.


Numeric Comparisons:
-eq
equal to
-ne
not equal to
-lt
less than
-le
less than or equal to
-gt
greater than
-ge
greater than or equal to


The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.



Comparison operators (binary)

integer comparison:
-eq
is equal to
if [ "$a" -eq "$b" ]
-ne
is not equal to
if [ "$a" -ne "$b" ]
-gt
is greater than
if ["$a" -gt "$b" ]
-ge
is greater than or equal to
if [ "$a" -ge "$b" ]
-lt
is less than
if [ "$a" -lt "$b" ]
-le
is less than or equal to
if [ "$a" -le "$b" ]
< 
is less than (within double parentheses)
(("$a" < "$b"))
<=
is less than or equal to (within double parentheses)
(("$a" <= "$b"))
> 
is greater than (within double parentheses)
(("$a" > "$b"))
>=
is greater than or equal to (within double parentheses)
(("$a" >= "$b"))

string comparison:
=
is equal to
if [ "$a" = "$b" ]
==
is equal to
if [ "$a" == "$b" ]
This is a synonym for =.
   1 [[ $a == z* ]]    # true if $a starts with an "z" (pattern matching)
   2 [[ $a == "z*" ]]  # true if $a is equal to z*
   3 
   4 [ $a == z* ]      # file globbing and word splitting take place
   5 [ "$a" == "z*" ]  # true if $a is equal to z*
   6 
   7 # Thanks, S.C.
!=
is not equal to
if [ "$a" != "$b" ]
This operator uses pattern matching within a [[ ... ]] construct.
< 
is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
> 
is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
-z
string is "null", that is, has zero length
-n
string is not "null".
**The -n test absolutely requires that the string be quoted within the test brackets. Using an unquoted string with ! -z, or even just the unquoted string alone within test brackets normally works, however, this is an unsafe practice. Always quote a tested string.





Example Codes:-

OS=`uname -s`;
echo "OS= $OS";

if [ "$OS" = 'SunOS' ]; then
                echo "SunOS";
elif [ "$OS" = 'Linux' ]; then
                echo "Linux";
else
                echo "unknown";
fi;
exit;



------------------------------------------------------------------------------------------ 



f1_base()
{
        if [[ -d /tmp/logger ]] && [[ -d /tmp/testlogger ]]; then
                rm -f /tmp/logger/*;
                rm -f /tmp/testlogger/*;

        elif [[ -d /tmp/logger ]]; then
                mkdir /tmp/testlogger;
                rm -f /tmp/logger/*;
                rm -f /tmp/testlogger/*;

        elif [[ -d /tmp ]]; then
                mkdir /tmp/logger;
                mkdir /tmp/testlogger;

        else
                mkdir /tmp;
                mkdir /tmp/logger;
                rm -f /tmp/testlogger/*;

        fi;


        echo -e -n "\n\n\033[31mPlease provide the User Full Name: \033[m";
        read user_name;
}










No comments:

Post a Comment