Guides:Shells/sh-bash/if Statement and test Command

From CoderGuide

Jump to: navigation, search

Contents

If Statement and Logical Tests

Control statements are an important part of any language. In this section we'll be covering logical tests (performed by the test program), and the if statement.

if Statement

The if (and else, elif, fi) statement takes the following formats:

if test
then
    Statements...
fi
if test
then
    statements...
else
   statements...
fi


if test
then
    statements...
elif test
   statements...
fi

As you can see, elif is a short hand for "else if", but, with it, we only need to end our if-else-if-else... chain with one fi instead of many. It also makes the code easier to follow.


The test result is taken from the exit status of a program. There are actually two programs that always return the same exit code true and false. The test program returns an exit code based on the result of evaluating it's arguments. Any exit code of zero is treated as being true, all non-zero exit codes are treated as false (the exact opposite of C, and other programming languages).

To make things easier, '[' is aliased to the test command (the test command, like the echo command, are often built into the shell to speed up execution time). Here are some typical expression:

if true
then
  echo this will always run
else
  echo this never will
fi

#these next two if statements are functionally the same

if test "$var" = 5
then
  echo we got 5
fi

#the spaces between the square brackets is important. They must be separated by a 
#space from everything else.
if [ "$var" = 5 ]
then
  echo we got 5
fi

You can get more information on test parameters by reading the test man page ("man test").

Logical tests

Here is a list of tests that can be performed. Much of this is taken directly from the Linux man page.

General Logic

( expr )Placing a statement in parenthesis tells test to evaluate that expression first-- they basically do the same thing as in math.
expr1 -o expr2true if expr1 OR expr2 are true
expr1 -a expr2true only if expr1 AND expr2 are true
! exprLogical NOT: If the expr is true, then the result is false; if expr is false, then the result is true


Numerical Values

expr1 -gt expr2true if expr1 is greater than expr2
expr1 -lt expr2true if expr1 is less than expr2
expr1 -ge expr2true if expr1 is greater than, or equal to, expr2
expr1 -le expr2true if expr1 is less than, or equal to, expr2
expr1 -eq expr2true if expr1 is equal to expr2
expr1 -ne expr2true if expr1 is NOT equal to expr2

Strings

−n stringtrue if the length of the string is not zero
−z stringtrue if the length of the string is zero
str1 = str2true if str1 is the same as str2
str1 != str2true if str1 is not the same as str2

Files

−d filetrue if the file exists, and it is a directory
−e filetrue if the file exists
−s filetrue if the file exists, and it's size is greater than zero
−f filetrue if the file exists,and is a regular file
−x filetrue if the file exists, and execute (or search if a directory) permission is granted
−w filetrue if file exists, and write permission is granted
−r filetrue if file exists, and read permission is granted
file1 -nt file2true if file1 is newer (mtime) than file2
file1-ot file2true if file1 is older than file2
−h file OR -L filetrue if the file exists, and is a symbolic link
−O filetrue if file exists, and is owned by the effective user ID
−G filetrue if the file exists, and is owned by the effective group ID
−b filetrue if the file exists, and if it is a block special (device)
−c filetrue if the file exists, and is a character special (device)
file1 -ef file2true if file1 has the same device and inode numbers (i.e they point to the same piece of data)
−p filetrue if the file exists, and is a named pipe
−k filetrue If the file exists, and has the sticky bit set
−g filetrue If the file exists, and is set-gid
−u filetrue If the file exists, and is set-uid
Personal tools