■ はじめに
https://dk521123.hatenablog.com/entry/2015/02/20/001600
より分冊。 シェルのループについて、メモっておく。
目次
【1】while 1)構文 【2】for 1)構文 2)サンプル 例1:規定回数ループする 例2:拡張子「.sql」のファイル一覧表示 【3】until 1)構文 【4】その他事項 1)continue 2)break
【1】while
* 条件が真である間、処理を繰り返す
1)構文
while [ 条件 ] do 処理 done
【2】for
1)構文
for 変数 in 引数1 引数2 … do 処理 done
2)サンプル
例1:規定回数ループする
#!/bin/bash for((i=0; i<10; i++)) do echo ${i} done
https://hacknote.jp/archives/54054/
例2:拡張子「.sql」のファイル一覧表示
#!/bin/sh for file in `ls ./*.sql`; do echo "Result = ${file}." done
【3】until
* 条件が偽である間、処理を繰り返す
1)構文
until [ 条件 ] do 処理 done
【4】その他事項
* プログラムである以下の事項は、シェルでもできる。
1)continue
https://xtech.nikkei.com/it/article/COLUMN/20060228/231135/
#!/bin/bash for((i=0; i<10; i++)) do if [ ${i} -ne 5 ]; then echo "Not 5 is ${i}" continue fi echo "GO GO GO ${i}" done
出力結果
Not 5 is 0 Not 5 is 1 Not 5 is 2 Not 5 is 3 Not 5 is 4 GO GO GO 5 Not 5 is 6 Not 5 is 7 Not 5 is 8 Not 5 is 9
2)break
https://xtech.nikkei.com/it/article/COLUMN/20060228/231133/
#!/bin/bash for((i=0; i<10; i++)) do if [ ${i} -ne 5 ]; then echo "Not 5 is ${i}" break fi echo "GO GO GO ${i}" done
出力結果
Not 5 is 0
関連記事
シェル ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/10/23/005406
シェル ~ 基本編・構文全般 ~
https://dk521123.hatenablog.com/entry/2015/02/20/001600
シェル ~ 基本編・条件分岐 if / case ~
https://dk521123.hatenablog.com/entry/2015/05/01/000043