一、if条件判断
1. 语法格式: if then else fi 2. 实例: 如果上条命令执行正确,返回commend execute successful 如果上条命令执行错误,返回commend execute failed
[root@localhost script]
[root@localhost script]
[root@localhost script]
ls &>> /dev/null
if [ "$?"==0 ]
then
echo "commend execute successful"
else
echo "commend execute failed"
fi
[root@localhost script]
commend execute successful
#同上,只是把 $? 的引号去掉了:
[root@localhost script]
ls &>> /dev/null
if [ $? == 0 ]
then
echo "commend execute successful"
else
echo "commend execute failed"
fi
[root@localhost script]
commend execute successful
[root@localhost script]
Ls &>> /dev/null
if [ "$?" == 0 ]
then
echo "commend execute successful"
else
echo "commend execute failed"
fi
[root@localhost script]
commend execute failed
判断ip是否up
[root@localhost script]
ping -c 1 $1 &>> /dev/null
if [ $? == 0 ]
then
echo "$1 is up"
else
echo "$1 is down"
fi
[root@localhost script]
[root@localhost script]
10.10.10.10 is down
[root@localhost script]
PING 10.10.10.10 (10.10.10.10) 56(84) bytes of data.
逻辑运算符: && ||
[root@localhost script]
&&:前面执行正确,就不执行后面 ||:前面执行错误,才会执行后面(执行第一个正确的)
二、case语句
1. 语法格式: case var in var1) ;; var2) ;; *) 匹配不到结果,执行该条 ;; 2. 实例
[root@localhost script]
[root@localhost script]
[root@localhost script]
echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
1) echo 'You select 1'
;;
2) echo 'You select 2'
;;
3) echo 'You select 3'
;;
4) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac
[root@localhost script]
Input a number between 1 to 4
Your number is:\c
2
You select 2
[root@localhost script]
Input a number between 1 to 4
Your number is:\c
5
You do not select a number between 1 to 4
注: read:表示从用户的输入获取变量
三、for循环
1. 语法格式 for var in ## do done
2.实例 在这里,总结了 in 后面的两种样式:
in后面样式一:
[root@localhost script]
[root@localhost script]
for i in {1..10}
do
echo "The vaule is: $i"
done
[root@localhost script]
[root@localhost script]
The vaule is: 1
The vaule is: 2
The vaule is: 3
The vaule is: 4
The vaule is: 5
The vaule is: 6
The vaule is: 7
The vaule is: 8
The vaule is: 9
The vaule is: 10
in后面样式二:
[root@localhost script]
[root@localhost script]
for i in 1 2 3 4 5
do
echo "The vaule is: $i"
done
[root@localhost script]
The vaule is: 1
The vaule is: 2
The vaule is: 3
The vaule is: 4
The vaule is: 5
实例三: 使用本机 ping 一下10.163.1.2-10网段的设备,检查哪些设备是 up ,哪些设备 down
[root@localhost script]
for i in {2..10}
do
ping 10.163.1.$i -c 1 &>> /dev/null && echo "$i up" || echo "$i down" &
done
wait
[root@localhost script]
[root@localhost script]
7 down
8 down
3 down
4 down
5 down
9 down
6 down
2 down
10 down
同理:使用本机 ping 一下10.163.1.*/24网段的设备,检查哪些设备是 up ,哪些设备 down
[root@localhost script]
for i in {1..254}
do
ping 10.163.1.$i -c 1 &>> /dev/null && echo "$i up" || echo "$i down" &
done
wait
注: &: 放到最后面,表示放到后台执行,2百多条都放到后台,营造一种并发的效果。前台执行只能一条执行完再执行下一条。 wait: 表示等待上面的命令执行完,再能执行其他命令。放到后台执行时,一定要写wait。
四、while循环
1.语法格式
while [条件] #当while后面的条件成立时,执行do里面的内容 do done
2.几个“运算符”
ge: greater equal 大于等于 gt: grater than 大于 le: less equal 小于等于 lt: less than 小于 eq: equal 等于
3.实例
当COUNTER变量的值小于 5 时,输出 COUNTER + 1 的值
[root@localhost script]
COUNTER=0
while [ "$COUNTER" -lt 5 ]
do
COUNTER=`expr $COUNTER + 1`
echo $COUNTER
done
[root@localhost script]
1
2
3
4
5
同上,只是将 $COUNTER 的引号去掉了,依然成立:
[root@localhost script]
[root@localhost script]
COUNTER=0
while [ $COUNTER -lt 5 ]
do
COUNTER=`expr $COUNTER + 1`
echo $COUNTER
done
[root@localhost script]
[root@localhost script]
1
2
3
4
5
五、until循环
1.语法格式
until [条件] #当until后面的条件成立时,退出循环 do done
2.实例
当变量 a 的值不小于 10 时(即>=10时),退出循环
[root@localhost script]
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
[root@localhost script]
[root@localhost script]
0
1
2
3
4
5
6
7
8
9
快乐学习,不足之处,欢迎指出,创作不易,转载请 注明出处,谢谢
|