Shell script的语法二:判断命令

来源:互联网 发布:新网域名转出密码 编辑:程序博客网 时间:2024/05/09 06:11

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)-gtn1 大于 n2 (greater than)-ltn1 小于 n2 (less than)-gen1 大于等于 n2 (greater than or equal)-len1 小于等于 n2 (less than or equal)

5、test对字符串进行判断的参数

test -z string判断字符串是否为 null ?若 string 为空字符串,则返回 truetest -n string判断字符串是否不为 null ?若 string 为空字符串,则返回 false。注意: -n 可省略test str1 == str2判断 str1 是否等于 str2 ,若相等,则返回 truetest 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 releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATH#1.让用户输入文件名,并判断用户输入的文件名是否存在?echo "Please input a filename,I will check the filename's type and permission.\n\n"read -p "Input a filename:" filenametest -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.shPlease input a filename,I will check the filename's type and permission.Input a filename:shuThe 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 releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport 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 0echo "I don't know what your choice is" && exit 0
运行结果:

root@Test:~/svn/public$ chmod a+x ans_yn.sh;./ans_yn.shPlease input (Y/N):yOK, continue





0 0