码迷,mamicode.com
首页 > 其他好文 > 详细

条件判断if

时间:2018-06-18 01:20:10      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:iptable   bak   lse   str   ring   and   pgrep   错误   等于   

条件判断: 语法结构: if [ 条件 ];then command... fi ++++++++++++++++++++++ if [ 条件 ];then command... else command... fi ++++++++++++++++++++++ if [ 条件1 ];then command1... elif [ 条件2 ];then command2... else command3... fi ++++++++++++++++++++++ if [ 条件1 ];then command1... if [ 条件2 ];then command2... fi else if [ 条件3 ];then command3... elif [ 条件4 ];then command4... else command5... fi fi # man test 文件存在与否的判断 -e:是否存在; -f:是否存在并且为普通文件 -d:是否存在并且为目录 -S:是否存在并且套接字文件 -p:是否存在并且管道文件 -c:是否存在并且字符文件 -b:是否存在并且块设备文件 -L:是否存在并且符号连接文件 文件权限相关判断: -r:可读 -w:可写 -x:可执行 -u:是否有suid -g:是否有sgid -k:是否有t位 -s:是否为空白文件,-s表示非空;! -s 空文件 两个文件比较: file1 -nt file2 : 比较文件的新(文件创建的时间) file1 -ot file2:比较文件的老(文件创建的时间) file1 -ef file2 比较file1和file2是否是同一个文件,也可以来判断是否为硬链接文件 整数比较: -eq :相等 -ne :不等 -gt:大于 -lt:小于 -ge:大于等于 -le:小于等于 字符串比较: -z:判断是否为空的字符串 -n:是否为非空的字符串 string1 = string2 string1 != string2 多重条件判断: -a 和 && 逻辑与 [ 条件1 -a 条件2 ] 条件1和条件2必须同时成立整个大条件才成立 -o 和 || 逻辑或 [ 条件1 -o 条件2 ] 条件1和条件2其中一个满足整个大条件就成立 ! 非 优先级: -a 和 && 大于 -o 和 || 大于 ! demo1:判断ip是否通 ping -c --->$? # vim ping_ip.sh #!/bin/bash #Name:xxx #Usage:xxx #Update: #Path:xxx (1) ping -c 2 10.1.1.254 > /dev/null 2>&1 if [ $? -eq 0 ];then echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log else echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log fi (2) ping -c 2 $1 > /dev/null 2>&1 if [ $? -eq 0 ];then echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log else echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log fi (3) read -p "Input your ip:" IP ping -c 2 $IP > /dev/null 2>&1 if [ $? -eq 0 ];then echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log else echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log fi (4) read -p "Input your ip:" IP ping -c 2 $IP > /dev/null 2>&1 [ $? -eq 0 ] && echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log || echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log (5) read -p "Input your ip:" IP ping -c 2 $IP > /dev/null 2>&1 test $? -eq 0 && echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log || echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log demo2:判断一个进程是否存在 #!/bin/bash ps -ef|grep vsftpd|grep -v ‘grep‘ &>/dev/null 或者 ( # pgrep -l vsftpd) [ $? -eq 0 ] && echo "vsftpd ok" >>/tmp/log ||echo "vsftpd not ok" >>/tmp/log demo3:判断一个服务是否正常 #!/bin/bash wget -P /tmp http://localhost &>/dev/null [ $? -ne 0 ] && echo "httpd not ok" >>/tmp/log ||echo "httpd is ok" >>/tmp/log 练习: 1、写一个脚本判断一个用户是否存在 2、完善上第一个例子中给脚本传参时的bug,如果每个给脚本传参或者参数个数不等于1时,提示脚本的用法:usage:xxx.sh ip 练习1: 1、判断/tmp/run目录是否存在,如果不存在就建立,如果存在就删除目录里所有文件 #!/bin/bash dir=/tmp/run [ -f $dir ] && mv $dir $dir.bak [ -d $dir ] && rm -fr $dir/* || mkdir $dir -p 2、输入一个路径,判断路径是否存在,而且需要输出是文件还是目录,如果是链接文件,还需要输出是有效连接还是无效链接 3、交互式输入一个ip,然后脚本判断这个ip是否能ping通,输入结果如下: Server 10.1.1.20 is Down 最后要求将结果邮件给管理员root和redhat echo hello |mail root mail -s xxx root 4、写一个脚本,要求当给脚本输入参数hello时,脚本返回world;给脚本输入world时,脚本返回hello。当脚本没有参数或者参数错误时,屏幕上输出脚本的用法:"usage:xxx hello or world" 5、写一个脚本自动搭建nfs服务 /nfs/share 10.1.1.1(rw) #!/bin/bash #关闭防火墙和selinux service iptables stop setenfoce 0 > /dev/null #配置yum源 #1.判断内网网络是否ok ip=10.1.1.254 ping -c $ip >/dev/null # 判断 # wget -P /etc/yum.repos.d/ ftp://$ip/xxx.repo &>/dev/null #软件三步曲 # yum -y install xxxx read -p "dir" dir [ ! -d $dir ] && mkdir -p $dir chmod 1777 $dir read -p "192.168.0.1(ro):" host cat >> /etc/exports << end $dir $host end #重启服务 #测试验证 mkdir /u01 mount.nfs $ip:/$dir /u01 ....

条件判断if

标签:iptable   bak   lse   str   ring   and   pgrep   错误   等于   

原文地址:https://www.cnblogs.com/skyzy/p/9194209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!