shell--while循环

1.基本语法

bash 复制代码
while [ 条件表达式 ]
do
	语句
	语句
done

示例:循环输出 1~10这几个数

bash 复制代码
[root@openEuler ~]# cat while1.sh 
#!/bin/bash

i=1
while [ $i -le 10 ]
do
	echo $i
	let i++
done

示例:使用 exec 读取指定文件的内容并循环输出。

bash 复制代码
# 第一步创建文件及内容
[root@openEuler ~]# cat > myfile << EOF
> open
> openlab
> openlab123
> linux
> readhat
> EOF
[root@openEuler ~]# cat myfile 
open
openlab
openlab123
linux
readhat
# 第二步:编写脚本来实现文件读取并循环输出
[root@openEuler ~]# cat while2.sh 
#!/bin/bash

exec < myfile

while read line
do
	echo $line
done
[root@openEuler ~]# bash while2.sh 
open
openlab
openlab123
linux
readhat

使用另一种方式来读取文件:

bash 复制代码
[root@openEuler ~]# cat while3.sh 
#!/bin/bash

while read line
do
	echo $line
done < myfile
[root@openEuler ~]# bash while3.sh 
open
openlab
openlab123
linux
readhat

2.无限循环

在 while 的表达式中,可以指定以下几个特殊值:

  • true 它会一直循环,而且它的状态返码是 0

  • false 它不做任何事,表示成功,状态码为 0

  • : 它的作用与 true 相同,都是进行无限循环

示例:

bash 复制代码
[root@openEuler ~]# while true ; do echo 123123 ; done   #会一直循环

[root@openEuler ~]# while false ; do echo 123123 ; done
[root@openEuler ~]# echo $?
0

[root@openEuler ~]# while : ; do echo 123123 ; done

3.使用示例

bash 复制代码
[root@openEuler ~]# cat while4.sh 
#!/bin/bash

price=$[ $RANDOM % 100 ]
time=0

while true
do
	read -p 'Please enter product price [0-99]: ' input
	let time++
	if [ $input -eq $price ]; then
		echo 'Good luck, you guessed it.'
		echo 'You have guessed $time times.'
		exit 0
	elif [ $input -gt $price ]; then
		echo "$input is to high"
	else
		echo "$input is to low"
	fi
	if [ $time -eq 5 ]; then
		echo "You have guessed is 5 times. exit"
		exit 1
	fi
done
[root@openEuler ~]# bash while4.sh 
Please enter product price [0-99]: 50
50 is to low
Please enter product price [0-99]: 80
80 is to high
Please enter product price [0-99]: 70
70 is to high
Please enter product price [0-99]: 60
60 is to low
Please enter product price [0-99]: 65
65 is to low
You have guessed is 5 times. exit
[root@openEuler ~]# 

示例:使用while读取文件

bash 复制代码
# 1. 创建文件
[root@openEuler ~]# cat ips
192.168.72.131  22
192.168.72.132  23
192.168.72.133  22

# 2. 编写脚本 
[root@openEuler ~]# cat while6.sh 
#!/bin/bash

while read line
do
	IP=`echo $line|cut -d" " -f1`   # 也可以使用awk来实现,如:IP=`echo $line|awk '{print $1}'`
	PORT=$(echo $line|cut -d " " -f 2)
	echo "IP:$IP, PORT:${PORT}"
done < ips

# 3. 运行测试
[root@openEuler ~]# bash while6.sh 
IP:192.168.72.131, PORT:22
IP:192.168.72.132, PORT:23
IP:192.168.72.133, PORT:22
相关推荐
阿正的梦工坊13 分钟前
深入解析 Chrome 浏览器的多进程架构:标签页是进程还是线程?(中英双语)
linux·服务器·前端·chrome·架构·unix
今天也要努力搬砖20 分钟前
通信易懂唠唠SOME/IP——SOME/IP消息格式
服务器·网络·tcp/ip·some/ip
千夜啊20 分钟前
k8s集群
运维
稚辉君.MCA_P8_Java1 小时前
SpringAI 人工智能
大数据·linux·人工智能·分布式·spring
CodingCarrot1 小时前
自动化软件测试的基本流程
运维·自动化
时差freebright1 小时前
【Linux系统】信号:信号保存 / 信号处理、内核态 / 用户态、操作系统运行原理(中断)
linux·运维·信号处理
利明的博客2 小时前
【流媒体】搭建流媒体服务器
运维·服务器
成都被卷死的程序员2 小时前
从0开始,来看看怎么去linux排查Java程序故障
java·linux·运维·服务器
山海青风2 小时前
OpenAI 实战进阶教程 - 第六节: OpenAI 与爬虫集成实现任务自动化
运维·人工智能·爬虫·python·自动化·nlp·beautifulsoup
学问小小谢2 小时前
第21节课:前端构建工具—自动化与模块化的利器
运维·前端·学习·计算机·自动化·电脑·硬件工程