20.10 for循环
eg:
求1到100数字的和。
[[email protected] sbin]# vim sum.sh
#!/bin/bash
sum=0
for i in seq 1 5
do
sum=$[sum+$i]
done
echo "$sum"
[[email protected] sbin]# sh sum.sh
15
文件列表循环
[[email protected] sbin]# vim for.sh
#!/bin/bash
dir=/usr/local/sbin/
for a in ls $dir
do
if [ -d $a ]
then
echo $a
ls $a
fi
done
echo "No directory file!"
[[email protected] sbin]# sh -x for.sh
- dir=/usr/local/sbin/
++ ls /usr/local/sbin/ - for a in ‘
ls $dir
‘ - ‘[‘ -d datefile.sh ‘]‘
- for a in ‘
ls $dir
‘ - ‘[‘ -d filetar.sh ‘]‘
- for a in ‘
ls $dir
‘ - ‘[‘ -d for.sh ‘]‘
- for a in ‘
ls $dir
‘ - ‘[‘ -d if2.sh ‘]‘
- for a in ‘
ls $dir
‘ - ‘[‘ -d sum.sh ‘]‘
- echo ‘No directory file!‘
No directory file!
for——分隔符
[[email protected] adai]# ll
总用量 0
-rw-r--r-- 1 root root 0 9月 14 19:25 1
-rw-r--r-- 1 root root 0 9月 14 19:25 2
-rw-r--r-- 1 root root 0 9月 14 19:25 3 4.txt
#注意:3 4.txt是一个文件(34中间有一个空格)
[[email protected] adai]# for i in ls ./
; do echo $i ; done
1
2
3
4.txt
以上结果显示说明,for默认情况下把空格或换行符(回车)作为分隔符。
20.11-20.12 while循环
格式: while 条件;do…;done
eg:
当系统负载大于10的时候,发送邮件,每隔30秒执行一次。
[[email protected] adai]# vim while.sh
#!/bin/bash
while :
do
load=w|head -1 |awk -F ‘load average:‘ ‘{print $2}‘ |cut -d . -f1
if [ $load -gt 10 ]
then
top |mail -s "load is high: $load" [email protected]
fi
sleep 30
done
#while “:”表示死循环,也可以写成while true,意思是“真”(数学--真命题、假命题)
#Attention:awk -F ‘load average: ‘此处指定‘load average: ‘为分隔符,注意冒号后面的空格
#如果不加该空格,过滤出来的结果会带空格,需要在此将空格过滤掉
[[email protected] adai]# sh -x while.sh
- :
++ head -1
++ awk -F ‘load average: ‘ ‘{print $2}‘
++ cut -d. -f1
++ w - load=0
- ‘[‘ 0 -gt 10 ‘]‘
- sleep 30
.
.
.
如果不手动停止该脚本,它会一直循环执行(按Ctrl+c结束),实际环境中配合screen使用。
交互模式下,用户输入一个字符,检测该字符是否符合条件,如:空、非数字、数字。分别对字符做出判断,然后做出不同的回应。
[[email protected] sbin]# vim while2.sh
#!/bin/bash
while true
do
read -p "Please input a number:" n
if [ -z "$n" ]
then
echo "You need input some characters!"
continue
fi
n1=echo $n|sed ‘s/[-0-9]//g‘
if [ -n "$n1" ]
then
echo "The character must be a number!"
continue
fi
break
done
echo $n
#continue:中断本次while循环后重新开始;
#break:表示跳出本层循环,即该while循环结束
[[email protected] sbin]# sh while2.sh
Please input a number:
You need input a character!
Please input a number:eee333
The character must be a number!
Please input a number:3
3
20.13 break 跳出循环
eg:
[[email protected] sbin]# vim break.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
break
fi
echo "$i"
done
echo "Finished!"
[[email protected] sbin]# sh break.sh
1
1
2
2
3
Finished!
即,跳出while循环,继续执行循坏之外的命令。
20.14 continue 结束本次循环
eg:
[[email protected] sbin]# vim continue.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
continue
fi
echo "$i"
done
echo "Finished!"
[[email protected] sbin]# sh continue.sh
1
1
2
2
3
4
4
5
5
Finished!
即,结束本次循环之后重新开始下一次循环。
20.15 exit退出整个脚本
eg:
[[email protected] sbin]# vim exit.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
exit
fi
echo "$i"
done
[[email protected] sbin]# sh exit.sh
1
1
2
2
3
退出整个脚本,后面的命令不会执行。
扩展:shell中select的用法
select也是循环的一种,它比较适合用在用户选择的情况下。
比如,我们有一个这样的需求,运行脚本后,让用户去选择数字,选择1,会运行w命令,选择2运行top命令,选择3运行free命令,选择4退出。脚本这样实现:
#!/bin/bash
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo
select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
;;
esac
done
执行结果如下:
sh select.sh
Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit
1) w
2) top
3) free
4) quit
#? 1
16:06:58 up 109 days, 22:01, 1 user, load average: 0.11, 0.05, 0.01
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 222.128.156.84 16:05 0.00s 0.00s 0.00s w
#? 3
total used free shared buffers cached
Mem: 1020328 943736 76592 0 86840 263624
-/+ buffers/cache: 593272 427056
Swap: 2097144 44196 2052948
#?
我们发现,select会默认把序号对应的命令列出来,每次输入一个数字,则会执行相应的命令,命令执行完后并不会退出脚本。它还会继续让我们再次输如序号。序号前面的提示符,我们也是可以修改的,利用变量PS3即可,再次修改脚本如下:
#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo
select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
esac
done
如果想要脚本每次输入一个序号后就自动退出,则需要再次更改脚本如下:
#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo
select command in w top free quit
do
case $command in
w)
w;exit
;;
top)
top;exit
;;
free)
free;exit
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4).";exit
esac
done
原文地址:http://blog.51cto.com/13242922/2105610