微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐