shell编程--for循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell编程--for循环相关的知识,希望对你有一定的参考价值。

脚本

[[email protected] shell]# vim for1.sh

#!/bin/bash
for i in `seq 1 6`
do
      echo $i
done

执行结果

[[email protected] shell]# sh for1.sh
1
2
3
4
5
6

脚本

[[email protected] shell]# vim for2.sh

#!/bin/bash
sum=0
for i in `seq 1 6`
do
       echo "$sum + $i"
       sum=$[$sum+$i]
       echo $sum
done

执行结果

[[email protected] shell]# sh for2.sh
0 + 1
1
1 + 2
3
3 + 3
6
6 + 4
10
10 + 5
15
15 + 6
21

for循环是以空格或者回车来作为分割符来循环的

[[email protected] shell]# mkdir test
[[email protected] shell]# cd test
[[email protected] test]# touch 1 2
[[email protected] test]# touch 3\ 4.txt
[[email protected] test]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 4月  20 08:13 1
-rw-r--r-- 1 root root 0 4月  20 08:13 2
-rw-r--r-- 1 root root 0 4月  20 08:13 3 4.txt
[[email protected] test]# for i in `ls ./ `; do echo $i ; done
1
2
3
4.txt

以上是关于shell编程--for循环的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本编程循环之for/while/untill循环

shell编程中的for循环如何实现

Shell编程Shell中for循环while循环until循环语句

Shell编程Shell中for循环while循环until循环语句

shell编程—for循环

shell编程中for/while循环命令