shell中for循环的几个常用写法

数字性循环

#!/bin/bash

for ((i = 1; i <= 10; i++)); do
    echo $(expr $i \* 3 + 1)
done
#!/bin/bash

for i in $(seq 1 10); do
    echo $(expr $i \* 3 + 1)
done
#!/bin/bash

for i in {1..10}; do
    echo $(expr $i \* 3 + 1)
done
#!/bin/bash

awk 'BEGIN{for(i=1; i<=10; i++) print i}'

字符性循环

Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下:

array_name=(value1 value2 ... valuen)
#!/bin/bash

list="rootfs usr data data2"
for i in $list; do
    echo $i is appoint
done
#!/bin/bash

for i in $*; do
    echo $i is input chart\!
done
#!/bin/bash

for i in f1 f2 f3; do
    echo $i is appoint
done
#!/bin/bash

list="rootfs usr data data2"
for i in $list; do
    echo $i is appoint
done

路径查找

#!/bin/bash

for file in /proc/*; do
    echo $file is file path \!
done
#!/bin/bash

for file in $(ls *.sh); do
    echo $file is file path \!
done

 

上一篇:linux刷新查看文件夹内容


下一篇:Hadoop中的jobhistory配置与启动停止