Grammar test conditions
Usually carried out in a variety of flow control structures bash various tests, and perform different operations according to the test results, sometimes by binding conditions if statement, we can easily complete the determination.
grammar
It supports three formats of grammar test conditions
- test <test expression>
- [<Test expression>]
- [[<Test expression>]]
Format 1 and Format 2 is equivalent to the format of the extended test command 3
You can use wildcards in [[]] in the match. &&, ||,>, <, etc. the operator can be applied [[]], but not the application in [].
example
An output file exists, the file does not exist, the output 0
[root@chenfanlinux ~]# [ -f /etc/passwd ] && echo 1||echo 0
1
[root@chenfanlinux ~]# test -f /etc/passwd && echo 1||echo 0
1
[root@chenfanlinux ~]# [[ -f /etc/passwd ]] && echo 1||echo 0
1
[root@chenfanlinux ~]# [[ -f /etc/passd ]] && echo 1||echo 0
0
Non-! Wording
test.txt file if does not exist, it is created.
[root@chenfanlinux ~]# test ! -f test1.txt && touch test.txt || ehco "test1.txt已经存在"
[root@chenfanlinux ~]# test ! -f test1.txt && touch test1.txt || echo "test1.txt已经存在"
test1.txt已经存在
[root@chenfanlinux ~]# [ ! -f test.txt ] && touch test.txt || echo 'test.txt已经存在'
[root@chenfanlinux~]# [ ! -f test.txt ] && touch test.txt || echo 'test.txt已经存在'
test.txt已经存在
test or [] grammar test
Functionally test 测试表达式
command is equivalent to [测试表达式]
, and therefore will be summarized herein summarize them together.
Numerical test
Parameter Description
parameter | Explanation |
---|---|
-eq | True equals |
-born | It is not equal to True |
-gt | True greater than |
-give | True or greater |
-lt | Less than True |
-the | Less than True |
example
Determining whether the two numbers are equal
[root@chenfanlinux ~]# num1=1
[root@chenfanlinux ~]#chenfanlinux num2=2
[root@chenfanlinux ~]# [ $num1 -eq $num2 ] && echo "两个数相等" || echo "两个数不相等"
两个数不相等
Numerical computation
[root@chenfanlinux ~]# a=5
[root@chenfanlinux ~]# b=6
[root@chenfanlinux ~]# c=$[a+b]
[root@chenfanlinux ~]# echo $c
11
[root@chenfanlinux ~]# ((c=a+b))
[root@chenfanlinux ~]# echo $c
11
[root@chenfanlinux ~]# c=`expr $a + $b`
[root@chenfanlinux ~]# echo $c
11
[root@chenfanlinux ~]# let c=a+b
[root@chenfanlinux ~]# echo $c
11
- (())
- let
- expr
- bc (decimal)
- $[]
String test
Parameter Description
parameter | Explanation |
---|---|
"Series 1" = "Series 2" | If the string is equal to a true string 2, you can use the "==" instead of "=" |
"String 1"! = "String 2" | If the string is not equal to a string of two true, but can not "! ==" instead of "! =" |
-z "string" | If the string length is true 0, -z be understood to zero |
-n "string" | If the string length is not 0 is true, -n can be understood as no zero |
example
Compares two strings are the same
num1="chenfan"
num2="chenfanlinux"
if [ "$num1" = "$num2" ]
then
echo '两个字符串相等!'
else
echo '两个字符串不相等!'
fi
File test
Parameter Description
parameter | Explanation |
---|---|
-e filename | True if file exists |
-r filename | If the file exists and is readable True |
-w file name | If the file exists and is writable True |
-x file name | If the file exists and is executable True |
-s filename | If the file exists and there is at least one character True |
-d filename | True if file exists and is a directory was |
-f filename | True if file exists and is a regular file |
-c filename | If the file exists and is a character special file True |
-b filename | If the file exists and is a block special file True |
String test operation symbol above table be sure to use the "string" in quotes.
example
File exists print presence
cd /bin
if test -e ./bash
then
echo '文件已存在!'
else
echo '文件不存在!'
fi
Logical Operators
Parameter Description
example
To determine whether documents exist
[root@chenfanlinux ~]# f1='/etc/rc.local'
[root@chenfanlinux ~]# f2='/etc/pass'
[root@chenfanlinux ~]# [ -f "$f1" -o -f "$f2" ] && echo 1|| echo 0
1
[root@chenfanlinux ~]# [ -f "$f1" -a -f "$f2" ] && echo 1|| echo 0
0
Original: Big Box Bash programming conditions of entry test (c)