Shell scripts的判断命令有2种,一种是使用test命令,并搭配$? 或 && 及 || 展示判断的结果;第二种是使用 [ ]
一、test用法:
以下例子:
[root@Test ~bin]$ test -e /CS && echo "exist" || echo "Not exist"
Not exist #结果显示不存在
就是使用test命令判断/CS是否存在,并使用&&和||将显示结果打印到屏幕上。
1、test判断某个文件文件类型使用的参数表如下:
-e 判断文件是否存在?(常用)
-f 判断文件是否存在且为文件类型(file)?(常用)
-d 判断文件是否存在且为目录类型(directory)?(常用)
-b 判断文件是否存在且为一个 block device?
-c 判断文件是否存在且为一个 character device?
-S 判断文件是否存在且为一个 Socket 文件?
-p 判断文件是否存在且为一个FIFO (pipe)文件?
-L 判断文件是否存在且为一个链接文件?
2、test判断文件所属权限的使用参数如下:(如 test -r filename 表示判断文件是否可读)
-r 判断该文件是否存在且具有“ 可读 ”的权限?
-w 判断该文件是否存在且具有“ 可写 ”的权限?
-x 判断该文件是否存在且具有“ 可执行 ”的权限?
-u 判断该文件是否存在且具有“ SUID ”的属性?
-g 判断该文件是否存在且具有“ SGID ”的属性?
-k 判断该文件是否存在且具有“ Sticky bit ”的属性?
-s 判断该文件是否存在且为“ 非空白文件 ”?
3、test判断中用于比较2个文件的参数:
-nt (newer than)判断 file1 是否比 file2 新
-ot (older than)判断 file1 是否比 file2 旧
-ef 判断 file1 与 file2 是否为同一文件,可用在判断 hard link 中。 主要意义在判定两个文件是否均指向同一个 inode
4、test对2个整数进行判断的参数
-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)
5、test对字符串进行判断的参数
test -z string 判断字符串是否为 null ?若 string 为空字符串,则返回 true
test -n string 判断字符串是否不为 null ?若 string 为空字符串,则返回 false。注意: -n 可省略
test str1 == str2 判断 str1 是否等于 str2 ,若相等,则返回 true
test str1 != str2 判断 str1 是否不等于 str2 ,若相等,则返回 false
6、test的多条件判定
-a (and)两种情况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,返回 true。
-o (or)两种情况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 或 x 权限时,返回 true。
! 非,相反,如 test ! -x file ,当 file 不具有 x 时,返回 true
7、test的应用
如下实现用户输入文件名,并判断文件类型和执行用户所拥有该文件的权限
#!/bin/bash
#Program:
# User input a filename,program will check the flowing:
# 1)exist? 2)file/directory? 3)file permissions
#History:
#2017/01/10 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#1.让用户输入文件名,并判断用户输入的文件名是否存在?
echo "Please input a filename,I will check the filename's type and permission.\n\n"
read -p "Input a filename:" filename
test -z ${filename} && echo "You Must input a filename." && exit 0
#2.判断文件是否存在?若不存在显示信息并结束程序
test ! -e ${filename} && echo "The filename '${filename} DO NOT exist" && exit 0
#3.开始判断文件类型与属性
test -f ${filename} && filetype="regulare file"
test -d ${filename} && filetype="directory"
test -r ${filename} && perm="readable"
test -w ${filename} && perm="${perm} writable"
test -x ${filename} && perm="${perm} executable"
#4.输出文件类型和权限
echo "The filename:${filename} is a ${filetype} "
echo "And the premissions for you are:${perm}"
运行结果如下:
root@Test:~/bin$ sh file_perm.sh
Please input a filename,I will check the filename's type and permission.
Input a filename:shu
The filename:shu is a regulare file
And the premissions for you are:readable writable
二、 [ ] 的用法
除了书写格式不同,其他用法与test一致
1、书写格式
1)在中括号 [] 内的每个元素都需要空白键进行分隔;
2)在中括号内的变量,最好都以双引号括起來;
3)在中括号内的常量,最好都以单或双引号括起來。
2、应用举例如下:
#!/bin/bash
#Program:
# This program shows the user's choice
#History:
#2017/01/10 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#提示用户输入
read -p "Please input (Y/N):" yn
[ "${yn}" == "y" -o "${yn}" == "Y" ] && echo "OK, continue" && exit 0
[ "${yn}" == "n" -o "${yn}" == "N" ] && echo "Oh, interrupt" && exit 0
echo "I don't know what your choice is" && exit 0
运行结果:
root@Test:~/svn/public$ chmod a+x ans_yn.sh;./ans_yn.sh
Please input (Y/N):y
OK, continue