► -le — To compare whether one number is less than or equal to the other number
► -ne — To compare whether two numbers are not equal
► -gt — To compare whether one number is greater than the other number
► -lt — To compare whether one number is less than the other number
The following shell program compares three numbers, number1, number2, and number3:
#!/bin/sh
number1=5
number2=10
number3=5
if [ $number1 -eq $number3 ]; then
echo "number1 is equal to number3"
else
echo "number1 is not equal to number3"
fi
if [ $number1 -ne $number2 ]; then
echo "number1 is not equal to number2"
else
echo "number1 is equal to number2"
fi
if [ $number1 -gt $number2 ]; then
echo "number1 is greater than number2"
else
echo "number1 is not greater than number2"
fi
if [ $number1 -ge $number3 ]; then
echo "number1 is greater than or equal to number3"
else
echo "number1 is not greater than or equal to number3"
fi
if [ $number1 -lt $number2 ]; then
echo "number1 is less than number2"
else
echo "number1 is not less than number2"
fi
if [ $number1 -le $number3 ]; then
echo "number1 is less than or equal to number3"
else
echo "number1 is not less than or equal to number3"
fi
When you execute the shell program, you get the following results:
number1 is equal to number3
number1 is not equal to number2
number1 is not greater than number2
number1 is greater than or equal to number3
number1 is less than number2
number1 is less than or equal to number3
File Operators
The following operators can be used as file comparison operators:
► -d — To ascertain whether a file is a directory
► -f — To ascertain whether a file is a regular file
► -r — To ascertain whether read permission is set for a file
► -s — To ascertain whether a file exists and has a length greater than zero
► -w — To ascertain whether write permission is set for a file
► -x — To ascertain whether execute permission is set for a file
Assume that a shell program called compare3 is in a directory with a file called file1 and a subdirectory dir1 under the current directory. Assume that file1 has a permission of r-x (read and execute permission) and dir1 has a permission of rwx (read, write, and execute permission). The code for the shell program would look like this:
#!/bin/sh
if [ -d $dir1 ]; then
echo "dir1 is a directory"
else
echo "dir1 is not a directory"
fi
if [ -f $dir1 ]; then
echo "dir1 is a regular file"
else
echo "dir1 is not a regular file"
fi
if [ -r $file1 ]; then
echo "file1 has read permission"
else
echo "file1 does not have read permission"
fi
if [ -w $file1 ]; then
echo "file1 has write permission"
else
echo "file1 does not have write permission"
fi
if [ -x $dir1 ]; then
echo "dir1 has execute permission"
else
echo "dir1 does not have execute permission"
fi
If you execute the shell program, you get the following results:
dir1 is a directory
file1 is a regular file
file1 has read permission
file1 does not have write permission
dir1 has execute permission
Logical Operators
Logical operators are used to compare expressions using Boolean logic, which compares values by using characters representing NOT, AND, and OR.
► ! —To negate a logical expression
► -a — To logically AND two logical expressions
► -o — To logically OR two logical expressions
This example named logic uses the file and directory mentioned in the previous compare3 example:
#!/bin/sh
if [ -x file1 -a -x dir1 ]; then
echo file1 and dir1 are executable
else
echo at least one of file1 or dir1 are not executable
fi
if [ -w file1 -o -w dir1 ]; then
echo file1 or dir1 are writable
else
echo neither file1 or dir1 are executable
fi
if [ ! -w file1 ]; then
echo file1 is not writable
else
echo file1 is writable
fi
If you execute logic, it yields the following result:
file1 and dir1 are executable
file1 or dir1 are writable
file1 is not writable
Special Statements: for, while, and Others
Bash comes with a variety of built-in statements to handle more complex condition checks such as loops and switch blocks. As with many things, bash shares the same syntax, so these next sections are applicable to both shells.
The for Statement
The for statement is used to execute a set of commands once each time a specified condition is true. The for statement has a number of formats. The first format used by bash is as follows:
for curvar in list