Shell 中的条件测试
来源:互联网 发布:拦截软件修改数据 编辑:程序博客网 时间:2024/05/16 05:57
shell提供了两种形式的条件测试 test 和 [ 都是shell的内置类型
root@localhost Shell]# type test
test is a shell builtin
[root@localhost Shell]# type [
[ is a shell builtin
[root@localhost Shell]#
1) 数字测试
int1 -eq int2如果int1 等于int2,则返回真int1 -ne int2如果int1 不等于int2,则返回真int1 -lt int2如果int1 小于int2,则返回真int1 -le int2如果int1 小于等于int2,则返回真int1 -gt int2如果int1 大于int2,则返回真int1 -ge int2如果int1 大于等于int2,则返回真也可以使用c语言风格的 (()) 测试:
<小于(在双括号里使用)(("$a" < "$b"))<=小于等于 (在双括号里使用)(("$a" <= "$b"))>大于 (在双括号里使用)(("$a" > "$b"))>=大于等于(在双括号里使用)(("$a" >= "$b"))2) 字符串测试3) 文件测试
-b filename当filename 存在并且是块文件时返回真(返回0)-c filename当filename 存在并且是字符文件时返回真-d pathname当pathname 存在并且是一个目录时返回真-e pathname当由pathname 指定的文件或目录存在时返回真-f filename当filename 存在并且是正规文件时返回真-g pathname当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真-h filename当filename 存在并且是符号链接文件时返回真 (或 -L filename)-k pathname当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真-p filename当filename 存在并且是命名管道时返回真-r pathname当由pathname 指定的文件或目录存在并且可读时返回真-s filename当filename 存在并且文件大小大于0 时返回真-S filename当filename 存在并且是socket 时返回真-t fd当fd 是与终端设备相关联的文件描述符时返回真-u pathname当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真-w pathname当由pathname 指定的文件或目录存在并且可写时返回真-x pathname当由pathname 指定的文件或目录存在并且可执行时返回真-O pathname当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)-G pathname当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真file1 -nt file2file1 比file2 新时返回真file1 -ot file2file1 比file2 旧时返回真f1 -ef f2f1和h2都是同一个文件的硬链接在[ 测试中可以使用-a -o ! 表示逻辑与,或,非
在[[ 中使用&&或者||
一个有意思的测试:
[root@localhost Shell]# if [ 0 ]
> then
> echo "0 is true"
> else
> echo "0 is false"
> fi
0 is true
[root@localhost Shell]#
[root@localhost Shell]# if [ 1 ] ; then echo "1 is true"; else echo "1 is false"; fi
1 is true
[root@localhost Shell]#
在shell中0代表true,非零代表false,为什么这里都是true呢
因为在条件测试中,0和1只是表达式,shell为解析表达式,这个表达式是一个单数字的表达式,shell会返回这个表达式的值为0和1 但是表达式自身正确,因此都返回0,(注意执行表达式返回值和表达式计算值的区别)
- shell中的条件测试
- SHELL中的测试条件
- shell中的条件测试
- Shell 中的条件测试
- Shell中的条件测试
- Shell中的条件测试语句
- shell编程中的条件测试
- shell条件测试
- shell之条件测试
- shell条件测试
- shell条件测试
- shell条件测试
- linux shell 测试条件
- shell条件测试
- Shell条件测试
- Shell 条件测试
- SHELL 编程 -- 条件测试
- shell条件测试
- TableView
- 网页打印,相当简单的一句话。
- linux 硬链接和软链接
- 将Controller中的数据传递到View中显示
- Reading record--TDD
- Shell 中的条件测试
- 寻找缺失的那个数
- Robbie的Surface初体验
- UVa 575 / ZOJ 1712 / Mid-Central USA 1997 Skew Binary (water ver.&斜二进制)
- 输入一个正整数,求取其平方根
- JVM系列一:JVM内存组成及分配
- 排序系列之四 计数排序
- JVM系列二:GC策略&内存申请、对象衰老
- 归并排序