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 "
GRADE−le100];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 "
GRADE−le88];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