(6)Linux ----- Shell编程条件测试 ---- ④
来源:互联网 发布:中海达v30数据怎么导出 编辑:程序博客网 时间:2024/05/18 17:02
--------------------------------------------------------
--------------------------------------------------------
条件测试
命令成功或失败,表达式为真或假
[root@p ~]# grep pan1 /etc/passwdpan1:x:501:502::/home/pan1:/bin/bash[root@p ~]# echo $?0[root@p ~]#
--------------------------------------------------------
--------------------------------------------------------
[root@p ~]# x=5;y=10[root@p ~]# test $x -gt $y[root@p ~]# echo $?1[root@p ~]#
[root@panjinming ~]# x=5;y=10
You have new mail in /var/spool/mail/root
[root@panjinming ~]# [$x -gt $y]
-bash: [5: command not found
[root@panjinming ~]# [ $x -gt $y ]
[root@panjinming ~]# echo $?
1
[root@panjinming ~]#
[root@p ~]# x=5;y=10[root@p ~]# [$x -gt $y]-bash: [5: command not found[root@p ~]# [ $x -gt $y ][root@p ~]# echo $?1[root@p ~]#
[root@panjinming ~]# name=Tom[root@panjinming ~]# [ $name = [Tt]?? ][root@panjinming ~]# echo $?1[root@panjinming ~]# [[ $name = [Tt]?? ]][root@panjinming ~]# echo $?0[root@panjinming ~]#
--------------------------------------------------------
[-z$str]
如果字符串 str 长度为0,返回真 (为0 返回假)
[-n$str]
如果字符串 str 长度为0,返回假 (不为 0 返回真)
[ $str1 = $str2]
两字符串相等
[ $str1 != $str2]
两字符串不等
[root@p ~]# name=Tom[root@panjinmin ~]# [-z $name]-bash: [-z: command not found[root@p ~]# [ -z $name ][root@p~]# echo $?1[root@p ~]# [ -n $name ][root@p ~]# echo $?0[root@p ~]# name2=Andy[root@p ~]# [ $name=$name2 ] ## 操作符两边的空格[root@p ~]# echo $?0[root@p ~]# [ $name!=$name2 ] ##<span style="font-family: Arial, Helvetica, sans-serif;">操作符两边的空格</span>[root@p ~]# echo $?0[root@p ~]# [ $name = $name2 ][root@p ~]# echo $?1[root@p ~]# [ $name != $name2 ][root@p ~]# echo $?0[root@panjinming ~]#
--------------------------------------------------------
整数测试[ int1 -eqint2 ]
int1等于int2
[ int1 -neint2 ]
int1不等于int2
[ int1 -gtint2 ]
int1大于int2
[ int1 -geint2 ]
int1大于或等于int2
[ int1 -ltint2 ]
int1小于int2
[ int1 -leint2 ]
int1小于或等于int2
x=1; [ $x -eq 1 ];echo $? 0
x=a; [ $x -eq 1 ];echo $? -bash: [: a: integer expression expected 2
==、!=、>、>=、<、<=
x=1; let"$x==1";echo $? 0
x=1; (($x+1>=2 ));echo $? 0
--------------------------------------------------------
[expr1-aexpr2 ]
逻辑与,都为真时,结果为真
[expr1-oexpr2 ]
逻辑或,有一个为真时,结果为真
[!expr ]
逻辑非
x=1; name=Tom;
[ $x -eq 1 –a –n$name ];echo $?
0注:不能随便添加括号
[($x-eq1)–a(–n $name)];echo $?
-bash: syntax error near unexpected token `$x'[[pattern1&& pattern2]]
逻辑与
[[pattern1|| pattern2]]
逻辑或
[[!pattern ]]
逻辑非
x=1; name=Tom;
[[ $x -eq 1 && $name= To? ]];echo $?
0--------------------------------------------------------
[ "$name" = ""]
[ ! "$name" ]
[ "X${name}"="X" ]
--------------------------------------------------------
文件测试
-ffname
fname 存在且是普通文件时,返回真 ( 即返回0 )
-L fname
fname 存在且是链接文件时,返回真
-d fname
fname 存在且是一个目录时,返回真
-e fname
fname(文件或目录)存在时,返回真
-s fname
fname 存在且大小大于0 时,返回真
-r fname
fname(文件或目录)存在且可读时,返回真
-w fname
fname(文件或目录)存在且可写时,返回真
-x fname
fname(文件或目录)存在且可执行时,返回真
--------------------------------------------------------
括号总结
括号形式
功能说明
${...}
获取变量值
$(...)
命令替换
$[...]
让无类型的变量参与算术运算
$((...))
同上
((…))
算术运算
[ ... ]
条件测试,等价于test命令
[[ ... ]]
条件测试,支持模式匹配与通配符
--------------------------------------------------------
总结- (6)Linux ----- Shell编程条件测试 ---- ④
- Linux之shell编程-条件测试
- Linux 与 Shell编程(2)-条件测试
- linux shell编程3 - 条件测试
- linux shell编程(二) 条件测试
- SHELL 编程 -- 条件测试
- shell编程条件测试
- linux shell 测试条件
- linux shell 条件测试
- Shell编程--2.条件测试
- shell编程中的条件测试
- shell编程之条件测试
- Linux Shell编程(三)--条件测试
- linux shell编程指南第十七章------条件测试
- linux之shell 条件测试
- Linux Shell编程-条件判断
- 【Linux shell】 Shell编程中的条件判断
- Shell 编程学习笔记:条件测试
- o2o的理解
- 音视频SDK开发包哪个更好
- 支付宝接入
- silverlight/Flash 跨域访问给新手的一点经验
- 2015如何理财
- (6)Linux ----- Shell编程条件测试 ---- ④
- 自由布局 + Fragment
- C/S框架-发布和部署WebService
- 协议——RTP/RTCP/RTSP/SIP/SDP
- 滑动窗口
- unity4.6.1发布安卓apk安装后启动不了游戏的解决方案
- 周海鹏-从草根到云端–TalkingData 数据库技术进化
- MySQL 数据类型
- 刻苦铭心2014