shell test command 列表
2007-03-07 14:44
323 查看
用shell编一些小程序时老是会忘记这些test命令,终于下决心把它整理出来:)
1. 数学表达式比较
-eq 相当于 ==
-ne 相当于 != not equal
-lt 相当于 less than <
-le 相当于 less than or equal <=
-gt 相当于 greater than >
-ge 相当于 greater than or equal >=
2. 字符串比较
1) 相同
if [ str1 = str2 ]; then
fi
2)不同
if [ str1 != str2 ]; then
fi
3)字符串str1 定义了且不为NULL (原文是string1 is NOT NULL or not defined , 但测试结果好象不同)
if [ str1 ]; then
fi
4)这个的原文是string1 is NOT NULL and does exist, 但测试结果是不管str1是否定义以及是否为空,该条件都成立
if [ -n str1 ]; then
fi
5)这个原文是string1 is NULL and does exist, 但测试结果是定义了为NULL,或者未定义,该条件都成立
if [ -z str1 ]; then
fi
3. 文件属性判断
-s 文件不为空
-f 是文件而不是directory
-d 是directory
-w 可以写
-r 是只读文件
-x 是可执行文件
4. 逻辑操作比较
! 相当于not such as: if [ ! 5 -gt 6 ]; then ....
-a and if [ 5 -gt 6 -a 5 -gt 7 ]; then...
-o or as above......
5. 其它
还可以测试一个命令的返回是否为真,例如:
if rm $1
then
fi
测试rm 命令的返回码.
1. 数学表达式比较
-eq 相当于 ==
-ne 相当于 != not equal
-lt 相当于 less than <
-le 相当于 less than or equal <=
-gt 相当于 greater than >
-ge 相当于 greater than or equal >=
2. 字符串比较
1) 相同
if [ str1 = str2 ]; then
fi
2)不同
if [ str1 != str2 ]; then
fi
3)字符串str1 定义了且不为NULL (原文是string1 is NOT NULL or not defined , 但测试结果好象不同)
if [ str1 ]; then
fi
4)这个的原文是string1 is NOT NULL and does exist, 但测试结果是不管str1是否定义以及是否为空,该条件都成立
if [ -n str1 ]; then
fi
5)这个原文是string1 is NULL and does exist, 但测试结果是定义了为NULL,或者未定义,该条件都成立
if [ -z str1 ]; then
fi
3. 文件属性判断
-s 文件不为空
-f 是文件而不是directory
-d 是directory
-w 可以写
-r 是只读文件
-x 是可执行文件
4. 逻辑操作比较
! 相当于not such as: if [ ! 5 -gt 6 ]; then ....
-a and if [ 5 -gt 6 -a 5 -gt 7 ]; then...
-o or as above......
5. 其它
还可以测试一个命令的返回是否为真,例如:
if rm $1
then
fi
测试rm 命令的返回码.
相关文章推荐
- Jenkins可用环境变量列表以及环境变量的使用(Shell/Command/Maven/Ant)
- shell出错test_path: command not found
- UNIX and Linux Shell Script Test Command
- Most Basic Shell Command for Linux(Ubuntu)
- shell数组、文件列表处理和后几行删除汇总
- Shell: test
- shell special characters within shell command lines
- Linux手机DIY.Shell应用扩展二.自动生成播放列表(E680系列)
- shell语法中test命令详解
- 关于ansbile工具的shell、command、script、raw模块的区别和使用场景
- shell 笔记 test命令
- 老大让我学Linux之test指令,expr指令,Shell特殊变量
- Shell test命令
- The Linux Command Line 读书笔记(一)初识shell
- Shell if else test 语句
- 编写shell脚本处理test文件
- Hadoop Shell Command
- mysql的command line shell的问题
- [chromium][browser][idea][closed] 如何把Chromium_testshell导到 eclipse工程中
- Shell文件测试运算符列表