9.17任务
程序员文章站
2022-05-07 10:54:01
...
20.10 for循环
案例1:求1到100的整数的和。
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
done
echo $sum
[email protected]:~/shell_script# bash for1.sh
5050
[email protected]:~/shell_script# bash -x for1.sh
+ sum=0
++ seq 1 100
+ for i in '`seq 1 100`'
+ sum=1
+ for i in '`seq 1 100`'
+ sum=3
+ for i in '`seq 1 100`'
+ sum=6
...
+ sum=4851
+ for i in '`seq 1 100`'
+ sum=4950
+ for i in '`seq 1 100`'
+ sum=5050
+ echo 5050
5050
案例2:文件列表循环
#!/bin/bash
for a in `ls /etc/`
do
[ -d /etc/$a ] && ls /etc/$a
done
这个会列出/etc下所有的文件,并且ls所有的一级目录。下面是部分过程
+ for a in '`ls /etc/`'
+ '[' -d /etc/pam.d ']'
+ ls /etc/pam.d
chfn chsh common-auth common-session cron gnome-screensaver lightdm-autologin login other polkit-1 runuser sshd sudo unity
chpasswd common-account common-password common-session-noninteractive cups lightdm lightdm-greeter newusers passwd ppp runuser-l su systemd-user
+ for a in '`ls /etc/`'
+ '[' -d /etc/papersize ']'
循环目录的时候要注意文件是否有空格。因为for循环中的条件的分割符是空格或者是回车。
[email protected]:~/shell_script# touch 1\ 2\ 3\ 4
[email protected]:~/shell_script# ll 1\ 2\ 3\ 4
-rw-r--r-- 1 root root 0 Sep 17 23:58 1 2 3 4
[email protected]:~/shell_script# for i in `ls 1\ 2\ 3\ 4 `; do echo $i ; done
1
2
3
4
案例三:常用条件由seq生成
[email protected]:~/shell_script# seq 1 3
1
2
3
[email protected]:~/shell_script# for i in 1 2 3; do echo $i; done
1
2
3
[email protected]:~/shell_script# for i in `seq 1 3`; do echo $i; done
1
2
3
20.11/12 while循环
案例1:每隔30s检查一次系统负载,如果负载过高就报警。
[email protected]# uptime | awk -F 'load average: ' '{print $2}' | cut -d . -f1
0
[email protected]# uptime
00:08:19 up 1 day, 9:02, 3 users, load average: 0.00, 0.00, 0.00
#!/bin/bash
#while :
#while 1
while true
do
load=`uptime | awk -F 'load average: ' '{print $2}' | cut -d . -f1`
if [ $load -gt 10 ]
then
/usr/local/sbin/mail.py [email protected] "load high" "$load"
fi
sleep 30
done
[email protected]:~/shell_script# bash -x while1.sh
+ true
++ uptime
++ awk -F 'load average: ' '{print $2}'
++ cut -d . -f1
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30
while后的条件冒号,非0数字,true都是恒真。
不希望脚本意外中断可以在screen里运行。
案例2:带交互的while循环。让用户做选择,直到满足条件退出。
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
[email protected]:~/shell_script# bash -x while2.sh
+ :
+ read -p 'Please input a number: ' n
Please input a number:
+ '[' -z '' ']'
+ echo 'you need input sth.'
you need input sth.
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: a
+ '[' -z a ']'
++ echo a
++ sed 's/[0-9]//g'
+ n1=a
+ '[' -n a ']'
+ echo 'you just only input numbers.'
you just only input numbers.
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: 12
+ '[' -z 12 ']'
++ echo 12
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ break
+ echo 12
12
本身while的条件是恒真的,是不会退出的,为什么能退出就是因为break关键字,下节再讲。
20.13 break跳出循环
break的作用就是结束循环,跳出到循环体外。
#!/bin/bash
for i in `seq 1 5`
do
echo $i first
if [ $i -eq 3 ]
then
break
fi
echo $i second
done
echo aaa
[email protected]:~/shell_script# bash -x break.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1 first
1 first
+ '[' 1 -eq 3 ']'
+ echo 1 second
1 second
+ for i in '`seq 1 5`'
+ echo 2 first
2 first
+ '[' 2 -eq 3 ']'
+ echo 2 second
2 second
+ for i in '`seq 1 5`'
+ echo 3 first
3 first
+ '[' 3 -eq 3 ']'
+ break
+ echo aaa
aaa
也就是说4和5就没有被执行,直接跳出循环,而且当前循环的后续部分不会被执行。
[email protected]:~/shell_script# bash break.sh
1 first
1 second
2 first
2 second
3 first
aaa
20.14 continue结束本次循环
忽略continue之下的代码,直接进行下一次循环
#!/bin/bash
for i in `seq 1 5`
do
echo $i first
if [ $i -eq 3 ]
then
continue
fi
echo $i second
done
echo aaa
[email protected]:~/shell_script# bash -x continue.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1 first
1 first
+ '[' 1 -eq 3 ']'
+ echo 1 second
1 second
+ for i in '`seq 1 5`'
+ echo 2 first
2 first
+ '[' 2 -eq 3 ']'
+ echo 2 second
2 second
+ for i in '`seq 1 5`'
+ echo 3 first
3 first
+ '[' 3 -eq 3 ']'
+ continue
+ for i in '`seq 1 5`'
+ echo 4 first
4 first
+ '[' 4 -eq 3 ']'
+ echo 4 second
4 second
+ for i in '`seq 1 5`'
+ echo 5 first
5 first
+ '[' 5 -eq 3 ']'
+ echo 5 second
5 second
+ echo aaa
aaa
最明显的就是continue只结束了本次循环,可以看到3 second没有被输出
[email protected]:~/shell_script# bash continue.sh
1 first
1 second
2 first
2 second
3 first
4 first
4 second
5 first
5 second
aaa
20.15 exit退出整个脚本
#!/bin/bash
for i in `seq 1 5`
do
echo $i first
if [ $i -eq 3 ]
then
exit
fi
echo $i second
done
echo aaa
[email protected]:~/shell_script# bash -x exit.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1 first
1 first
+ '[' 1 -eq 3 ']'
+ echo 1 second
1 second
+ for i in '`seq 1 5`'
+ echo 2 first
2 first
+ '[' 2 -eq 3 ']'
+ echo 2 second
2 second
+ for i in '`seq 1 5`'
+ echo 3 first
3 first
+ '[' 3 -eq 3 ']'
+ exit
碰到exit,这个脚本就直接结束了,最后的aaa输出都没有了。
[email protected]:~/shell_script# bash exit.sh
1 first
1 second
2 first
2 second
3 first
exit后面可以跟一个数字,就是$?的返回值。
[email protected]:~/shell_script# bash
[email protected]:~/shell_script# exit
exit
[email protected]:~/shell_script# echo $?
0
[email protected]:~/shell_script# bash
[email protected]:~/shell_script# exit 1
exit
[email protected]:~/shell_script# echo $?
1
扩展
select用法
转载于:https://my.oschina.net/u/3866688/blog/2053323
上一篇: PHP实现模板引擎功能