语法一:
for 变量 in 值1 值2 值3...
do
程序
done
示例1:打印时间
#!/bin/bash
#打印时间
for time in morning noon afternoon evening
do
echo "this time is $time"
done
示例2:批量解压缩文件
#!/bin/bash
#批量解压缩文件,如文件在/Users/chenzhitao/code/linuxtest
cd /Users/chenzhitao/code/linuxtest
ls *.tar.gz > ls.log
for item in $(cat ls.log)
do
tar -zxf $item &>null
done
rm -rf /Users/chenzhitao/code/linuxtest/ls.log
2、语法二
格式:
for ((初始值;循环条件;变量变化))
do
程序
done
示例1:从1累加到100
#!/bin/bash
#从1加到100
sum=0
for((i=1;i<=100;i++))
do
sum=$(($sum+$i))
done
echo "result is $sum"
网友评论