这篇文章上次修改于 1689 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
Shell脚本
循环语句应用
for语句
语法格式1
for 变量 in 值1 值2 ... 值N
do
命令序列
done
1.给多个用户群发邮件
[root@Hyui-VM ~]# cat sendmail.sh
#!/bin/bash
DOMAIN=gmail.com
for MAIL_U in tom tim sam
do
mail -s "Log" $MAIL_U@$DOMAIN < /var/log/messages
done
2.将1~10循环赋值给变量NUM,只输出偶数
[root@Hyui-VM ~]# cat numprint.sh
#!/bin/bash
for NUM in {1..10}
do
if [ $(($NUM%2)) -eq 0 ];
then
echo $NUM
fi
done
运行结果:
[root@Hyui-VM ~]# ./numprint.sh
2
4
6
8
10
语法格式2
for ((初始化变量值;结束循环条件;运算))
do
命令序列
done
使用for语句打印9x9乘法表
[root@Hyui-VM ~]# cat 9x9.sh
#!/bin/bash
for i in {1..9}
do
for ((j=1;j<=i;j++))
do
printf "%-8s" $j*$i=$((j*i))
done
done
%-8s意为在变量右边缩进8个字符,运行结果:
[root@Hyui-VM ~]# ./9x9.sh
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
while语句
语法格式1
while [条件]
do
命令序列
done
使用while将1~10赋值给NUM变量,只输出奇数
[root@Hyui-VM ~]# cat numprint.sh #!/bin/bash NUM=1 while [ $NUM -lt 11 ] do if [ $(($NUM%2)) -ne 0 ]; then echo $NUM fi NUM=$(($NUM+1)) done
运行结果:
[root@Hyui-VM ~]# ./numprint.sh 1 3 5 7 9
语法格式2
while read -r line do 命令序列 done < file
1.打印rc.local文件的每一行
[root@Hyui-VM ~]# cat printrc.sh #!/bin/bash FILE=/etc/rc.local while read -r line do echo $line done < $FILE
运行结果:
[root@Hyui-VM ~]# ./printrc.sh #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local
2.无限循环菜单的实现,用户通过选择实现不同的菜单功能
[root@Hyui-VM ~]# cat tools.sh #!/bin/bash while true do clear echo "------------------------------" echo "1.查看CPU信息" echo "2.查看系统运行时间" echo "3.查看内存及swap情况" echo "4.查看存储挂载情况" echo "5.退出程序" echo "------------------------------" read -p "请输入一个数字进行选择(1~5):" SELECT_N case $SELECT_N in 1) echo $(cat /proc/cpuinfo) read -p "按任意键继续" ;; 2) echo $(uptime) read -p "按任意键继续" ;; 3) echo $(free) read -p "按任意键继续" ;; 4) echo $(df -h) read -p "按任意键继续" ;; 5) exit ;; *) read -p "请输入1~5之间的数字,点击回车继续" esac done
until语句
until [ 条件 ] do 命令序列 done
- until和while有什么区别?
while为满足条件就执行,until为满足条件就不执行。 使用until实现从5递减到1
[root@Hyui-VM ~]# cat 5.sh #!/bin/bash NUM=5 until [ $NUM -eq 0 ] do echo $NUM NUM=$(($NUM-1)) done
运行结果:
[root@Hyui-VM ~]# ./5.sh 5 4 3 2 1
select语句
- select与for循环格式相同
使用select实现带用户选择项的提问菜单
[root@Hyui-VM ~]# cat form.sh #!/bin/bash echo "Which city do you like?" select addr in "Beijing" "NewYork" "London" "Tokyo" do break done echo "You like $addr"
运行结果:
[root@Hyui-VM ~]# ./form.sh Which city do you like? 1) Beijing 2) NewYork 3) London 4) Tokyo #? 4 You like Tokyo
没有评论