linux之test命令详解

test作用检查某个条件是否成立,返回值为0(真)或者其他值(假),可通过echo $?查看返回值,也常用于循环和条件语句。

test一般有三种用法:

  1. 测试文件或者文件夹
  2. 字符串比较
  3. 数值比较

1.测试文件或者文件夹

参数说明
-e当路径存在时返回真
-f当路径存在且为文件时返回真
-d当路径存在且为文件夹时返回真
[root@linuxforliuhj test]# ll
total 20
-rw-r--r--. 1 root root 16 Oct 25 23:43 hello.sh
drwxr-xr-x. 2 root root  6 Oct 26 18:42 mk
-rw-r--r--. 1 root root 82 Oct 14 00:03 read.sh
lrwxrwxrwx. 1 root root  7 Oct 26 19:27 read.txt -> read.sh
-rw-r--r--. 1 root root 78 Oct 13 00:04 sub.sh
-rw-r--r--. 1 root root 53 Oct 25 23:37 test.txt
-rw-r--r--. 1 root root 55 Oct 25 23:45 te.txt

【1】判断路径是否存在使用-e参数,当路径存在时,返回值为0(真)

[root@linuxforliuhj test]# test -e test.txt
[root@linuxforliuhj test]# echo $?
0
[root@linuxforliuhj test]#
[root@linuxforliuhj test]# test -e mk
[root@linuxforliuhj test]# echo $?   
0
[root@linuxforliuhj test]#
[root@linuxforliuhj test]# test -e read.txt
[root@linuxforliuhj test]# echo $?         
0
[root@linuxforliuhj test]# 
[root@linuxforliuhj test]# test -e hellojava.txt
[root@linuxforliuhj test]# echo $?              
1
[root@linuxforliuhj test]# 

【2】判断路径是否存在并且是否为文件使用-f参数,当路径存在并且路径为文件时,返回值为0(真)

[root@linuxforliuhj test]# test -f hello.sh 
[root@linuxforliuhj test]# echo $?          
0
[root@linuxforliuhj test]#
[root@linuxforliuhj test]# test -f mk       
[root@linuxforliuhj test]# echo $?   
1
[root@linuxforliuhj test]#
[root@linuxforliuhj test]# test -f read.txt 
[root@linuxforliuhj test]# echo $?          
0
[root@linuxforliuhj test]#

【3】判断路径是否存在并且是否为文件夹使用-d参数,当路径存在并且路径为文件夹时,返回值为0(真)

[root@linuxforliuhj test]# test -d mk       
[root@linuxforliuhj test]# echo $?   
0
[root@linuxforliuhj test]# 
[root@linuxforliuhj test]# test -d hello.sh 
[root@linuxforliuhj test]# echo $?          
1
[root@linuxforliuhj test]#

2.字符串比较

参数说明
-z当str为空时返回真
-n当str为非空时返回真
=两个字符串相等时返回真
==两个字符串相等时返回真,同=
!=两个字符串不相等时返回真
[root@linuxforliuhj test]# str=''
[root@linuxforliuhj test]# test -z $str
[root@linuxforliuhj test]# echo $?     
0
[root@linuxforliuhj test]# str='linux'
[root@linuxforliuhj test]# test -z $str
[root@linuxforliuhj test]# echo $?     
1
[root@linuxforliuhj test]# test -n $str
[root@linuxforliuhj test]# echo $?     
0
[root@linuxforliuhj test]# 
[root@linuxforliuhj test]# str='linux'
[root@linuxforliuhj test]# str1='linux'
[root@linuxforliuhj test]# test $str = $str1
[root@linuxforliuhj test]# echo $?          
0
[root@linuxforliuhj test]# test $str == $str1
[root@linuxforliuhj test]# echo $?           
0
[root@linuxforliuhj test]# str2='linuy'
[root@linuxforliuhj test]# test $str = $str2
[root@linuxforliuhj test]# echo $?          
1
[root@linuxforliuhj test]# test $str != $str2
[root@linuxforliuhj test]# echo $?           
0
[root@linuxforliuhj test]# test $str != $str1
[root@linuxforliuhj test]# echo $?           
1
[root@linuxforliuhj test]#

注意:
str=‘linux’是赋值语句,等号左右两侧不能有空格;
test中$str = $str为比较语句,等号两侧必须有空格

3.数值比较

参数说明
-eq等于时返回真
-ne不等于时返回真
-lt小于时返回真
-le小于等于时返回真
-gt大于时返回真
-ge大于等于时返回真
[root@linuxforliuhj test]# a=5
[root@linuxforliuhj test]# b=5
[root@linuxforliuhj test]# test $a -eq $b
[root@linuxforliuhj test]# echo $?
0
[root@linuxforliuhj test]# test $a -ne $b
[root@linuxforliuhj test]# echo $?       
1
[root@linuxforliuhj test]#
[root@linuxforliuhj test]# a=4
[root@linuxforliuhj test]# b=5
[root@linuxforliuhj test]# test $a -lt $b
[root@linuxforliuhj test]# echo $?
0
[root@linuxforliuhj test]# test $a -gt $b 
[root@linuxforliuhj test]# echo $?       
1
[root@linuxforliuhj test]# test $a -le $b
[root@linuxforliuhj test]# echo $?       
0
[root@linuxforliuhj test]# test $a -ge $b 
[root@linuxforliuhj test]# echo $?       
1
[root@linuxforliuhj test]# 

4.逻辑运算

参数说明
-a逻辑与
-o逻辑或
逻辑非

举个栗子:

  • 判断文件hello.sh和文件夹mk是否同时存在,当二者同时存在时返回真
[root@linuxforliuhj test]# ll
total 24
-rw-r--r--. 1 root root 16 Oct 25 23:43 hello.sh
-rw-r--r--. 1 root root  0 Oct 26 20:19 linuy
drwxr-xr-x. 2 root root  6 Oct 26 18:42 mk
-rw-r--r--. 2 root root 82 Oct 14 00:03 read.ln
-rw-r--r--. 2 root root 82 Oct 14 00:03 read.sh
lrwxrwxrwx. 1 root root  7 Oct 26 19:27 read.txt -> read.sh
-rw-r--r--. 1 root root 78 Oct 13 00:04 sub.sh
-rw-r--r--. 1 root root 53 Oct 25 23:37 test.txt
-rw-r--r--. 1 root root 55 Oct 25 23:45 te.txt
[root@linuxforliuhj test]# test -f hello.sh  -a -d mk
[root@linuxforliuhj test]# echo $?
0
[root@linuxforliuhj test]# test -f hello.sh  -a -d mkfr
[root@linuxforliuhj test]# echo $?
1
[root@linuxforliuhj test]# 
  • 判断文件test.txt或者文件sub.sh是否存在,当其中任意一个文件存在时,返回真
[root@linuxforliuhj test]# test -f test.txt -o -f sub.sh 
[root@linuxforliuhj test]# echo $?
0
[root@linuxforliuhj test]# test -f test.txt -o -f sudhs.sh
[root@linuxforliuhj test]# echo $?
0
[root@linuxforliuhj test]# 
  • 判断某个路径是否存在
[root@linuxforliuhj test]# test ! -e te.txt
[root@linuxforliuhj test]# echo $?
1
[root@linuxforliuhj test]# test ! -e tettt.txt
[root@linuxforliuhj test]# echo $?            
0
[root@linuxforliuhj test]# 

5.shell中的test
在shell中test命令等同于[ commond ]
例如test a == b等同于[ a == b ],注意"[“后和”]“前都需要有空格,并且”=="两边也都要有空格

[root@linuxforliuhj test]# cat test.sh 
a='linux'
b='linux'
if test $a == $b
then
        echo 'a与b相等'
fi
[root@linuxforliuhj test]# sh test.sh 
a与b相等
[root@linuxforliuhj test]# 
[root@linuxforliuhj test]# cat test.sh  
a='linux'
b='linux'
if [ $a == $b ]
then
        echo 'a与b相等'
fi
[root@linuxforliuhj test]# sh test.sh  
a与b相等
[root@linuxforliuhj test]#

中括号[ ]的使用只是一种情况,还有其他的例如双括号(( ))、双中括号[[ ]]的使用等等,后续会继续介绍各种括号的使用方法。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐