shell 循环
  TEZNKK3IfmPf 2023年11月14日 27 0

循环

 

文章目录

  • 循环
    • 1. while
      • 1.1 大小判断
      • 1.2 100相加
      • 1.3 无限循环
      • 1.4 多列输出
      • 1.5 读行
    • 2. for
    • 3. break
    • 4. continue

 


1. while

示例

#!/bin/bash

#基础

i=1

while [ $i -le 5 ]

do 
	echo $i
	let i++
done

输出:

$ sh while.sh
1
2
3
4
5

1.1 大小判断

i=10
while [ $i -ne 0 ]
do
	i=`expr $i - 1`
	echo $i
# sleep 1
done

输出:

sh while2.sh
9
8
7
6
5
4
3
2
1
0

1.2 100相加

#!/bin/bash
#计算1+2+...+100的值

i=1
sum=0
while (( i<101 ))
do
    (( sum=sum+i ))
    (( i++ ))
done

echo "The total value is $sum"

输出:

$ bash while.sh
The total value is 5050

1.3 无限循环

while true
do
    uptime
    sleep 1
done

输出:

sh while.sh

10:20:54 up 4 min,  1 user,  load average: 0.05, 0.19, 0.12
10:20:56 up 4 min,  1 user,  load average: 0.05, 0.19, 0.12
10:20:57 up 4 min,  1 user,  load average: 0.05, 0.19, 0.12
10:20:58 up 4 min,  1 user,  load average: 0.05, 0.19, 0.12

参考:

  • Shell中while循环的陷阱, 变量实效,无法赋值变量

1.4 多列输出

$ cat test.txt 
xiaoming 122458930 山西
dali  343536546232 新疆
lihua  35354646565 广东

$ cat while2.sh 
#!/bin/bash
while read  name phone origin
do
  echo "$name : $phone : $origin"
done < test.txt

执行:

$ bash while2.sh 
xiaoming : 122458930 : 山西
dali : 343536546232 : 新疆
lihua : 35354646565 : 广东

1.5 读行

$ cat test 
1
22
33 3
  • while-read.sh
#!/bin/bash
# while-read: read lines from a file
count=0
while read; do
	printf "%d %s\n" $REPLY
	count=$(expr $count + 1)
done <$1

执行:

$ bash  wile_read.sh test
1 
22 
33 3
count:3

2. for

格式:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

写成一行:

for var in item1 item2 ... itemN; do command1; command2… done;
  • in列表可以包含替换、字符串和文件名。
  • in列表是可选的,如果不用它,for循环使用命令行的位置参数。

例如,顺序输出当前列表中的数字:

#!/bin/bash
 
for varible1 in {
     
       1..5}
#for varible1 in 1 2 3 4 5
do
     echo "Hello, Welcome $varible1 times "
done

计算1~100内所有的奇数之和

#!/bin/bash
sum=0
 
for i in {
     
       1..100..2}
do
    let "sum+=i"
done
    
echo "sum=$sum"

执行:

$ bash while1.sh 
sum=2500

通过i的按步数2不断递增,计算sum值为2500。同样可以使用seq命令实现按2递增来计算1~100内的所有奇数之和,for i in $(seq 1 2 100),seq表示起始数为1,跳跃的步数为2,结束条件值为100。

#!/bin/bash
sum=0
 
for i in $(seq 1 2 100)
do
    let "sum+=i"
done
    
echo "sum=$sum"
[root@localhost control]# bash while2.sh 
sum=2500

查看当前目录下文件以及文件夹

#!/bin/bash
 
for file in $( ls )
#for file in *
do
   echo "file: $file"
done

for循环列表参数

#!/bin/bash
echo "number of arguments is $#"
echo "What you input is: "
for argument
do
    echo "$argument"
done
#!/bin/bash
for((integer = 1; integer <= 5; integer++))
do
    echo "$integer"
done

3. break

break 跳出整个循环

$ cat break1.sh 
#!/bin/bash
for ((a=1; a<=3; a++))
do
   echo "outer loop: $a"
   for ((b=1; b<=4; b++))
   do
     if [ $b -eq 3 ]
     then
       break
     fi
   echo "inter loop: $b"
   done
done

执行:

$ bash break1.sh
outer loop: 1
inter loop: 1
inter loop: 2
outer loop: 2
inter loop: 1
inter loop: 2
outer loop: 3
inter loop: 1
inter loop: 2

4. continue

continue跳出本次循环

$ cat continue1.sh 
#!/bin/bash
for ((a=1; a<=3; a++))
do
   echo "outer loop: $a"
   for ((b=1; b<=4; b++))
   do
     if [ $b -eq 3 ]
     then
       continue  
     fi
   echo "inter loop: $b"
   done
done

执行:

$ bash continue1.sh
outer loop: 1
inter loop: 1
inter loop: 2
inter loop: 4
outer loop: 2
inter loop: 1
inter loop: 2
inter loop: 4
outer loop: 3
inter loop: 1
inter loop: 2
inter loop: 4

参考:

  • Looping Statements | Shell Script
  • Bash 脚本教程:循环
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年04月12日   36   0   0 shellbash
  TEZNKK3IfmPf   2024年05月31日   37   0   0 遍历shell
  TEZNKK3IfmPf   2024年04月19日   65   0   0 shellphp
  TEZNKK3IfmPf   21天前   31   0   0 shell
  TEZNKK3IfmPf   2024年05月31日   40   0   0 linuxshell
TEZNKK3IfmPf