论坛首页 综合技术论坛

Unix Shell 的 for 循环

浏览 6132 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-29  
这里有三种形式的for循环:
首先生成一个列表list,然后循环list:
for variable in list
do
   echo $variable
done


通配符循环,列出目录中所有文件,你如果使用a*,则是列出所有a开头的文件:
for file in *
do
   echo $file
done


指定次数循环
for n in {1..10}
do
   echo $n
done


下面是个能够执行的示例:
#!/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
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics