shell 循环

简介: shell 循环

循环

文章目录

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

参考:

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

参考:


相关文章
|
16天前
|
存储 运维 Shell
shell中for while until 三种循环的用法
shell编程中,有几种常见的循环结构,包括for循环、while循环和until循环,总的来说,循环shell编程中扮演着至关重要的角色,它们使得自动化任务变得更加容易,提高了效率,并且可以处理各种各样的编程需求。
shell中for while until 三种循环的用法
|
4月前
|
Shell
在Shell脚本中,`for`循环
在Shell脚本中,`for`循环
30 2
|
6月前
|
Shell
shell里的for循环详解
shell里的for循环详解
101 0
|
6月前
|
Shell
shell里的while循环详解
shell里的while循环详解
85 0
|
6月前
|
Shell
shell脚本里的循环
shell脚本里的循环
57 0
|
2月前
|
Shell
shell脚本for循环复杂用法
shell脚本for循环复杂用法
46 5
|
2月前
|
算法 Shell Linux
Linux的shell命令——判断与循环
Linux的shell命令——判断与循环
39 1
|
4月前
|
Shell
在Shell(如Bash)中,`while`循环
在Shell(如Bash)中,`while`循环
44 2
|
4月前
|
Shell
shell的for循环使用
shell的for循环使用
24 0
|
4月前
|
前端开发 Shell PHP
shell(八)循环
循环这个东西还是很重要的。比如说,给公司前端小姐姐秀一秀在控制台输出个爱心啥的啊,都是需要使用到循环的。
32 0