初次编辑2017年9月26日 9:35:33
摘要
引用:百度
- 循环结构
一.while循环
- while循环
- while (条件)
do
动作
done - 需要无限循环时我们会选择while :
- continue:默认退出本次循环
- break:默认退出本层循环
- while (条件)
- while死循环
1 #!/bin/bash 2 var1=AAA 3 var2=BBB 4 var3=CCC 5 while : 6 do 7 clear 8 echo -e "A:${var1} B:${var2} C:${var3}" 9 temp=$var1 10 var1=$var2 11 var2=$var3 12 var3=$temp 13 sleep 1 14 done
- 列出数值1至10
1 #!/bin/bash 2 i=1 3 while ((i<10)) 4 do 5 echo $i 6 ((i++)) 7 done
- 判断账号密码
1 #!/bin/bashwhile : 2 do 3 read -p 'please input your name: ' name 4 read -p 'please input your password: ' pwd 5 if [ $name = 'egon' ] && [ $pwd = '123' ] 6 then 7 echo 'login sucessful' 8 break #continue 9 fi 10 done 11 [root@MiWiFi-R3-srv ~]# ./login.sh 12 please input your name: egon 13 please input your password: 123 14 login sucessful
二. for循环
- 语句格式
- for i in {1..10}
do
echo $i
done
- for i in {1..10}
三.case循环
- 语句格式
- case $uname in
root)
echo "welcome $uname"
;;
seker)
echo "welcome $uname"
;;
default)
echo "welcome $uname"
;;
*)
echo "no user $uname"
esac
- case $uname in