双层循环和循环控制语句 while与until

双层循环和循环控制循环语句

exit:满足条件立马跳出,无论有几层

echo 打印

echo -n 表示不换行输出

echo -e 表示输出转义字符

\b:相当于退格键(backspace)

\n:换行,相当于回车

\f:换行,换行后的新行的开头连接上一行的行尾

\t:相当于tab键,横向制表符

复制代码
[root@test2 opt]# vim test3.sh
for i in {1..9}
do
 for ((j=1;j<i;j++))
   do
    echo -n "$i*$j=$(($i*$j))"
  done
echo 
done
​
[root@test2 opt]# sh test3.sh 
1*1=1
2*1=2
3*1=33*2=6
4*1=44*2=84*3=12
5*1=55*2=105*3=155*4=20
6*1=66*2=126*3=186*4=246*5=30
7*1=77*2=147*3=217*4=287*5=357*6=42
8*1=88*2=168*3=248*4=328*5=408*6=488*7=56
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=72

break控制语句

复制代码
[root@test2 opt]# vim test3.sh
for i in {1..3}
do
 for j in {1..5}
do
if [ $j -eq 3 ]
then
break 2
#循环控制,continue和break默认数值就是1.
fi
echo $j
done
done
​
[root@test2 opt]# sh test3.sh 
1
2

continue控制语句

复制代码
[root@test2 opt]# vim test3.sh
for i in {1..3}
do
 for j in {1..5}
do
if [ $j -eq 3 ]
then
continue  2
#循环控制,continue和break默认数值就是1.
fi
echo $j
done
echo $i
done
​
[root@test2 opt]# sh test3.sh 
1
2
1
2
1
2

set -x 开启调试模式

set +x 关闭调试模式

while和until的语法

while:满足条件就执行循环,不满足条件才会推出。

死循环,不知道要循环多少次,需要主动结束循环或者达到条件才结束循环

复制代码
while [判断条件]
do
•     命令序列
done
复制代码
求1-100的和
[root@test2 opt]# vim test3.sh
i=0
sum=0
while [ $i -lt 100 ]
do
 let i++
 sum=$(($sum + $i))
echo $sum
done
复制代码
求奇偶
i=0
sum=0
sum2=0
while [ $i -lt 100 ]
do
let i++
if (( $i%2==0 ))
then
 sum=$(($sum + $i))
else
sum2=$(($sum2+$i))
fi
echo $sum
done
echo $sum2
while死循环

while true #永远为增,死循环

while [ 1 -eq 1 ]

while:

until循环:

条件不满足才执行循环,一旦条件成立,循环终止。

复制代码
[root@test2 opt]# vim test3.sh
i=0
until [ $i -gt 10 ]
do
 let i++
echo $i
done
[root@test2 opt]# sh test3.sh 
1
2
3
4
5
6
7
8
9
10
11

测试一个网段,c类的私有地址 32位的网段,测试可以ping同的保留到/opt/hosts.txt,如果不通提示不通即可。1-254

复制代码
for i in {1..254}
do
{
ping -c 2 192.168.65.$i
if [ $? -eq 0 ]
then
echo ping -c 2 192.168.65.$i >> /opt/hosts.txt
else
echo "不通"
fi
}&
done
echo $i
wait
[root@test2 opt]# cat /opt/hosts.txt
ping -c 2 192.168.65.2
ping -c 2 192.168.65.21
ping -c 2 192.168.65.2
ping -c 2 192.168.65.21
复制代码
设置密码错误三次
[root@test2 opt]# vim test1.sh
sum=123456
for ((i=1;i<=3;i++))
do
read -p "输入密码" a
if [ "$sum" -eq "$a" ]
then
echo "正确"
break
elif [ "$sum" -ne "$a" ]
then
echo "不正确"
fi
echo "被锁定"
done
[root@test2 opt]# sh test1.sh 
输入密码123
不正确
输入密码123
不正确
输入密码123
不正确
被锁定
作业1
复制代码
[root@test2 opt]# vim test1.sh
for user in $(cat /opt/users.txt)
do
cat /etc/passwd | awk  -F: '{print $1}' | grep "$user"
if [ $? -eq 0 ]
then
echo "用户存在"
​
     mima=$(cat /etc/passwd | grep "$user" | awk -F: '{print $2}')
     if [ "$2" -eq "!!" ] || [ "$2" -eq "-z" ]
     then
      echo "$user 用户无密码"
     echo 123456 | passwd --stdin $user
     echo "$user 用户有密码"
     fi
else
useradd $user
echo 123 | passwd --stdin $user
fi
done
​
[root@test2 opt]# sh test1.sh 
更改用户 wbl1 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 wbl2 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@test2 opt]# cat users.txt
wbl1
wbl2
作业2
复制代码
[root@test2 opt]# vim test2.sh
​
#使用循环语句,将一个0-255的十进制数转化为8位的二进制数
sum=$((RADOM%256))
read -p "输入0-255的整数" sum
for ((i=1;i<=8;i++))
do
a=$(($sum%2))$a
sum=$(($sum/2))
done
echo $a
​
[root@test2 opt]# sh test2.sh 
输入0-255的整数200
11001000
​
相关推荐
沉到海底去吧Go12 小时前
【工具教程】PDF电子发票提取明细导出Excel表格,OFD电子发票行程单提取保存表格,具体操作流程
pdf·excel
开开心心就好13 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
沉到海底去吧Go1 天前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
Abigail_chow2 天前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
xiaohezi3 天前
Rag chunk 之:Excel 文档解析
excel
weixin_472339463 天前
python批量解析提取word内容到excel
python·word·excel
3 天前
Unity与Excel表格交互热更方案
unity·游戏引擎·excel
金融小白数据分析之路4 天前
Excel高级函数使用FILTER、UNIQUE、INDEX
excel
未来之窗软件服务4 天前
Excel表格批量下载 CyberWin Excel Doenlaoder 智能编程-——玄武芯辰
excel·批量下载·仙盟创梦ide·东方仙盟
阿斯加德的IT4 天前
Power Automate: 从Excel 选择列,每200条生成一个CSV文件并保存在sharepoint文档库
低代码·excel