CentOS 9 Shellにwhile文とbreak文のサンプル
環境
CentOS Stream release 9
構文
while :
do
if [条件式]; then
break
fi
処理コード
done
while :
do
if [条件式]; then
break
fi
処理コード
done
while : do if [条件式]; then break fi 処理コード done
条件式がtrueの間、処理を繰り返します。
条件式がfalseの場合、処理を終了します。
比較演算子 説明
a -eq b aとbの値は等しい
使用例
#!/bin/bash
i=10
while :
do
if [ $i -eq 13 ]; then
break
fi
echo $i
((i++))
done
#!/bin/bash
i=10
while :
do
if [ $i -eq 13 ]; then
break
fi
echo $i
((i++))
done
#!/bin/bash i=10 while : do if [ $i -eq 13 ]; then break fi echo $i ((i++)) done
実行結果
# sh test04.sh
10
11
12
# sh test04.sh
10
11
12
# sh test04.sh 10 11 12