LoginSignup
1
1

More than 3 years have passed since last update.

bashでdo...whileする

Posted at

bashには do...while が(たぶん)ないが、while文の条件式に全てを詰め込めば似たようなことができる。
条件式が true を返せばいいので最後に判定を入れとけばいい。

この例では、AWS CloudMapのサービスインスタンスが5つ登録されるまで待機している。
((n < 5)) が true の間 sleep 1 で待機する。

n=0
while
  values=$(
    aws servicediscovery list-instances \
      --service-id srv-xxx \
      --query 'Instances[].Attributes|sort_by(@,&order)|[].value'
  )
  n=$(echo "$values" | jq length)
  (( n < 5 ))
do
  echo "wait..."
  sleep 1
done
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1