shell的for循环语句(下)

简介: shell的for循环语句

3. 课堂练习

㈠ 批量创建用户

**需求1:**批量新建5个用户stu1~stu5,要求这几个用户的家目录都在/rhome.


#!/bin/bash#判断/rhome是否存在[ -f/rhome ] &&mv/rhome/rhome.baktest!-d/rhome&&mkdir/rhome或者[ -f/rhome ] &&mv/rhome/rhome.bak|| [ !-d/rhome ] &&mkdir/rhome#创建用户,循环5次for ((i=1;i<=5;i++))
douseradd-d/rhome/stu$istu$iecho123|passwd--stdinstu$idone


㈡ 局域网内脚本检查主机网络通讯

需求2:


写一个脚本,局域网内,把能ping通的IP和不能ping通的IP分类,并保存到两个文本文件里


以10.1.1.1~10.1.1.10为例


10.1.1.1~10.1.1.254#!/bin/bash#定义变量ip=10.1.1#循环去ping主机的IPfor ((i=1;i<=10;i++))
doping-c1$ip.$i&>/dev/nullif [ $?-eq0 ];thenecho"$ip.$i is ok">>/tmp/ip_up.txtelseecho"$ip.$i is down">>/tmp/ip_down.txtfi或者    [ $?-eq0 ] &&echo"$ip.$i is ok">>/tmp/ip_up.txt||echo"$ip.$i is down">>/tmp/ip_down.txtdone[root@servershell03]#time ./ping.shreal0m24.129suser0m0.006ssys0m0.005s



延伸扩展:shell脚本并发


并行执行:{程序}&表示将程序放到后台并行执行,如果需要等待程序执行完毕再进行下面内容,需要加wait#!/bin/bash#定义变量ip=10.1.1#循环去ping主机的IPfor ((i=1;i<=10;i++))
do{
ping-c1$ip.$i&>/dev/nullif [ $?-eq0 ];thenecho"$ip.$i is ok">>/tmp/ip_up.txtelseecho"$ip.$i is down">>/tmp/ip_down.txtfi}&donewaitecho"ip is ok...."[root@server~]#time ./ping.shipisok...
real0m3.091suser0m0.001ssys0m0.008s


㈢ 判断闰年

需求3:


输入一个年份,判断是否是润年(能被4整除但不能被100整除,或能被400整除的年份即为闰年)


#!/bin/bashread-p"Please input year:(2017)"yearif [ $[$year%4] -eq0-a$[$year%100] -ne0 ];thenecho"$year is leap year"elif [ $[$year%400] -eq0 ];thenecho"$year is leap year"elseecho"$year is not leap year"fi


##4. 总结


FOR循环语法结构

FOR循环可以结合条件判断和流程控制语句

do …done 循环体

循环体里可以是命令集合,再加上条件判断以及流程控制

控制循环语句

continue 继续,跳过本次循环,继续下一次循环

break 打断,跳出循环,执行循环体外的代码

exit 退出,直接退出程序

#二、while循环语句


特点:条件为真就进入循环;条件为假就退出循环


##1. while循环语法结构


while表达式docommand...
donewhile  [ 1-eq1 ] 或者 (( 1>2 ))
docommandcommand     ...
done


循环打印1-5数字


FOR循环打印:for ((i=1;i<=5;i++))
doecho$idonewhile循环打印:i=1while [ $i-le5 ]
doecho$ileti++done


2. 应用案例

㈠ 脚本计算1-50偶数和

#!/bin/envbashsum=0for ((i=0;i<=50;i+=2))
doletsum=$sum+$i  (letsum=sum+i)
doneecho"1-50的偶数和为:$sum"#!/bin/bash#定义变量sum=0i=2#循环打印1-50的偶数和并且计算后重新赋值给sumwhile [ $i-le50 ]
doletsum=$sum+$ileti+=2或者$[$i+2]
done#打印sum的值echo"1-50的偶数和为:$sum"



㈡ 脚本同步系统时间

① 具体需求

写一个脚本,30秒同步一次系统时间,时间同步服务器10.1.1.1

如果同步失败,则进行邮件报警,每次失败都报警

如果同步成功,也进行邮件通知,但是成功100次才通知一次

② 思路

每隔30s同步一次时间,该脚本是一个死循环 while 循环


同步失败发送邮件 1) ntpdate 10.1.1.1 2) rdate -s 10.1.1.1


同步成功100次发送邮件 定义变量保存成功次数


③ 落地实现

#!/bin/envbash#该脚本用于时间同步NTP=10.1.1.1count=0whiletruedontpdate$NTP&>/dev/nullif [ $?-ne0 ];thenecho"system date failed"|mail-s"check system date"root@localhostelseletcount++if [ $count-eq100 ];thenecho"systemc date success"|mail-s"check system date"root@localhost&&count=0fifisleep30done#!/bin/bash#定义变量count=0ntp_server=10.1.1.1whiletruedordate-s$ntp-server&>/dev/nullif [ $?-ne0 ];thenecho"system date failed"|mail-s'check system date'root@localhostelseletcount++if [ $[$count%100] -eq0 ];thenecho"system date successfull"|mail-s'check system date'root@localhost&&count=0fifisleep3done以上脚本还有更多的写法,课后自己完成



#三、until循环


特点:条件为假就进入循环;条件为真就退出循环


1. until语法结构

untilexpression   [ 1-eq1 ]  (( 1>=1 ))
docommandcommand  ...
done



打印1-5数字


i=1while [ $i-le5 ]
doecho$ileti++donei=1until [ $i-gt5 ]
doecho$ileti++done


2. 应用案例

###㈠ 具体需求


使用until语句批量创建10个用户,要求stu1—stu5用户的UID分别为1001—1005;

stu6~stu10用户的家目录分别在/rhome/stu6—/rhome/stu10

㈡ 思路

创建用户语句 useradd -u|useradd -d

使用循环语句(until)批量创建用户 until循环语句结构

判断用户前5个和后5个 条件判断语句

㈢ 落地实现

#!/bin/envbashif [ -d/rhome ];thenecho"/rhome目录已存在"elsemkdir/rhomeecho"/rhome不存在,已完成创建"fii=1until [ $i-gt10 ]
doif [ $i-le5 ];thenuseradd-u$[1000+$i] stu$iecho123|passwd--stdinstu$ielseuseradd-d/rhome/stu$istu$iecho123|passwd--stdinstu$ifileti++done==================================================#!/bin/bashi=1until [ $i-gt10 ]
doif [ $i-le5 ];thenuseradd-u$[1000+$i] stu$i&&echo123|passwd--stdinstu$ielse  [ !-d/rhome ] &&mkdir/rhomeuseradd-d/rhome/stu$istu$i&&echo123|passwd--stdinstu$ifileti++done


相关文章
|
2月前
|
人工智能 机器人 Shell
【shell】shell条件判断、循环语句、基本运算符
【shell】shell条件判断、循环语句、基本运算符
|
27天前
|
Shell UED Python
Shell 循环语句:重复任务的自动化利器
在Shell脚本中,循环语句如`while`和`for`是自动化任务的关键。`while`循环在条件满足时执行,例如计算1到100的和;`for-in`循环遍历列表,可用于迭代指定数值或命令输出,如求1到100的和。`select-in`循环提供交互式菜单,增强脚本用户体验。理解并运用这些循环能提升脚本效率和可读性。现在,动手试试吧!
26 2
|
2月前
|
机器学习/深度学习 Shell Perl
shell 脚本循环语句
shell 脚本循环语句
|
2月前
|
Shell 数据安全/隐私保护
shell的for循环语句
shell的for循环语句
57 0
|
10月前
|
Shell C语言
shell编程之for、while、until循环语句
shell编程之for、while、until循环语句
72 1
|
Shell
【shell】shell条件判断、循环语句、基本运算符
【shell】shell条件判断、循环语句、基本运算符
168 0
|
Shell
【Shell编程】Shell中for循环、while循环、until循环语句
【Shell编程】Shell中for循环、while循环、until循环语句
43 0
|
网络协议 Perl
Linux-Shell语言循环语句练习
Linux-Shell语言循环语句练习
81 0
|
Shell 数据安全/隐私保护
|
Shell 数据安全/隐私保护
Shell编程之循环语句与函数(for、while)
Shell编程之循环语句与函数(for、while)
Shell编程之循环语句与函数(for、while)