if test (表达式为真)
if test !表达式为假
test 表达式1 –a 表达式2 两个表达式都为真
test 表达式1 –o 表达式2 两个表达式有一个为真
2)判断字符串
test –n 字符串 字符串的长度非零
test –z 字符串 字符串的长度为零
test 字符串1=字符串2 字符串相等
test 字符串1!=字符串2 字符串不等
3)判断整数
test 整数1 –eq 整数2 整数相等
test 整数1 –ge 整数2 整数1大于等于整数2
test 整数1 –gt 整数2 整数1大于整数2
test 整数1 –le 整数2 整数1小于等于整数2
test 整数1 –lt 整数2 整数1小于整数2
test 整数1 –ne 整数2 整数1不等于整数2
4)判断文件
test File1 –ef File2 两个文件具有同样的设备号和i结点号
test File1 –nt File2 文件1比文件2 新
test File1 –ot File2 文件1比文件2 旧
test –b File 文件存在并且是块设备文件
test –c File 文件存在并且是字符设备文件
test –d File 文件存在并且是目录
test –e File 文件存在
test –f File 文件存在并且是正规文件
test –g File 文件存在并且是设置了组ID
test –G File 文件存在并且属于有效组ID
test –h File 文件存在并且是一个符号链接(同-L)
test –k File 文件存在并且设置了sticky位
test –b File 文件存在并且是块设备文件
test –L File 文件存在并且是一个符号链接(同-h)
test –o File 文件存在并且属于有效用户ID
test –p File 文件存在并且是一个命名管道
test –r File 文件存在并且可读
test –s File 文件存在并且是一个套接字
test –t FD 文件描述符是在一个终端打开的
test –u File 文件存在并且设置了它的set-user-id位
test –w File 文件存在并且可写
test –x File 文件存在并且可执行
Bash shell 在当今的许多 Linux® 和 UNIX® 系统上都可使用,是 Linux 上常见的默认 shell。Bash 包含强大的编程功能,其中包括丰富的可测试文件类型和属性的函数,以及在多数编程语言中可以使用的算术和字符串比较函数。理解不同的测试并认识到 shell 还能把一些操作符解释成 shell 元字符,是成为高级 shell 用户的重要一步。这篇文章摘自 developerWorks 教程 LPI 102 考试准备,主题 109: Shell、脚本、编程和编译,介绍了如何理解和使用 Bash shell 的测试和比较操作。
[ian@pinguino ~]$ test 3 -gt 4 && echo True || echo falsefalse[ian@pinguino ~]$ [ "abc" != "def" ];echo $?0[ian@pinguino ~]$ test -d “$HOME” ;echo $?0
[ian@pinguino ~]$ test “abc” = “def” ;echo $?1[ian@pinguino ~]$ [ "abc" != "def" ];echo $?0[ian@pinguino ~]$ [ "abc" \< "def" ];echo $?0[ian@pinguino ~]$ [ "abc" \> "def" ];echo $?1[ian@pinguino ~]$ [ "abc" \<"abc" ];echo $?1[ian@pinguino ~]$ [ "abc" \> "abc" ];echo $?1
-d 目录
-e 存在(也可以用 -a)
-f 普通文件
-h 符号连接(也可以用 -L)
-p 命名管道
-r 可读
-s 非空
-S 套接字
-w 可写
-N 从上次读取之后已经做过修改
-nt 测试 file1 是否比 file2 更新。修改日期将用于这次和下次比较。
-ot 测试 file1 是否比 file2 旧。
-ef 测试 file1 是不是 file2 的硬链接。
[ian@pinguino ~]$ set +o nounset[ian@pinguino ~]$ [ -o nounset ];echo $?1[ian@pinguino ~]$ set -u[ian@pinguino ~]$ test -o nounset; echo $?0
[ian@pinguino ~]$ test “a” != “$HOME” -a 3 -ge 4 ; echo $?1[ian@pinguino ~]$ [ ! \( "a" = "$HOME" -o 3 -lt 4 \) ]; echo $?1[ian@pinguino ~]$ [ ! \( "a" = "$HOME" -o '(' 3 -lt 4 ')' ")" ]; echo $?1
[ian@pinguino ~]$ let x=2 y=2**3 z=y*3;echo $? $x $y $z0 2 8 24[ian@pinguino ~]$ (( w=(y/x) + ( (~ ++x) & 0x0f ) )); echo $? $x $y $w0 3 8 16[ian@pinguino ~]$ (( w=(y/x) + ( (~ ++x) & 0x0f ) )); echo $? $x $y $w0 4 8 13
[ian@pinguino ~]$ [[ ( -d "$HOME" ) && ( -w "$HOME" ) ]] && > echo “home is a writable directory”home is a writable directory
[ian@pinguino ~]$ [[ "abc def .d,x--" == a[abc]*\ ?d* ]]; echo $?0[ian@pinguino ~]$ [[ "abc def c" == a[abc]*\ ?d* ]]; echo $?1[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]*\ ?d* ]]; echo $?1
[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]*\ ?d* || (( 3 > 2 )) ]]; echo $?0[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]*\ ?d* || 3 -gt 2 ]]; echo $?0[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]*\ ?d* || 3 > 2 ]]; echo $?0[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]*\ ?d* || a > 2 ]]; echo $?0[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]*\ ?d* || a -gt 2 ]]; echo $?-bash: a: unbound variable
[ian@pinguino ~]$ function mycalc ()> {> local x> if [ $# -lt 1 ]; then> echo “This function evaluates arithmetic for you if you give it some”> elif (( $* )); then> let x=”$*”> echo “$* = $x”> else> echo “$* = 0 or is not an arithmetic expression”> fi> }[ian@pinguino ~]$ mycalc 3 + 43 + 4 = 7[ian@pinguino ~]$ mycalc 3 + 4**33 + 4**3 = 67[ian@pinguino ~]$ mycalc 3 + (4**3 /2)-bash: syntax error near unexpected token `(‘[ian@pinguino ~]$ mycalc 3 + “(4**3 /2)”3 + (4**3 /2) = 35[ian@pinguino ~]$ mycalc xyzxyz = 0 or is not an arithmetic expression[ian@pinguino ~]$ mycalc xyz + 3 + “(4**3 /2)” + abcxyz + 3 + (4**3 /2) + abc = 35
[[ ! ("$*" == *[a-zA-Z]* ]]
,或使用适合自己范围的形式)消除包含字母表字符的表达式,但是这会妨碍在输入中使用 16 进制标记,因为使用 16 进制标记时可能要用 0x0f 表示 15。实际上,shell 允许的基数最高为 64(使用 base#value 标记),所以可以在输入中加入 _ 和 @ 合法地使用任何字母表字符。8 进制和 16 进制使用常用的标记方式,开头为 0 表示八进制,开头为 0x 或 0X 表示 16 进制。清单 10 显示了一些示例。
[ian@pinguino ~]$ mycalc 015015 = 13[ian@pinguino ~]$ mycalc 0xff0xff = 255[ian@pinguino ~]$ mycalc 29#3729#37 = 94[ian@pinguino ~]$ mycalc 64#1az64#1az = 4771[ian@pinguino ~]$ mycalc 64#1azA64#1azA = 305380[ian@pinguino ~]$ mycalc 64#1azA_@64#1azA_@ = 1250840574[ian@pinguino ~]$ mycalc 64#1az*64**3 + 64#A_@64#1az*64**3 + 64#A_@ = 1250840574
[ian@pinguino ~]$ type mycalcmycalc is a functionmycalc (){ local x; if [ $# -lt 1 ]; then echo “This function evaluates arithmetic for you if you give it some”; else if (( $* )); then let x=”$*”; echo “$* = $x”; else echo “$* = 0 or is not an arithmetic expression”; fi; fi}
原创文章,转载请注明: 转载自PT Ubuntu Blog