zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    简单计算器(栈和队列的应用)
    01背包问题(dfs+剪枝)
    PAT1057 stack(分块思想)
    算法笔记求序列A每个元素左边比它小的数的个数(树状数组和离散化)
    求序列A中每个数的左边比它小的数的个数(树状数组)
    ubuntu-创建python虚拟环境
    vue-打包
    django-项目目录结构
    接口自动化-常见工具
    selenium-封装一个browser
  • 原文地址:https://www.cnblogs.com/bigbean/p/3661553.html
Copyright © 2011-2022 走看看