Linux bash for循环
时间:2020-01-09 10:41:14 来源:igfitidea点击:
问题描述:如何在Linux操作系统下使用bash进行循环?
答:bash for循环用于处理文件组或用于其他自动化目的。
Linux bash for循环语法
for variable in list do echo $variable done
要遍历目录中的所有文件,请执行:
for file in * do echo $file done
要打印数字1至10,请执行:
for n in {1..10}
do
echo $n
done
Shell脚本使用for循环打印奇数和偶数
#!/bin/bash
for n in {1..10}
do
out=$(( $n % 2 ))
if [ $out -eq 0 ]
then
echo "$n is even number"
else
echo "$n is ODD number"
fi
done

