このエントリーをはてなブックマークに追加


whileループのサンプル

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


関連記事

whileで無限ループ

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の条件ループ

whileの条件ループについて以下に記します。
判定方法は以下のif文を利用したサンプルと同様になっています。

while条件ループ - 数値比較編

下表に記す文字列により、数値比較ができます。

-eq等しいかをチェック 数値 = 数値equal
-ne異なるかをチェック 数値 ≠ 数値not equal
-lt数値 < 数値をチェックless than
-le数値 ≦ 数値をチェックless than or equal
-gt数値 > 数値をチェックgreater than
-ge数値 ≧ 数値をチェックgreater than or equal

-eq のサンプルスクリプト (数値 = 数値)

以下のサンプルスクリプトは、変数$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 のサンプルスクリプト (数値 ≠ 数値)

以下のサンプルスクリプトは、変数$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 のサンプルスクリプト (数値 < 数値)

以下のサンプルスクリプトは、変数$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 のサンプルスクリプト (数値 ≦ 数値)

以下のサンプルスクリプトは、変数$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 のサンプルスクリプト (数値 > 数値)

以下のサンプルスクリプトは、変数$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 のサンプルスクリプト (数値 ≧ 数値)

以下のサンプルスクリプトは、変数$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条件ループ - 文字列比較編

以下に文字列比較によるwhileループのサンプルを記述します。

文字列による比較 (not equal)

変数$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

文字列による比較 (equal)

$ 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(つまり空)の場合は真

-z のサンプルスクリプト

変数$sが空の間、whileループ内を処理します。

#!/bin/bash

s=''
while [ -z "$s" ]
do
  echo "s : $s"
  s=${s}a
done
  • -z のサンプルスクリプト実行結果
    $ ./str-n.sh 
    s : 

-n のサンプルスクリプト

変数$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

複数の条件式にする場合

下表に記す文字列により、複数の条件式を記述することができます。

-aand 条件式 -a 条件式 ...
-oor 条件式 -o 条件式 ...

-a のサンプルスクリプト (and)

以下のサンプルスクリプトは以下の条件式が成立する間、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
  • -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)

以下のサンプルスクリプトは以下の条件式が成立する間、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
  • -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


添付ファイル: fileeq.sh 1052件 [詳細] filege.sh 731件 [詳細] filele.sh 892件 [詳細] filelt.sh 989件 [詳細] filene.sh 888件 [詳細] fileor.sh 767件 [詳細] filestr-n.sh 771件 [詳細] filestr-ne.sh 880件 [詳細] fileand.sh 775件 [詳細] filegt.sh 817件 [詳細] filestr-eq.sh 815件 [詳細] filestr-z.sh 780件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-11-22 (火) 12:57:15