Linux shell之三种For 循环结构
1. 列表for 循环
for variable in {list}
do
command
command
done
cat for_exam1.sh
#!/bin/bash
for varible1 in 1 2 3 4 5
do
echo "hello ,welcome $varible1 times"
done
执行:./for_exam1.sh
hello ,welcome 1 times
hello ,welcome 2 times
hello ,welcome 3 times
hello ,welcome 4 times
hello ,welcome 5 times
注:for 循环支持缩略计数
for varible1 in {1..5} #只能用两个点做为省略号
do
echo "hello ,welcome $varible1 times"
done
#for循环支持按规定的步数跳跃
cat for_exam3.sh
#!/bin/bash
#对sum赋初值
sum=0
#使用列表for循环计算1~100内所有的奇数之和,并将值保存在sum中
for i in {1..100..2}
do
let "sum+=i"
done
#输出sum值
echo "sum=$sum"
#for循环用seq命令支持按步数递增
#!/bin/bash
#对sum赋初值
sum=0
#使用列表for循环计算1~100内所有的奇数之和,并将值保存在sum中
for i in $(seq 1 2 100)
do
let "sum+=i"
done
#输出sum值
echo "sum=$sum"
#当前目录下所有文件的显示
#!/bin/bash
#ls显示当前目录下所有文件的显示
for file in $( ls )
do
echo "file:$file"
done