#! /bin/bash
var_1=10
var_2=20
#First expression about "test" command
if test $var_1 -le $var_2
then
echo "var_1 is less than var_2"
else
echo "var_2 is less than var_1"
fi
#Second expression about "test" command, after the bracket,
#we must leave one space. Please keep in mind, [] is another
#expression of test command, all variables inside are taken as
#the argument of test command
if [ $var_1 -le $var_2 ]
then
echo "var_1 is less than var_2"
else
echo "var_2 is less than var_1"
fi
2. test command expression
#! /bin/bash
# -d decides whether the input is one directory
if [ -d "text" ]
then
echo "text is directory"
else
echo "text is not directory"
fi
#output: text is not directory
# -n decides whether the input is empty string
if [ -n "" ]
then
echo "this is not empty string"
else
echo "this is empty string"
fi
#output: this is empty string
# -z decides if input string is empty string or not
# this is "reversed way" of -n
if [ -z "" ]
then
echo "this is empty string"
else
echo "this is not empty string"
fi
# output: this is empty string
# -r decides if the input file is readable
if [ -r "text" ]
then
echo "text is readable"
else
echo "text is not readable"
fi
#text has read permission
#output: text is readable
# -s decides if input file is empty or not
if [ -s "text" ]
then
echo "text is not empty"
else
echo "text is empty"
fi
#text file is not empty
#output: text is not empty
# -w decides if input file is writable or not
if [ -w "text" ]
then
echo "text is writable"
else
echo "text is not writable"
fi
#text file is writable in system
#output: text is writable
# -x decides if input file is executable or not
if [ -x "text" ]
then
echo "text is executable"
else
echo "text is not executable"
fi
#text file is not executable in system
#output: text is not executable
# '=' could only be used to compare string, no way to
# compare number, following operation is just comparing
# "30+60" and "90", both are taken as string
if [ 30+60 = 90 ]
then
echo "equal string"
else
echo "unequal string"
fi
#output: unequal string
# '!=' could only be used compare string, same as above
if [ 30+60 != 90 ]
then
echo "unequal string"
else
echo "equal string"
fi
#output: unequal string
# -eq is used to compare numbers only, we use arithmatic expression below
# to compute "30+60"
if [ $((30+60)) -eq 90 ]
then
echo "equal number"
else
echo "unequal number"
fi
#output: equal number
# -ne is used to compare number only, decide whether 2 numbers are unequal
if [ $((30+60)) -ne 90 ]
then
echo "unequal number"
else
echo "equal number"
fi
#output: equal number
# -lt is used to compare numbers only, decide whether first number is less
# than 2nd number
if [ 50 -lt 90 ]
then
echo "50 is less than 90"
else
echo "50 is larger than 90"
fi
#output: 50 is less than 90
# -gt is used to compare numbers only, decide whether first number is larger
# than 2nd number
if [ 90 -gt 50 ]
then
echo "90 is greater than 50"
else
echo "90 is less than 50"
fi
#output: 90 is greater than 50
# -le is used to compare numbers only, decide whether first number is less
# than or equal to 2nd number
if [ 50 -le 50 ]
then
echo "50 is less than or equal to 50"
else
echo "50 is larger than 50"
fi
#output: 50 is less than or equal to 50
# -ge is used to compare numbers only, decide whether first number is greater
# than or equal to 2nd number
if [ 50 -ge 50 ]
then
echo "50 is greater than or equal to 50"
else
echo "50 is less than 50"
fi
#output: 50 is greater than or equal to 50
3. Require arguements
script:
#! /bin/bash
file=""
#normally we use double quote to circle the $file, if $file is empty
#sometimes in some system, it will cause error, since "test" command need arguments!
#With double quotes, even if $file is empty, we just take one empty string as arguement.
if [ -r "$file" ]
then
echo "file is readable"
else
echo "file is not readable"
fi
4. String Comparisons
script:
#! /bin/bash
var=""
#Normally we add one characterer before the string used to be compared
#Since if one of string is empty, in some system, it may make "test"
#command confuse
if [ "W" = "W$var" ]
then
echo "two strings are equal"
else
echo "two strings are not equal"
fi
5. Numeric test is only for integer
script_1
#! /bin/bash
var_1=4.5
var_2=4
if [ $var_1 -ge $var_2 ]
then
echo "var_1 is larger"
else
echo "var_2 is larger"
fi
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
./script_1: line 6: [: 4.5: integer expression expected
var_2 is larger
No comments:
Post a Comment