linux shell 条件测试
来源:互联网 发布:风云遥感数据服务网 编辑:程序博客网 时间:2024/05/05 01:46
1.文件条件测试
-r 可读
-w 可写
-x 可执行
-d 目录
-f 一般文件
-b 块设备
-c 字符设备
-u 文件suid
-L 符号链接
-s 文件大于0,为非空
-e 文件是否存在
2.字符串条件测试
= 两个字符串相同
!= 两个字符串不相同
-z 为空串
-n 非空串
3.数值条件测试
-eq 相等
-ne 不相等
-gt 大于
-ge 大于或等于
-lt 小于
-le 小于或等于
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
文件状态测试
-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 f2files f1 and f2 are hard links to the same file举例: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按字典顺序排序,字符串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,则返回真在 (()) 中的测试:
<小于(在双括号里使用)(("$a" < "$b"))<=小于等于 (在双括号里使用)(("$a" <= "$b"))>大于 (在双括号里使用)(("$a" > "$b"))>=大于等于(在双括号里使用)(("$a" >= "$b"))举例: x=1 ; [ $x -eq 1 ] ; echo $? // 将打印 0 表示变量x 的值等于数字1x=a ; [ $x -eq "1" ] // shell 打印错误信息 [: a: integer expression expected
test , [] , [[]]
因为 shell 和我们通常编程语言不同,更多的情况是和它交互,总是调用别人。所以有些本属于程序语言本身的概念在 shell 中会难以理解。"基本功" 不好,更容易 "犯困" 了,我就是一个 :-) 。
以 bash 为例 (其他兼容 shell 差不多):
- test 和 [ 是 bash 的内部命令,GNU/linux 系统的 coreutils 软件包通常也带 /usr/bin/test 和 /usr/bin/[ 命令。如果我们不用绝对路径指明,通常我们用的都是 bash 自带的命令。
- [[ 是 bash 程序语言的关键字!
$ ls -l /usr/bin/[ /usr/bin/test
-rwxr-xr-x 1 root root 37400 9月 18 15:25 /usr/bin/[
-rwxr-xr-x 1 root root 33920 9月 18 15:25 /usr/bin/test
$ type [ [[ test
[ is a shell builtin
[[ is a shell keyword
test is a shell builtin
绝大多数情况下,这个三个功能通用。但是命令和关键字总是有区别的。命令和关键字的差别有多大呢?
如果是命令,它就和参数组合为一体被 shell 解释,那样比如 ">" "<" 就被shell 解释为重定向符号了。关键字却不这样。
在 [[ 中使用 && 和 ||
[ 中使用 -a 和 -o 表示逻辑与和逻辑或。
[[ 中可以使用通配符
arch=i486
[[ $arch = i*86 ]] && echo "arch is x86!"
[[ 中匹配字符串或通配符,不需要引号
[[ $arch_com = i386 || $ARCH = i*86 ]] &&
cat >> $TFS_REPO <<EOF
[tfs-i386]
name=GTES11.3 prelim1
baseurl=${BASEURL}i386/
enabled=1
EOF
- linux shell 测试条件
- linux shell 条件测试
- linux之shell 条件测试
- Linux之shell编程-条件测试
- Linux 与 Shell编程(2)-条件测试
- linux shell编程3 - 条件测试
- (6)Linux ----- Shell编程条件测试 ---- ④
- linux shell编程(二) 条件测试
- shell中的条件测试
- shell条件测试
- shell之条件测试
- shell条件测试
- shell条件测试
- SHELL中的测试条件
- shell条件测试
- shell条件测试
- Shell条件测试
- Shell 条件测试
- ubuntu下安装wine
- ICE入门实例──ping和获取远程时间
- ICE第三方包简介及安装&ICE安装(linux)
- ICE协议摘要
- ICE集群──负载均衡及容错
- linux shell 条件测试
- shell正则表达式实例解析
- Grep正则表达式
- shell函数的调用执行
- 三十分钟掌握STL(转帖)
- ubuntu下安装virtualbox 错误及解决办法
- STM32F407的时钟配置(system_config.c浅见)
- Ubuntu安装VirtualBox虚拟机
- windows 版Tomcat 7.0的配置