Checking condition by test
# cat check_gt10.sh
if test $1 -gt 10
then
echo "$1 is greater than 10"
fi
# chmod +x check_gt10.sh
# ./check_gt10.sh 19
19 is greater than 10
# ./check_gt10.sh 5
Checking condition by [expression]
# vi check_gt10_2.sh
if [ $1 -gt 10 ]
then
echo "$1 is greater than 10"
fi
# chmod +x check_gt10_2.sh
# ./check_gt10_2.sh 10
# ./check_gt10_2.sh 11
11 is greater than 10
With test or [expression] you can check condition of
-integer numbers.
-file types.
-character strings
Integer Number Comparison:
| Usage in shell script | Meaning | Mathematical Statements | Inside Shell Script | |
| Check by test statement with if condition | Check by [ expr ] statement with if condition | |||
| -eq | is equal to | 3 == 4 | if test 3 -eq 4 | if [ 3 -eq 4 ] |
| -ne | is not equal to | 3 != 4 | if test 3 -ne 4 | if [ 3 -ne 4 ] |
| -lt | is less than | 3 <> | if test 3 -lt 4 | if [ 3 -lt 4 ] |
| -le | is less than or equal to | 3 <= 4 | if test 3 -le 4 | if [ 3 -le 4 ] |
| -gt | is greater than | 3 > 4 | if test 3 -gt 4 | if [ 3 -gt 4 ] |
| -ge | is greater than or equal to | 3 >= 4 | if test 3 -ge 4 | if [ 3 -ge 4 ] |
Strings Comparison:
| Operator | Meaning of the using |
| string1 = string2 | string1 is equal to string2 |
| string1 != string2 | string1 is NOT equal to string2 |
| string1 | string1 is NOT NULL or not defined |
| -n string1 | string1 is NOT NULL and does exist |
| -z string1 | string1 is NULL and does exist |
Files Comparison:
| Test | Meaning of the text |
| -s file | Non empty file |
| -f file | Is File exist or normal file and not a directory |
| -d dir | Is Directory exist and not a file |
| -w file | Is writeable file |
| -r file | Is read-only file |
| -x file | Is file is executable |
Comparison with logical operators:
| Operator | Meaning of using |
| ! expression | Logical NOT |
| expression1 -a expression2 | Logical AND |
| expression1 -o expression2 | Logical OR |
Related Documents
No comments:
Post a Comment