2021-10-29

2021年10月29日 星期五 晴

++++++++++++++++++++++++++++++++++
10月马上结束了,完成目标还差的有点远。
是什么阻挡了你前进的脚步?
++++++++++++++++++++++++++++++++++

(4.3.1)if条件测试语句
1、用if条件测试语句判断一个目录是否存在,若不存在则创建目录,反之结束测试并ls 查看目录
#!/bin/basn
#test dir yes or no
DIR="/media/cdrom"
if [ ! -d $DIR ] ; then
mkdir -p $DIR
fi
ls -l /media
[root@linuxprobe media]# bash ~/d.sh
total 0
drwxr-xr-x. 2 root root 6 Oct 30 08:41 cdrom
2、用if条件语句双分支if then else fi 组合ping命令实验,并用输出重定把过程输出到黑洞文件 &> /dev/null

ping -c 3 -i 0.2 -w 3 $1 &> /dev/null
-c 3次
-i 数据包发间隔
-w 超时等待时间

#!/bin/bash
#test ping
ping -c 3 -i 0.3 -w 5 $1 &> /dev/null
if [ $? -eq 0 ] ; then
echo “host $1 online”
else
echo “host $1 sleeping”
fi

[root@linuxprobe ~]# bash p.sh 127.0.0.1
host 127.0.0.1 online
[root@linuxprobe ~]# bash p.sh 192.168.100.1
host 192.168.100.1 sleeping

3、用if then elif else if 组成多分支结构实验,并用read接收用户输入的信息。
[root@linuxprobe ~]# cat grade.sh
#! /bin/bash
#for grade scripts
read -p “Enter your score (0-100): " GRADE
if [ $GRADE -ge 89 ] && [ G R A D E − l e 100 ] ; t h e n e c h o " GRADE -le 100 ] ; then echo " GRADEle100];thenecho"GRADE is Excellent”
elif [ $GRADE -ge 70 ] && [ G R A D E − l e 88 ] ; t h e n e c h o " GRADE -le 88 ] ; then echo " GRADEle88];thenecho"GRADE is pass"
else echo “$GRADE is fail”
fi
root@linuxprobe ~]# bash grade.sh
Enter your score (0-100): 90
90 is Excellent
[root@linuxprobe ~]# bash grade.sh
Enter your score (0-100): 87
87 is pass
[root@linuxprobe ~]# bash grade.sh
Enter your score (0-100): 60
60 is fail

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值