ループ文 †基本的な部分だけ 特定回数(回数指定、終わるまで) †for文 †スクリプト # cat for.sh #!/bin/bash DATA=`ls -1 ${HOME}/` for i in ${DATA} do echo $i done 実行結果 # sh for.sh Desktop anaconda-ks.cfg install.log install.log.syslog
# cat for_times.sh #!/bin/bash # コメントアウト部分でもseqで括っても同意 #for i in 0 1 2 3 4 for i in `seq 0 4` do echo $i done 実行結果 # sh for_times.sh 0 1 2 3 4
while †スクリプト # cat while.sh #!/bin/sh while read line do echo "${line}" done <$1 実行結果 # sh while.sh text 0 1 2 3 4
# cat while_times.sh #!/bin/bash count=0 while [ ${count} -le 4 ] do echo "${count}" count=`expr $count + 1` done 実行結果 # sh while_times.sh 0 1 2 3 4
1足していく #!/bin/bash i=1 while [ $i -lt 11 ] do echo $i i=$(($i+1)) done 外部ファイルを読み込んで計算 †別ファイルに吐き出したファイルの計算をしたい場合。 # cat int.txt このファイルを全部足した結果が欲しい場合は、 #!/bin/bash while read line do sum=`expr $sum + $line` done<./int.txt echo $sum コマンドラインでwhileを使う(ワンライナー) †# while :; do /etc/init.d/postgresql restart ; sleep 1; done |