设为首页 收藏本站
查看: 756|回复: 0

shell学习之条件测试(参考shell脚本编程诀窍)

[复制链接]

尚未签到

发表于 2018-8-29 09:27:25 | 显示全部楼层 |阅读模式
  1关于test测试,查看man文档知
  表达式的判断
  ( EXPRESSION )                    #EXPRESSION is true
  ! EXPRESSION                      #EXPRESSION is false
  EXPRESSION1 -a EXPRESSION2        #both are true,-o means or
  字符串是否为空,相等
  -n STRING  #the length of STRING is nonzero,-n can be removed
  -z STRING  #the length of STRING is zero(nonexists,or null)
  #when comes to string, we use =
  STRING1 = STRING2       #the strings are equal
  STRING1 != STRING2      #the strings are not equal
  数字的比较
  #when comes to number,we use different eq ne and so on
  INTEGER1 -eq INTEGER2   #INTEGER1 is equal to INTEGER2
  -ge  greater or equal
  -gt  greater than
  -le   -lt   -ne   I
  比较文件是否为链接文件
  FILE1 -ef FILE2         #FILE1 and FILE2 have the same device and inode numbers
  eg
                [root@www shell]# test  txt  -ef txthardlink  
                [root@www shell]# echo $?
  
                0
  
                [root@www shell]# test  txt  -ef txtlink(soft)
  
                [root@www shell]# echo $?
  
                0
  所以这个是测试是否是文件链接(包括软硬),大家可以自己试试
  比较文件的修改时间
  FILE1 -nt FILE2#FILE1 is newer/older (modification date) than FILE2,timestamp
  FILE1 -ot FILE2    文件有修改时间modify time (强调内容改变) 改变时间change time(本质即相关属性改变)  接触时间access time(被访问)
  文件类型
  -b FILE     #type:block special
  -c FILE     #type:character special
  -d FILE     # a directory
  -e FILE     #FILE exists是否存在

  -s FILE     #FILE exists and has a>  -f FILE     #a regular file
  -u FILE     #set user-ID   SUID
  -g FILE     #FILE exists and is set-group-ID使用时暂时拥有文件所属组的权限SGID

  -G FILE     #FILE exists and is owned by the effective group>  -h FILE     #a symbolic link (same as -L)
  -k FILE     #FILE exists and has its sticky bit set   sticky位其他用户只能添加不能删除
  -L FILE     #FILE exists and is a symbolic link (same as -h)

  -O FILE     #FILE exists and is owned by the effective user>  -p FILE     #a named pipe
  -S FILE     #FILE exists and is a socket
  -r FILE     #FILE exists and read permission is granted
  you can  use -w  ,write  ,-x  execute
  -t FD       #file descriptor FD is opened on a terminal
  很多,但有规律,常用的并不多
  2 上面的测试还可用于类似[ -e $num ]结构中
  3 if/then条件测试结构(还有case,while,等结构可能后面会涉及,不懂请自行baidu)
  ①if  [ condition ]
  then
  COMMAND
  fi
  or
  if  [ condition ];then
  COMMAND
  fi
  ②if  [ condition ];then
  COMMAND1
  else
  COMMAND2
  fi
  ③if  [ condition ];then
  COMMAND1
  elif  [ condition ];then
  COMMAND2
  else
  COMMAND3
  fi
  big eg.
#if1.sh  
#!/bin/bash
  
#
  
read -p "guss who am i?"  HELLO
  
test -z $HELLO&&echo "wrong argv please reinput!"&&exit 1
  
if [ "$HELLO" = "Jack" ];then
  
echo "oh,you are jack!"
  
elif [ "$HELLO" = "Kitty" ];then
  
echo "oh,you are Kitty!"
  
elif [ "$HELLO" = "Mike" ];then
  
echo "oh,you are Mike!"
  
else
  
echo "sorry i don't remember."
  
echo "ok,my name is $HELLO"
  
fi
  通常if then可以改为` conditon `&&条件ok||条件不ok
  3 ` `与[]的区别
  通常表现在以下几个方面
  [ ]使用-a,-o连接多个逻辑表达式,而` `使用&&,||连接多个表达式
  [ ]:通配符不起作用,` `会展开通配符
[ ]不能使用比较运算符,如>,

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-558000-1-1.html 上篇帖子: SHELL中的while与unil流程控制 下篇帖子: shell监控脚本1
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表