学贵有恒

方有所成

导航

Linux 命令练习

test 命令: (鸟哥linux私房菜视频第30集)

test  -d : 判断一个文件是否为目录。

 eg: test -d passwd && echo "is a dir"|| echo "is not a dir"

test  -f : 判断一个文件是否为普通文件。
 eg: test -f sh01.sh  && echo "is a  regular file" || echo "is not a regular file"

test -e :判断一个文件是否存在。

 eg: test -e sh06.sh && echo "the file exits" || echo "the file does not exit"

 test -b: 判断一个文件是否为块设备文件。

eg: test -b /dev/root && echo "is a block device" || echo "is not a block device"

 test -p: 判断一个文件是否为管道文件。

eg:test -p /dev/log && echo "is a FIFO file" || echo "is not a fifo file"

 test -S: 判断一个文件是否为套接字文件。

eg: test -S /dev/log && echo "is a socket file" || echo "is not a socket file"    

 test -s: 判断一个文件是否存在或文件大小是否大于0。

eg: test -s sh06.sh && echo "exit or size > 0" || echo "not exit or size <=0"

 

 test -r/w/x + filename: 判断一个文件是否可读/写/执行。

 test -r sh01.sh  && echo "readable" ||echo "unreadable"

 test -u + filename: 判断一个文件是否设置了uid。

test -u sh01.sh && echo "set uid"|| echo "have not set uid"

 

test file1 -nt file2 : 判断file1 是否比file2 更新。

test sh02.sh -nt sh01.sh && echo "newer" || echo "not newer"    

test file1 -ot file2 :  判断file1 是否比file2 更旧。

 test sh01.sh -ot sh02.sh && echo "old" || echo "not old"

 

test file1 -ef  file2 :  判断file2 是否为file2 的硬链接。

 eg: test sh01.sh  -ef sh01-hard && echo "hardlink" || echo "not hardlink"

 

 两个整数之间的判断:

test   n1   -eq  n2

-eq: 两数值相等。(equal)  

-ne:两数值不等(not equal)  

-gt:n1 大于n2(greater than)  

-lt:n1 小于n2  (less than)  

-ge:n1 大于等于n2 (greater than or equal  )  

-le: n1小于等于n2  ( less than or equal  )  

 eg:

qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ n1=5
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ n2=6
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test "$n1" -eq "$n2" && echo "equal" || echo "not equal"

not equal

 

 

 判定字符串数据:

test -z string  判定字符串是否为0?若string为空字符串,则为true 

test -z string  判定字符串是否为非0?若string为空字符串,则为fasle,否则为ture。 -n可省略

 test  str1 str2  判定str1是否等于str2,若相等,则回传true(=两边需要空格)

 test  str1 !=  str2  判定str1是否不等于str2,若相等,则回传false

eg:

qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ str=""
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test -z "$str" && echo "is null" || echo "is not null"
is null
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test -n "$str" && echo "is not null" || echo "is null"
is null
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test "$str" && echo "is not null" || echo "is null"
is null
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ str="abc"
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test "$str" && echo "is not null" || echo "is null"
is not null
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test "$str" && echo "is not null" || echo "is null"
is not null
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ str1="avc"
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ str1="abc"
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test "$str" = "$str1" && echo "equal" ||echo "not equal"
equal
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ ^C
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test "$str" = "$str1" && echo "equal" ||echo "not equal"
equal
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test "$str" != "$str1" && echo "not equal" ||echo " equal"
equal

 

 

多重条件判定:例如: test  -r filename -a -x filename 

eg:

qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test -r sh01.sh -a -x sh01.sh && echo "readable and excutable" || echo "no"
readable and excutable

qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ chmod 444 sh03.sh
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test -r sh03.sh -a -x sh03.sh && echo "readable and excutable" || echo "no"
no

qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test -r sh03.sh -o -x sh03.sh && echo "readable or excutable" || echo "no"
readable or excutable

 

qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test ! -x sh03.sh && echo "is not excutable" || echo "excutable"
is not excutable
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test ! -r sh03.sh && echo "is not readable" || echo "readable"
readable
qinshizhi@holadev-system:/mnt/workdir/qinshizhi$ test ! -w sh03.sh && echo "is not writeable" || echo "writeable"
is not writeable

 

posted on 2018-08-28 15:28  CarryBricks  阅读(130)  评论(0编辑  收藏  举报