while [ 表現式 ] do コマンド群 done
$ cat -n dirm 1 #! /bin/bash 2 # @(#) Print contents of sub-directory 3 # 4 [ $# -eq 0 ] && set - `ls` 5 6 while [ $# -gt 0 ] 7 do 8 if [ -d $1 ]; then 9 n=`ls $1 | wc -l ` 10 echo $1 contains $n file 11 fi 12 shift 13 done
$ dirm /usr/include /usr/include contains 266 file $ cd /usr/include $ dirm DPS contains 26 file FLAC contains 12 file FLAC++ contains 5 file GL contains 18 file Magick++ contains 12 file : (省略)この例は、シェル・スクリプト実行時に引き数としてディレクトリ名を渡した時はその ディレクトリについて、引き数を渡さない時は、カレント・ディレクトリのすべての サブディレクトリにあるファイルの数を表示します。