shell 中常见的for循环的用法

0x00 for循环常规

  1. 数字的依次打印
#!/bin/bash

for ((i=1;i<=10;i++));
do 
echo $(expr $i \* 3 + 1);
done
  1. 数字用seq生成
#!/bin/bash

for i in $(seq 1 10)
do 
echo $(expr $i \* 3 + 1);
done
  1. {1..11} 中括号生成序列
#!/bin/bash

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

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

0x01 字符循环相关

  1. 文件名
#!/bin/bash

for i in `ls`;
do 
echo $i is file name\! ;
done
  1. 输入字符,这里用了位置函数
#!/bin/bash

for i in $* ;
do
echo $i is input chart\! ;
done
  1. 手动列表
#!/bin/bash

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

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

0x02 路径相关

  1. 打印路径
#!/bin/bash

for file in /proc/*;
do
echo $file is file path \! ;
done
  1. in变量中引用通配符
#!/bin/bash

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

文章中的例子引用自

https://blog.csdn.net/babyfish13/article/details/52981110

上一篇
下一篇