#navi(../)

* whileループのサンプル [#b532e1a3]

シェルスクリプトのwhileループのサンプルを以下に記します。
サンプル例としては、無限ループ、ある値までの条件ループについて記述します。
尚、利用したシェルはbashになります。

#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)

* 関連記事 [#x1d1bb0c]
-[[while内部の変数が反映されない場合の対処>逆引きシェルスクリプト/while内部の変数が反映されない場合の対処]]
- [[シェルで無限ループ>逆引きシェルスクリプト/シェルで無限ループ]]
- [[forループのサンプル>逆引きシェルスクリプト/forループのサンプル]]

* whileで無限ループ [#rf89d5f5]
whileで無限ループを実現するには、:(コロン)または、trueを条件として記述します。~
以下の2つのサンプルソースは同じです。

- :を利用したサンプル
 #!/bin/bash
 
 while :
 do
   echo Hello world
   sleep 1
 done

- trueを利用したサンプル
 #!/bin/bash
 
 while true
 do
   echo Hello world
   sleep 1
 done

条件として:(コロン)とtrueを利用しています。~
意味を知りたい場合は、以下の記事を参考にしてください。
- [[シェルで無限ループ>逆引きシェルスクリプト/シェルで無限ループ]]

* whileの条件ループ [#u0535f79]
whileの条件ループについて以下に記します。~
判定方法は以下のif文を利用したサンプルと同様になっています。
- [[逆引きシェルスクリプト/if文のサンプルシェルスクリプト・数値比較編]]

* while条件ループ - 数値比較編 [#v2fb4e0d]
下表に記す文字列により、数値比較ができます。
|-eq|等しいかをチェック 数値 = 数値|''eq''ual|
|-ne|異なるかをチェック 数値 ≠ 数値|''n''ot ''e''qual|
|-lt|数値 < 数値をチェック|''l''ess ''t''han|
|-le|数値 ≦ 数値をチェック|''l''ess than or ''e''qual|
|-gt|数値 > 数値をチェック|''g''reater ''t''han|
|-ge|数値 ≧ 数値をチェック|''g''reater than or ''e''qual|

** -eq のサンプルスクリプト (数値 = 数値) [#ia0dce16]
#ref(eq.sh)
以下のサンプルスクリプトは、変数$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 のサンプルスクリプト実行結果
 $ ./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

** -ne のサンプルスクリプト (数値 ≠ 数値) [#p3294c92]
#ref(ne.sh)

以下のサンプルスクリプトは、変数$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 のサンプルスクリプト実行結果
 $ ./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

** -lt のサンプルスクリプト (数値 < 数値) [#m592d365]
#ref(lt.sh)
以下のサンプルスクリプトは、変数$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 のサンプルスクリプト実行結果
 $ ./lt.sh 
 (-lt : less than) : 0
 (-lt : less than) : 1
 (-lt : less than) : 2
 (-lt : less than) : 3
 (-lt : less than) : 4

** -le のサンプルスクリプト (数値 ≦ 数値) [#i9d6272d]
#ref(le.sh)
以下のサンプルスクリプトは、変数$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 のサンプルスクリプト実行結果
 $ ./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

** -gt のサンプルスクリプト (数値 > 数値) [#t4458a0f]
#ref(gt.sh)
以下のサンプルスクリプトは、変数$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 のサンプルスクリプト実行結果
 $ ./gt.sh 
 (-gt : greater than) : 10
 (-gt : greater than) : 9
 (-gt : greater than) : 8
 (-gt : greater than) : 7
 (-gt : greater than) : 6

** -ge のサンプルスクリプト (数値 ≧ 数値) [#u9ca56e3]
#ref(ge.sh)
以下のサンプルスクリプトは、変数$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 のサンプルスクリプト実行結果
 $ ./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条件ループ - 文字列比較編 [#vaa9e9b0]
以下に文字列比較によるwhileループのサンプルを記述します。

** 文字列による比較 (not equal) [#x93a76c6]
変数$sが='aaaaa'になるまでwhileループ内を実行します。
#ref(str-ne.sh)
 #!/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

** 文字列による比較 (equal) [#g1cda503]
#ref(str-eq.sh)
 $ 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

* 文字列長さの比較 [#zfe69e3f]
下表に記す文字列により、文字列長の確認ができます。
|-n|文字列の長さが0以外(つまり空ではない)の場合は真|
|-z|文字列の長さが0(つまり空)の場合は真|

* -z のサンプルスクリプト [#r48168cf]
#ref(str-z.sh)
変数$sが空の間、whileループ内を処理します。~
 #!/bin/bash
 
 s=''
 while [ -z "$s" ]
 do
   echo "s : $s"
   s=${s}a
 done

- -z のサンプルスクリプト実行結果
 $ ./str-n.sh 
 s : 

* -n のサンプルスクリプト [#c24dea0b]
#ref(str-n.sh)
変数$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

- -n のサンプルスクリプト実行結果
 $ ./str-n.sh 
 s : linux
 s : linux
 s : linu
 s : lin
 s : li
 s : l

* 複数の条件式にする場合 [#c33d99c6]
下表に記す文字列により、複数の条件式を記述することができます。
|-a|and 条件式 -a 条件式 ...|
|-o|or 条件式 -o 条件式 ...|

* -a のサンプルスクリプト (and) [#v43df001]
以下のサンプルスクリプトは以下の条件式が成立する間、whileループ内を処理します。
 $a < 5 and $b < 10
#ref(and.sh)
 #!/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

- -a のサンプルスクリプト実行結果
 $ ./and.sh 
 a=0, b=1
 a=1, b=2
 a=2, b=3
 a=3, b=4
 a=4, b=5

* -o のサンプルスクリプト (or) [#me956a55]
以下のサンプルスクリプトは以下の条件式が成立する間、whileループ内を処理します。
 $a < 5 or $b < 10
#ref(or.sh)
 #!/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

- -o のサンプルスクリプト実行結果
 $ ./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

#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS