BASH TEST 测试操作

来源:互联网 发布:学外贸英语的软件 编辑:程序博客网 时间:2024/05/06 20:27

NAME
       test - check file types and compare values

SYNOPSIS
       test EXPRESSION
       test

       [ EXPRESSION ]
       [ ]
       [ OPTION

DESCRIPTION
       Exit with the status determined by EXPRESSION.

       --help display this help and exit

       --version
              output version information and exit

       An omitted EXPRESSION defaults to false.  Otherwise, EXPRESSION is true
       or false and sets exit status.  It is one of:

       ( EXPRESSION )   #EXPRESSION is true

       ! EXPRESSION  # EXPRESSION is false

       EXPRESSION1 -a EXPRESSION2
              both EXPRESSION1 and EXPRESSION2 are true

       EXPRESSION1 -o EXPRESSION2
              either EXPRESSION1 or EXPRESSION2 is true

       -n STRING     the length of STRING is nonzero


       -z STRING       the length of STRING is zero

       STRING1 = STRING2       the strings are equal

       STRING1 != STRING2     the strings are not equal

       INTEGER1 -eq INTEGER2    INTEGER1 is equal to INTEGER2

       INTEGER1 -ge INTEGER2     INTEGER1 is greater than or equal to INTEGER2

       INTEGER1 -gt INTEGER2      INTEGER1 is greater than INTEGER2

       INTEGER1 -le INTEGER2      INTEGER1 is less than or equal to INTEGER2

       INTEGER1 -lt INTEGER2      INTEGER1 is less than INTEGER2

       INTEGER1 -ne INTEGER2      INTEGER1 is not equal to INTEGER2

       FILE1 -ef FILE2      FILE1 and FILE2 have the same device and inode numbers

       FILE1 -nt FILE2      FILE1 is newer (modification date) than FILE2

       FILE1 -ot FILE2       FILE1 is older than FILE2

       -b FILE       FILE exists and is block special

       -c FILE      FILE exists and is character special

       -d FILE       FILE exists and is a directory

       -e FILE       FILE exists

       -f FILE       FILE exists and is a regular file

       -g FILE       FILE exists and is set-group-ID

       -G FILE       FILE exists and is owned by the effective group ID

       -h FILE      FILE exists and is a symbolic link (same as -L)

       -k FILE       FILE exists and has its sticky bit set

       -L FILE       FILE exists and is a symbolic link (same as -h)

       -O FILE       FILE exists and is owned by the effective user ID

       -p FILE        FILE exists and is a named pipe

       -r FILE         FILE exists and read permission is granted


       -s FILE       FILE exists and has a size greater than zero

       -S FILE        FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE       FILE exists and its set-user-ID bit is set

       -w FILE       FILE exists and write permission is granted

       -x FILE       FILE exists and execute (or search) permission is granted

       Except for -h and  -L,  all  FILE-related  tests  dereference  symbolic   links.   Beware  that  parentheses  need  to be escaped (e.g., by back-
       slashes) for shells.  INTEGER may also be -l STRING, which evaluates to   the length of STRING.

       NOTE:  [  honors  the  --help and --version options, but test does not.   test treats each of those as it treats any other nonempty STRING.

       NOTE: your shell may have its own version of test and/or [, which  usually  supersedes  the  version  described  here.   Please refer to your
       shell's documentation for details about the options it supports.



/span>7.2 文件测试操作
----------------
返回 true 如果...
-e 文件存在
-a 文件存在
这个选项的效果与-e 相同.但是它已经被弃用了,并且不鼓励使用
-f file 是一个 regular 文件(不是目录或者设备文件)
-s 文件长度不为 0
-d 文件是个目录
-b 文件是个块设备(软盘,cdrom 等等)
-c 文件是个字符设备(键盘,modem,声卡等等)
-p 文件是个管道
-h 文件是个符号链接
-L 文件是个符号链接
-S 文件是个 socket
-t 关联到一个终端设备的文件描述符
这个选项一般都用来检测是否在一个给定脚本中的 stdin[-t0]或[-t1]是一个终端
-r 文件具有读权限(对于用户运行这个 test)
-w 文件具有写权限(对于用户运行这个 test)
-x 文件具有执行权限(对于用户运行这个 test)


-size:11pt;color:rgb(0,0,0);font-style-g set-group-id(sgid)标志到文件或目录上
如果一个目录具有 sgid 标志,那么一个被创建在这个目录里的文件,这个目录属于创建
这个目录的用户组,并不一定与创建这个文件的用户的组相同.对于 workgroup 的目录
共享来说,这非常有用.见<<UNIX 环境高级编程中文版>>第 58 页.
-u set-user-id(suid)标志到文件上
如果运行一个具有 root 权限的文件,那么运行进程将取得 root 权限,即使你是一个普通
用户.[1]这对于需要存取系统硬件的执行操作(比如 pppd 和 cdrecord)非常有用.如果
没有 suid 标志的话,那么普通用户(没有 root 权限)将无法运行这种程序.
见<<UNIX 环境高级编程中文版>>第 58 页.
-rwsr-xr-t 1 root 178236 Oct 2 2000 /usr/sbin/pppd
对于设置了 suid 的文件,在它的权限标志中有"s".
-k 设置粘贴位,见<<UNIX 环境高级编程中文版>>第 65 页.
对于"sticky bit",save-text-mode 标志是一个文件权限的特殊类型.如果设置了这
个标志,那么这个文件将被保存在交换区,为了达到快速存取的目的.如果设置在目录
中,它将限制写权限.对于设置了 sticky bit 位的文件或目录,权限标志中有"t".
drwxrwxrwt 7 root 1024 May 19 21:26 tmp/
如果一个用户并不时具有 stick bit 位的目录的拥有者,但是具有写权限,那么用户只
能在这个目录下删除自己所拥有的文件.这将防止用户在一个公开的目录中不慎覆盖
或者删除别人的文件,比如/tmp(当然 root 或者是目录的所有者可以随便删除或重命名
其中的文件).


-webkit-text-stroke-width: 0px; ">f1 -nt f2
文件 f1 比 f2 新
f1 -ot f2
f1 比 f2 老
f1 -ef f2
f1 和 f2 都硬连接到同一个文件.
! 非--反转上边测试的结果(如果条件缺席,将返回 true)


二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别.
整数比较
-eq 等于,如:if [ "$a" -eq "$b" ]
-ne 不等于,如:if [ "$a" -ne "$b" ]
-gt 大于,如:if [ "$a" -gt "$b" ]
-ge 大于等于,如:if [ "$a" -ge "$b" ]
-lt 小于,如:if [ "$a" -lt "$b" ]
-le 小于等于,如:if [ "$a" -le "$b" ]
< 小于(需要双括号),如:(("$a" < "$b"))
<= 小于等于(需要双括号),如:(("$a" <= "$b"))
> 大于(需要双括号),如:(("$a" > "$b"))
>= 大于等于(需要双括号),如:(("$a" >= "$b"))
字符串比较
= 等于,如:if [ "$a" = "$b" ]
== 等于,如:if [ "$a" == "$b" ],与=等价
注意:==的功能在[[]]和[]中的行为是不同的,如下:
1 [[ $a == z* ]] # 如果$a 以"z"开头(模式匹配)那么将为 true
2 [[ $a == "z*" ]] # 如果$a 等于 z*(字符匹配),那么结果为 true
3
4 [ $a == z* ] # File globbing 和 word splitting 将会发生
5 [ "$a" == "z*" ] # 如果$a 等于 z*(字符匹配),那么结果为 true
一点解释,关于 File globbing 是一种关于文件的速记法,比如"*.c"就是,再如~也是.
但是 file globbing 并不是严格的正则表达式,虽然绝大多数情况下结构比较像.
!= 不等于,如:if [ "$a" != "$b" ]


Version:0.9StartHTML:00000097EndHTML:00031529StartFragment:00000135EndFragment:00031491这个操作符将在[[]]结构中使用模式匹配.
< 小于,在 ASCII 字母顺序下.如:
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
注意:在[]结构中"<"需要被转义.
> 大于,在 ASCII 字母顺序下.如:
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
注意:在[]结构中">"需要被转义.
具体参考 Example 26-11 来查看这个操作符应用的例子.
-z 字符串为"null".就是长度为 0.
-n 字符串不为"null"
注意:
使用-n 在[]结构中测试必须要用""把变量引起来.使用一个未被""的字符串来使用! -z
或者就是未用""引用的字符串本身,放到[]结构中(见 Example 7-6)虽然一般情况下可
以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯.[1]
Example 7-5 数字和字符串比较
################################Start
Script#######################################
1 #!/bin/bash
2
3 a=4
4 b=5
5
6 # 这里的变量 a 和 b 既可以当作整型也可以当作是字符串.
7 # 这里在算术比较和字符串比较之间有些混淆,
8 #+ 因为 Bash 变量并不是强类型的.
9
10 # Bash 允许对整型变量操作和比较
11 #+ 当然变量中只包含数字字符.
12 # 但是还是要考虑清楚再做.
13
14 echo
15
16 if [ "$a" -ne "$b" ]
17 then
18 echo "$a is not equal to $b"
19 echo "(arithmetic comparison)"
20 fi
21
22 echo
23
24 if [ "$a" != "$b" ]
25 then
26 echo "$a is not equal to $b."
27 echo "(string comparison)"
28 # "4" != "5"
29 # ASCII 52 != ASCII 53
30 fi
31
32 # 在这个特定的例子中,"-ne"和"!="都可以.
33
34 echo
35
36 exit 0
################################End
Script#########################################
Example 7-6 测试字符串是否为 null
################################Start
Script#######################################
1 #!/bin/bash
2 # str-test.sh: 测试 null 字符串和非引用字符串,
3 #+ but not strings and sealing wax, not to mention cabbages and kings . . .
4 #+ 上边这句没看懂
5 # Using if [ ... ]
6
7
8 # 如果一个字符串没被初始化,那么它就没有定义的值(像这种话,总感觉像屁话)
9 # 这种状态叫做"null"(与 zero 不同)
10
11 if [ -n $string1 ] # $string1 没被声明和初始化
12 then
13 echo "String \"string1\" is not null."
14 else
15 echo "String \"string1\" is null."
16 fi
17 # 错误的结果.
18 # 显示$string1 为非空,虽然他没被初始化.
19
20
21 echo
22
23
24 # 让我们再试一下.
25
26 if [ -n "$string1" ] # 这次$string1 被引用了.
27 then
28 echo "String \"string1\" is not null."
29 else
30 echo "String \"string1\" is null."
31 fi # ""的字符串在[]结构中
32
33
34 echo
35
36
37 if [ $string1 ] # 这次$string1 变成"裸体"的了
38 then
39 echo "String \"string1\" is not null."
40 else
41 echo "String \"string1\" is null."
42 fi
43 # 这工作得很好.
44 # 这个[]test 操作检测 string 是否为 null.
45 # 然而,使用("$string1")是一种很好的习惯
46 #
47 # As Stephane Chazelas points out,
48 # if [ $string1 ] 有 1 个参数 "]"
49 # if [ "$string1" ] 有 2 个参数,空的"$string1"和"]"
50
51
52
53 echo
54
55
56
57 string1=initialized
58
59 if [ $string1 ] # 再来,$string1"裸体了"
60 then
61 echo "String \"string1\" is not null."
62 else
63 echo "String \"string1\" is null."
64 fi
65 # 再来,给出了正确的结果.
66 # 不过怎么说("$string1")还是好很多,因为. . .
67
68
69 string1="a = b"
70
71 if [ $string1 ] # 再来,$string1 再次裸体了.
72 then
73 echo "String \"string1\" is not null."
74 else
75 echo "String \"string1\" is null."
76 fi
77 # 非引用的"$string1"现在给出了一个错误的结果!
78
79 exit 0
80 # Thank you, also, Florian Wisser, for the "heads-up".
################################End
Script#########################################
Example 7-7 zmore
################################Start
Script#######################################
1 #!/bin/bash
2 # zmore
3
4 #使用'more'来查看 gzip 文件
5
6 NOARGS=65
7 NOTFOUND=66
8 NOTGZIP=67
9
10 if [ $# -eq 0 ] # 与 if [ -z "$1" ]同样的效果
11 # 应该是说前边的那句注释有问题,$1 是可以存在的,比如:zmore "" arg2 arg3
12 then
13 echo "Usage: `basename $0` filename" >&2
14 # 错误消息到 stderr
15 exit $NOARGS
16 # 脚本返回 65 作为退出码.
17 fi
18
19 filename=$1
20
21 if [ ! -f "$filename" ] # 将$filename ""起来,来允许可能的空白
22 then
23 echo "File $filename not found!" >&2
24 # 错误消息到 stderr
25 exit $NOTFOUND
26 fi
27
28 if [ ${filename##*.} != "gz" ]
29 # 在变量替换中使用中括号
30 then
31 echo "File $1 is not a gzipped file!"
32 exit $NOTGZIP
33 fi
34
35 zcat $1 | more
36
37 # 使用过滤命令'more'
38 # 如果你想的话也可使用'less'
39
40
41 exit $? # 脚本将返回 pipe 的结果作为退出码
42 # 事实上,不用非的有"exit $?",但是不管怎么说,有了这句,能正规一些
43 # 将最后一句命令的执行状态作为退出码返回
################################End
Script#########################################
混合比较
-a 逻辑与
exp1 -a exp2 如果 exp1 和 exp2 都为 true 的话,这个表达式将返回 true
-o 逻辑或
exp1 -o exp2 如果 exp1 和 exp2 中有一个为 true 的话,那么这个表达式就返回 true
这与 Bash 的比较操作符&&和|| 很相像.在[[]]中使用它.
1 [[ condition1 && condition2 ]]
-o 和-a 一般都是和 test 命令或者是[]一起工作.
1 if [ "$exp1" -a "$exp2" ]请参考 Example 8-3,Example 26-16 和 Example A-28 来查看混合比较操作的行为.注意事项:[1] S.C.(这家伙是个人名)指出,在使用混合比较的时候即使"$var"也可能会产生问题.如果$string 为空的话,[ -n "$string" -o "$a" = "$b" ]可能在某些版本的 Bash 中会有问题.为了附加一个额外的字符到可能的空变量中的一种安全的办法是,[ "x$string" != x -o "x$a" = "x$b" ](the "x's" cancel out)(没看懂).cancel out 是抵消的意思.7.4 嵌套的 if/then 条件 test-------------------------可以使用 if/then 来进行嵌套的条件 test.最终的结果和上边的使用&&混合比较操作是相同的.1 if [ condition1 ]2 then3 if [ condition2 ]4 then5 do-something # 这里只有在 condition1 和 condition2 都可用的时候才行.6 fi7 fi具体请查看 Example 34-4.7.5 检查你的 test 知识--------------------系统范围的 xinitrc 文件可以用来启动 X server.这个文件中包含了相当多的 if/then test,就像下边的节选一样:1 if [ -f $HOME/.Xclients ]; then2 exec $HOME/.Xclients3 elif [ -f /etc/X11/xinit/Xclients ]; then4 exec /etc/X11/xinit/Xclients5 else6 # 故障保险设置,虽然我们永远都不会走到这来.7 # (我们在 Xclients 中也提供了相同的机制)它不会受伤的.8 xclock -geometry 100x100-5+5 &9 xterm -geometry 80x50-50+150 &10 if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; then11 netscape /usr/share/doc/HTML/index.html &12 fi13 fi对上边的"test"结构进行解释,然后检查整个文件,/etc/X11/xinit/xinitrc,并分析 if/thentest 结构.你可能需要查看一下后边才能讲解到的 grep,sed 和正则表达式的知识.


7.4 嵌套的 if/then 条件 test
-------------------------
可以使用 if/then 来进行嵌套的条件 test.最终的结果和上边的使用&&混合比较操作是相同的.


ight: normal; letter-spacing: normal; 1 if [ condition1 ]
2 then
3 if [ condition2 ]
4 then
5 do-something # 这里只有在 condition1 和 condition2 都可用的时候才行.
6 fi
7 fi
具体请查看 Example 34-4.
7.5 检查你的 test 知识
--------------------
系统范围的 xinitrc 文件可以用来启动 X server.这个文件中包含了相当多的 if/then test,
就像下边的节选一样:
1 if [ -f $HOME/.Xclients ]; then
2 exec $HOME/.Xclients
3 elif [ -f /etc/X11/xinit/Xclients ]; then
4 exec /etc/X11/xinit/Xclients
5 else
6 # 故障保险设置,虽然我们永远都不会走到这来.
7 # (我们在 Xclients 中也提供了相同的机制)它不会受伤的.
8 xclock -geometry 100x100-5+5 &
9 xterm -geometry 80x50-50+150 &
10 if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; then
11 netscape /usr/share/doc/HTML/index.html &
12 fi
13 fi
对上边的"test"结构进行解释,然后检查整个文件,/etc/X11/xinit/xinitrc,并分析 if/then
test 结构.你可能需要查看一下后边才能讲解到的 grep,sed 和正则表达式的知识.

0 0
原创粉丝点击