Bash中test命令的使用(bash命令详解)满满干货

随心笔谈11个月前发布 admin
93 0



这个命令在if条件句中用得很多。test命令后都会跟一个表达式,作为它的参数。它有两种写法:

test EXPRESSION
[ EXPRESSION ]

test的执行过程就是拿一个元素与另一个元素进行比较。在网络上找了一个很有意思的例子,用它来说明一下test命令的使用:

test 1 -eq 2 && echo “true” || echo “false”

1:是用来作比较的第一个参数-eq:这是具体的比较方法2:这是用来比较的第二个参数

如果比较的结果是true,打印true,否则打印false

我们可以通过$?拿到test的结果。如果表达式的值是false,则$?的值是1,否则就是0。

上面的语句与下同的表达是一样的:

[ 1 -eq 2 ] && echo “true” || echo “false”

整型相关的表达式用到的两个数据的比较方法如下:

-eq:等于 (equal to)-ne:等于 (not equal to)-gt:大于 (greater than)-ge:大于或等于(greater than or equal to)-lt:小于 (less than)-le:小于或等于(less than or equal to)

#!/usr/bin/env bash

test 1 -eq 2 && echo “true” || echo “false”
test 1 -ne 2 && echo “true” || echo “false”
test 1 -gt 2 && echo “true” || echo “false”
test 1 -ge 2 && echo “true” || echo “false”
test 1 -lt 2 && echo “true” || echo “false”
test 1 -le 2 && echo “true” || echo “false”

[ 1 -eq 2 ] && echo “true” || echo “false”
[ 1 -ne 2 ] && echo “true” || echo “false”
[ 1 -gt 2 ] && echo “true” || echo “false”
[ 1 -ge 2 ] && echo “true” || echo “false”
[ 1 -lt 2 ] && echo “true” || echo “false”
[ 1 -le 2 ] && echo “true” || echo “false”

shell提供了字符串比较相关的表达式:

-n <string>: 字符串长度不为零-z <string>: 字符串长度为零<string>: 字符串值非零,与 -n <string>等价<string1>=<string2>: 两个字符串是否相等<string1> !=<string2>: 两个字符串是否不相等

针对字符串,shell提供了这些方便使用的表达式。比如说:-n <string>这个表达式就是将字符串长度与0作比较。其他依次类推。

test -n string1 && echo “true” || echo “false”
test -z string1 && echo “true” || echo “false”
test string1 && echo “true” || echo “false”
test string1=string2 && echo “true” || echo “false”
test string1!=string2 && echo “true” || echo “false”

[ -n string1 ] && echo “true” || echo “false”
[ -z string1 ] && echo “true” || echo “false”
[ string1 ] && echo “true” || echo “false”
[ string1=string2 ] && echo “true” || echo “false”
[ string1!=string2 ] && echo “true” || echo “false”

shell也提供了与文件相关的比较表达式:

<file1> -ef <file2>: 两个文件是否有相似的device和inode编号(这些概念在Linux相关的知识可以了解到。)<file1> -nt <file2>:通过比较文件的修改日期,判断file1是否比file2要新。(nt :newer than)<file1> -ot <file2>:通过比较文件的修改日期,判断file1是否比file2要旧。(ot :older than)-e <file>:文件是否存在(exists)-f <file>:文件存在且是一个常规文件(file)-d <file>:文件存在且是一个目录(directory)-r <file>:文件存在且有读权限(read)-w <file>:文件存在且有写权限(write)-x <file>:文件存在且有执行权限 (execute)-s <file>:文件存在且文件大小大于0(size)-S <file>:文件存在且文件是一个socket-O <file>:文件存在且文件所有者是有效的用户ID(owner)-G <file>:文件存在且文件所有者是有效的用户组ID(group)-h <file>:文件存在且是一个符号连接文件(hard)-L <file>:文件存在且是一个符号连接文件(link)-b <file>:文件存在且是一个特殊块文件(block)-c <file>:文件存在且是一个特殊字符文件(character)

#!/usr/bin/env bash

test -e /bin/bash && echo $? || echo $?
test -f /bin/bash && echo $? || echo $?
test -d /bin/bash && echo $? || echo $?
test -r /bin/bash && echo $? || echo $?
test -w /bin/bash && echo $? || echo $?
test -x /bin/bash && echo $? || echo $?
test -s /bin/bash && echo $? || echo $?
test -S /bin/bash && echo $? || echo $?
test -O /bin/bash && echo $? || echo $?
test -G /bin/bash && echo $? || echo $?
test -h /bin/bash && echo $? || echo $?
test -L /bin/bash && echo $? || echo $?
test -b /bin/bash && echo $? || echo $?
test -c /bin/bash && echo $? || echo $?

#!/usr/bin/env bash

[ -e /bin/bash ] && echo $? || echo $?
[ -f /bin/bash ] && echo $? || echo $?
[ -d /bin/bash ] && echo $? || echo $?
[ -r /bin/bash ] && echo $? || echo $?
[ -w /bin/bash ] && echo $? || echo $?
[ -x /bin/bash ] && echo $? || echo $?
[ -s /bin/bash ] && echo $? || echo $?
[ -S /bin/bash ] && echo $? || echo $?
[ -O /bin/bash ] && echo $? || echo $?
[ -G /bin/bash ] && echo $? || echo $?
[ -h /bin/bash ] && echo $? || echo $?
[ -L /bin/bash ] && echo $? || echo $?
[ -b /bin/bash ] && echo $? || echo $?
[ -c /bin/bash ] && echo $? || echo $?

shell提供了上面这些方便的表达式,我们就少做了很多功夫。

所以,现在看来test很简单,但是很有用。因为shell脚本里会出现很多条件语句,test会用到很多。

到此这篇关于Bash中test命令的使用的文章就介绍到这了,更多相关Bash test命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:Linux shell中的test命令用法教程Shell脚本test命令使用总结和实例

© 版权声明

相关文章