Bash 循环使用指南
0x00 for循环常规
- 数字的依次打印
#!/bin/bash
for ((i=1;i<=10;i++));
do
echo $(expr $i * 3 + 1);
done
- 数字用seq生成
#!/bin/bash
for i in $(seq 1 10)
do
echo $(expr $i * 3 + 1);
done
- {1..10} 中括号生成序列
#!/bin/bash
for i in {1..10}
do
echo $(expr $i * 3 + 1);
done
- awk 生成序列
#!/bin/bash
awk 'BEGIN{for(i=1; i<=10; i++) print i}'
0x01 字符循环相关
- 文件名
#!/bin/bash
for i in `ls`;
do
echo $i is file name! ;
done
- 输入字符,这里用了位置函数
#!/bin/bash
for i in $* ;
do
echo $i is input char! ;
done
- 手动列表
#!/bin/bash
for i in f1 f2 f3 ;
do
echo $i is appointed ;
done
(注:原文为"appoint",但根据上下文,这里可能想表达的是"appointed",即“指定的”)
- for in 变量
#!/bin/bash
list="rootfs usr data data2"
for i in $list;
do
echo $i is appointed ;
done
0x02 路径相关
- 打印路径
#!/bin/bash
for file in /proc/*;
do
echo $file is file path ! ;
done
- in变量中引用通配符
#!/bin/bash
for file in $(ls *.sh)
do
echo $file is file path ! ;
done
以上例子引用自: