Test command in the shell

Definition of test

The test command in Shell is used to check whether a certain condition is true. It can test three aspects: numerical value, character and file

format

test test condition or [test condition]

Note: There must be spaces on both sides of the brackets.
Test and [] have the same effect.

Test Conditions

Integer comparison

-eq is equal to (equal) [$a -eq $b]
-ne is not equal to (not equal) [$a -ne $b]
-gt is greater than [$a -gt $b]
-lt is less than (lesser than) [$a -lt $b]
-le lesser or equal [$a -le $b]
-ge greater or equal [$a -ge $b]

[root@haha ~]# Unum=$(who | wc -l)
[root@haha ~]# [ $Unum -gt 0 ] && echo "login user :$Unum"
login user :1
设置一个变量
如果有用户登录,就打印出登录的用户数量
[root@haha ~]# available_mem=`free -m | grep -i "mem" |
 awk '{print $7}'`
[root@haha ~]# [ $available_mem -lt 2048 ] && echo ${
    
    available
_mem}MB
714MB
判断当前主机可用内存大小,当低于2048时输出具体数值

String comparison

== Determine whether two strings are equal [" a "= =" a" == "a==" B"]
! = Determine whether two strings are not equal ["a"! = "A"! ="a!=" B"]
-z string to determine whether the string is empty [-z "$a"]

-n string to determine whether the string is not empty [-n "$a"]
str1> str2 to determine whether the string str1 is greater than str2 ["str1"> "str2"]
str1 <str2 to determine whether the string str1 is less than str2 [" str1" <"str2"]

[root@haha ~]# [ -n "abc" ] && echo 1 || echo 0
1
[root@haha ~]# [ -n " " ] && echo 1 || echo 0
1
[root@haha ~]# [ -n "" ] && echo 1 || echo 0
0
[root@haha ~]# [ -z "abc" ] && echo 1 || echo 0
0
[root@haha ~]# [ -z "" ] && echo 1 || echo 0
1

File comparison

-e exists (exist) [-e file]
-r can be read (read) [-r file]
-w can be written (write) [-w file]
-x executable (execute) [-x file]
-s exists Character [-s file]
-d exists and is a directory (directory) [-d file]
-f exists and is an ordinary file (file) [-f file]
-c exists and is a character-type special file [-c file]
- b exists and is a special block file (block) [-b file]
-nt check whether file1 is newer than file2 (new time) [file1 -nt file2]
-ot check whether file1 is older than file2 (old time) [file1 -nt file2 ]

[root@haha ~]# [ -f haha.txt ] && echo "haha is exist" || ech
o "haha is'n exist"
haha is exist

存在haha.txt这个普通文件,就输出||左侧内容,否则输出右侧

[root@haha ~]# [ -d haha ] && echo "haha is exist" || echo "ha
ha isn't exist"
haha isn‘t exist
[root@haha ~]# [ ! -f xxx ] && echo true || echo false
true

!表示取反的意思,即否定,感叹号和参数中间是有空格的不存在或者
xxx不是普通文件即为真

Logical operator

[[ ]] is an extended test command, &&, ||,>, <like this symbol can be used in [[ ]] but not in []
In [] and test:
-a and, both conditions are True, the condition is satisfied
-o or, one of the two conditions is true, the condition is satisfied
! not

In [[]] and (())
&& and, both conditions are true, the conditions are satisfied
|| or, one of the two conditions is true, and the conditions are satisfied
! not

[root@haha ~]#  [  -f haha -a -x haha.sh  ] && echo “1|| ec
ho “00

##haha是不是普通文件与 haha.sh是不是可执行的文件,需要同时
为真,才输出1

[root@haha ~]# [[  -f haha || -x haha.sh  ]] && echo “1|| ec
ho “01

##haha或者haha.sh其中成立一项,就输出1

Guess you like

Origin blog.csdn.net/weixin_52441468/article/details/112319871