shell 文件状态测试
来源:互联网 发布:js splice方法 w3c 编辑:程序博客网 时间:2024/06/06 05:01
shell 文件状态测试
http://blog.chinaunix.net/u1/56723/showart_498158.html
shell 文件状态测试
文件状态测试
-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 file2 : file1 比file2 新时返回真
file1 -ot file2 : file1 比file2 旧时返回真
举例: if [ -b /dev/hda ] ;then echo "yes" ;else echo "no";fi // 将打印 yes
test -c /dev/hda ; echo $? // 将打印 1 表示test 命令的返回值为1,/dev/hda 不是字符设备
[ -w /etc/passwd ]; echo $? // 查看对当前用户而言,passwd 文件是否可写
测试时逻辑操作符
-a 逻辑与,操作符两边均为真,结果为真,否则为假。
-o 逻辑或,操作符两边一边为真,结果为真,否则为假。
! 逻辑否,条件为假,结果为真。
举例: [ -w result.txt -a -w score.txt ] ;echo $? // 测试两个文件是否均可写
常见字符串测试
-z string : 字符串string 为空串(长度为0)时返回真
-n string : 字符串string 为非空串时返回真
str1 = str2 : 字符串str1 和字符串str2 相等时返回真
str1 != str2 : 字符串str1 和字符串str2 不相等时返回真
str1 < str2 : 按字典顺序排序,字符串str1 在字符串str2 之前
str1 > str2 : 按字典顺序排序,字符串str1 在字符串str2 之后
举例: name="zqf"; [ $name = "zqf" ];echo $? // 打印 0 表示变量name 的值和字符串"zqf"相等
常见数值测试
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,则返回真
举例: x=1 ; [ $x -eq 1 ] ; echo $? // 将打印 0 表示变量x 的值等于数字1
x=a ; [ $x -eq "1" ] // shell 打印错误信息 [: a: integer expression expected
转贴自:http://blog.chinaunix.net/u/21352/showart_135230.html
- shell 文件状态测试
- shell测试文件状态
- linux shell测试文件状态
- shell 文件测试操作
- shell文件测试
- Shell 学习(五、数值,字符串,文件状态测试,((..))和[[..]]的使用)
- linux shell 状态测试表达式含义
- shell编程测试文件权限
- shell文件测试 参数-N
- shell下的文件测试操作命令
- shell下的文件测试操作命令
- shell编程常用的文件测试选项
- shell中test文件测试指令
- Shell文件测试运算符列表
- shell脚本格式问题导致读取不到文件状态
- shell测试
- shell 测试
- shell测试
- 异常详细信息: System.Data.OleDb.OleDbException: 未指定的错误 (asp.net C#)
- 一道算法题:(数据分析)
- VC怎样把一副自己画的图存成BMP格式和在VC++下实现高彩色工具条
- json简介
- 安装android SDK2.0及创建2.0模拟器的方法
- shell 文件状态测试
- VC++在MFC程序中如何注册/注销ActiveX控件(.OCX)
- 雅虎CEO巴茨明年辞职 杨致远再次复出
- 加载外部sound之监听加载事件
- 如何递归的建立文件夹(ZZ)
- 音乐循环播放器的PCB设计
- 初学DataGrid------使用DataGrid中删除某数据如何出现提示信息
- Sharepoint 2010
- 在Fedora下安装PPStream网络电视