シェルスクリプトのwhileループのサンプルを以下に記します。 サンプル例としては、無限ループ、ある値までの条件ループについて記述します。 尚、利用したシェルはbashになります。
whileで無限ループを実現するには、:(コロン)または、trueを条件として記述します。
以下の2つのサンプルソースは同じです。
#!/bin/bash while : do echo Hello world sleep 1 done
#!/bin/bash while true do echo Hello world sleep 1 done
条件として:(コロン)とtrueを利用しています。
意味を知りたい場合は、以下の記事を参考にしてください。
whileの条件ループについて以下に記します。
判定方法は以下のif文を利用したサンプルと同様になっています。
下表に記す文字列により、数値比較ができます。
| -eq | 等しいかをチェック 数値 = 数値 | equal |
| -ne | 異なるかをチェック 数値 ≠ 数値 | not equal |
| -lt | 数値 < 数値をチェック | less than |
| -le | 数値 ≦ 数値をチェック | less than or equal |
| -gt | 数値 > 数値をチェック | greater than |
| -ge | 数値 ≧ 数値をチェック | greater than or equal |
以下のサンプルスクリプトは、変数$iが0の時にwhileループ内を実行します。
変数$jがループ毎に+1され、5になったときに$iが+1されwhileループの条件が成立せずにwhileループを抜けます。
#!/bin/bash
# -eq : equal
i=0
j=0
while [ $i -eq 0 ]
do
j=`expr $j + 1`
if [ $j -eq 5 ]; then
i=`expr $i + 1`
fi
echo "(-eq : equal) i=$i, j=$j"
done
$ ./eq.sh (-eq : equal) i=0, j=1 (-eq : equal) i=0, j=2 (-eq : equal) i=0, j=3 (-eq : equal) i=0, j=4 (-eq : equal) i=1, j=5
以下のサンプルスクリプトは、変数$iが5以外の時にwhileループ内を実行します。
ループ毎に$iが+1され、$iが5になった時点でwhileループの条件が成立せずにwhileループを抜けます。
#!/bin/bash # -ne : not equal i=0 while [ $i -ne 5 ] do i=`expr 1 + $i` echo "(-ne : not equal) i=$i" done
$ ./ne.sh (-ne : not equal) i=1 (-ne : not equal) i=2 (-ne : not equal) i=3 (-ne : not equal) i=4 (-ne : not equal) i=5
以下のサンプルスクリプトは、変数$iが5未満の時にwhileループ内を実行します。
ループ毎に$iが+1され、$iが5になった時点でwhileループの条件が成立せずにwhileループを抜けます。
#!/bin/bash # -lt : less than i=0 while [ $i -lt 5 ] # $i < 5 do echo "(-lt : less than) : $i" i=`expr $i + 1` done
$ ./lt.sh (-lt : less than) : 0 (-lt : less than) : 1 (-lt : less than) : 2 (-lt : less than) : 3 (-lt : less than) : 4
以下のサンプルスクリプトは、変数$iが5未満の時にwhileループ内を実行します。
ループ毎に$iが+1され、$iが5より大きな値になった時点でwhileループの条件が成立せずにwhileループを抜けます。
#!/bin/bash # -le : less than or equal i=0 while [ $i -le 5 ] # $i <= 5 do echo "(-le : less than or equal) : $i" i=`expr $i + 1` done
$ ./le.sh (-le : less than or equal) : 0 (-le : less than or equal) : 1 (-le : less than or equal) : 2 (-le : less than or equal) : 3 (-le : less than or equal) : 4 (-le : less than or equal) : 5
以下のサンプルスクリプトは、変数$iに10を初期値として代入し、$iが5より大きい値の時にwhileループ内を実行します。
ループ毎に$iが-1され、$iが5になった時点でwhileループの条件が成立せずにwhileループを抜けます。
#!/bin/bash # -gt : greater than i=10 while [ $i -gt 5 ] # $i > 5 do echo "(-gt : greater than) : $i" i=`expr $i - 1` done
$ ./gt.sh (-gt : greater than) : 10 (-gt : greater than) : 9 (-gt : greater than) : 8 (-gt : greater than) : 7 (-gt : greater than) : 6
以下のサンプルスクリプトは、変数$iに10を初期値として代入し、$iが5以上の時にwhileループ内を実行します。
ループ毎に$iが-1され、$iが4になった時点でwhileループの条件が成立せずにwhileループを抜けます。
#!/bin/bash # -ge : greater than or equal i=10 while [ $i -ge 5 ] # $i >= 5 do echo "(-gt : greater than) : $i" i=`expr $i - 1` done
$ ./ge.sh (-gt : greater than) : 10 (-gt : greater than) : 9 (-gt : greater than) : 8 (-gt : greater than) : 7 (-gt : greater than) : 6 (-gt : greater than) : 5
以下に文字列比較によるwhileループのサンプルを記述します。
変数$sが='aaaaa'になるまでwhileループ内を実行します。
#!/bin/bash
s='a'
while [ $s != 'aaaaa' ]
do
echo "str.sh s=$s"
s="${s}a"
done
$ ./str-ne.sh str.sh s=a str.sh s=aa str.sh s=aaa str.sh s=aaaa
$ cat ./str-eq.sh
#!/bin/bash
s='a'
while [ $s = 'a' ]
do
echo "str.sh s=$s"
s="${s}a"
done
$ ./str-eq.sh str.sh s=a
下表に記す文字列により、文字列長の確認ができます。
| -n | 文字列の長さが0以外(つまり空ではない)の場合は真 |
| -z | 文字列の長さが0(つまり空)の場合は真 |
変数$sが空の間、whileループ内を処理します。
#!/bin/bash
s=''
while [ -z "$s" ]
do
echo "s : $s"
s=${s}a
done
$ ./str-n.sh s :
変数$sを1文字ずつcutコマンドで削除しが変数$sが空になるまで、whileループ内を処理します。
#!/bin/bash
s="linux"
len=`expr length $s`
while [ -n "$s" ]
do
echo "s : $s"
if [ $len -ne 0 ]; then
s=`echo $s | cut -c 1-$len`
len=`expr $len - 1`
else
s=''
fi
done
$ ./str-n.sh s : linux s : linux s : linu s : lin s : li s : l
下表に記す文字列により、複数の条件式を記述することができます。
| -a | and 条件式 -a 条件式 ... |
| -o | or 条件式 -o 条件式 ... |
以下のサンプルスクリプトは以下の条件式が成立する間、whileループ内を処理します。
$a < 5 and $b < 10
#!/bin/bash a=0 b=1 while [ $a -lt 5 -a $b -lt 10 ] # $a < 5 and $b < 10 do echo "a=$a, b=$b" a=`expr $a + 1` b=`expr $b + 1` done
$ ./and.sh a=0, b=1 a=1, b=2 a=2, b=3 a=3, b=4 a=4, b=5
以下のサンプルスクリプトは以下の条件式が成立する間、whileループ内を処理します。
$a < 5 or $b < 10
#!/bin/bash a=0 b=1 while [ $a -lt 5 -o $b -lt 10 ] # $a < 5 or $b < 10 do echo "a=$a, b=$b" a=`expr $a + 1` b=`expr $b + 1` done
$ ./or.sh a=0, b=1 a=1, b=2 a=2, b=3 a=3, b=4 a=4, b=5 a=5, b=6 a=6, b=7 a=7, b=8 a=8, b=9