shell测试选项
来源:互联网 发布:nextdate函数java代码 编辑:程序博客网 时间:2024/05/17 23:17
1、测试文件状态
test命令一般有两种格式
test condition或
[condition]
注意:使用"[]"时,要在条件和"[]"之间加上空格测试文件状态的条件表达式很多,最常用的有以下几个
-b file检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。-c file检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。-d file检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。-f file检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。-g file检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。-k file检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。-p file检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。-u file检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。-r file检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。-w file检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。-x file检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。-s file检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。-e file检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。
1、
例:
#!/bin/sh#测试test.txt文件的读权限,使用"[]"方法[ -r test.txt ]echo $?#测试test.txt文件的写权限,使用"[]"的方法[ -w test.txt ]echo $?#测试test.txt文件的执行权限,使用"[]"的方法[ -x test.txt ]echo $?#测试test.txt文件的读权限,使用"test命令"的方法test -r test.txtecho $?#测试test.txt文件的写权限,使用"test命令"的方法test -w test.txtecho $?#测试test.txt文件的执行权限,使用"test命令"的方法test -x test.txtecho $?exit 02、使用ls命令查看test.txt的权限信息
$ls -l test.txt
-rwxrw-r-- 1 linux-c linux-c 0 5 月 27 13:07 test.txt
3、在shell中运行该shell脚本如下
0
0
0
0
0
0
运行结果说明test.txt文件只有可读可写执行的权限。
2、测试时使用逻辑操作符与c语言类似,shell脚本中同样提供三种逻辑操作完成此功能。逻辑运算符通常和分支语句配合使用,实现程序执行流程的不同
1、-a逻辑与,两个操作数均为真,结果为真,否则为假。
2、-o逻辑或,两个操作数一个为真,结果为真,否则为假。
3、!逻辑非,条件为假,结果为真,否则为假。
例:
#!/bin/sh[ -r test.txt -a -r test2.txt ]echo $?[ -w test.txt -o -w test2.txt ]echo $?[ ! -x test2.txt ]echo $?$ ls -l
---------- 1 hjx hjx 0 9月 11 11:40 test2.txt
-rwxrwxrwx 1 hjx hjx 120 9月 11 11:54 test.sh
-rwxrw-r-- 1 hjx hjx 0 9月 11 11:32 test.txt
$ ./test.sh
1
0
0
逻辑运算符
以下介绍 Shell的逻辑运算符,假定变量 a 为 10,变量 b 为 20:
运算符说明举例
&& 逻辑的 AND[[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| 逻辑的 OR[[ $a -lt 100 || $b -gt 100 ]] 返回 true
#!/bin/sha=10b=20if [[ $a -lt 100 && $b -gt 100 ]]then echo "返回 true"else echo "返回 false"fiif [[ $a -lt 100 || $b -gt 100 ]]then echo "返回 true"else echo "返回 false"fi./test.sh
返回 false
返回 true
3、字符串测试
字符串测试有下5种格式
test "string"
test operator "string"
test "string1" operator "string2"
[operator "string"]
["string1" operator "string2"]
其中string1和string2代表需要测试的字符串,而operator代表字符串测试的操作符
这个操作共有以下4种可能的情况
1、=两个字符串相等
2、!= 两个字符串不等
3、-z 字符串是空串。
4、-n字符串是非空串。
例:
#!/bin/shstr1="hello"str2="hello"str3= #str3是一个空串#测试字符串str1和str2相等test $str1=$str2echo $?#测试字符串str3是空串test -z ${str3}echo $?#测试字符串str2是非空串test -n $str2echo $?echo "using []" #使用"[]进行测试数据测试#测试字符串str1和str2相等[ $str1=$str2 ]echo $?#测试字符串str3是空串[ -z $str3 ]echo $?#测试字符串str2是非空串[ -n $str2 ]echo $?#测试字符串str1和str2不相等[ $str1 != $str2 ]echo $?exit 0$ ./test.sh
0
0
0
using []
0
0
0
1
4、测试数值test "number" operator "number"
或者
["number" operator "number"]
其中number代表要测试的数据,而operater代表测试的操作符
注:数据两边的双引号是不能省略的。这代表将数据化为字符串进行测试
operator有6种情况,如下所示
1、-ep两个数值相等
2、-ne两个数值不相等
3、-gt第一个数大于第二个数
4、-lt第一个数小于第二个数
5、-le第一个数小于等于第二个数
6、-ge第一个数大于等于第二个数
例:
#!/bin/shtest "1" -eq "2"echo $?test "1" -ne "2"echo $?test "1" -gt "2"echo $?test "1" -ge "2"echo $?test "1" -lt "2"echo $?test "1" -le "2"echo $?echo [ "1" -eq "2" ]echo $?[ "1" -ne "2" ]echo $?[ "1" -gt "2" ]echo $?[ "1" -ge "2" ]echo $?[ "1" -lt "2" ]echo $?[ "1" -le "2" ]echo $?
例:
#!/bin/shnum1=100num2=100if test $[num1] -eq $[num2]then echo '两个数相等!'else echo '两个数不相等!'fi
例:
num1="ru1noob"num2="runoob"if test $num1 = $num2then echo '两个字符串相等!'else echo '两个字符串不相等!'fi例:
#!/bin/shif test -e ./bashthen echo '文件已存在!'else echo '文件不存在!'fi
阅读全文
0 0
- shell测试选项
- shell编程常用的文件测试选项
- shell选项
- shell 跟踪选项
- shell的设置选项
- shell中if选项
- Shell选项处理
- shell调试选项
- shell脚本常用选项
- shell grep常用选项
- Shell 变量和选项
- shell脚本 set 选项
- impala-shell命令选项
- impala-shell 命令行选项
- [Shell]命令行选项解析
- shell脚本获取选项
- shell测试
- shell 测试
- 欢迎使用CSDN-markdown编辑器
- MVC模式和MVP模式
- springboot-cloud-7-eureka-source
- Hadoop之MapReduce调度:通过Python进行多日期多链接地址的串行跑批统计
- Map<String,Object>转为JSONObject ,JSONObject 转为JsonObject,JsonObject转为JsonElement
- shell测试选项
- activeMq 持久化分类
- C++基础学习笔记:类与动态内存分配
- Android应用内直接打开高德或者百度地图
- 关于任何路径中存在空格的问题
- 系统任务状态转换框架
- 子类实现父类的抽象方法,父类调用抽象方法
- 程序员,如何从平庸走向理想?
- 【RF教程】robotframework的安装(三) ride