シェルスクリプト初心者のつまづきポイント2

もう1つ。

よくある例としてRestAPIをたたいた結果コードを変数に格納する。

str=`curl -sS -L -XPOST -H "Content-Type: application/json" http://${user}:${pass}@192.168.1.1/hoge/fuga?piyo=true`
if [ $? -ne 0 ]; then
  return 1
fi
statusCode=`expr "${str}" : ".*\"statusCode\":\([0-9]*\),.*"`

 

ちなみに、あるバッチ処理APIからスタートさせ、その処理が終了するのをまつ場合は、定期的にポーリングするしかないのかな。

 

check() {
  echo "Waiting..."
  until [ $status != "FINISHED" ]
  do
    sleep 5s
    getStatus
    if [ $? -eq 0 ]; then
      echo "OK: Backup job was successfully done."
      return 0
    elif [ $? -eq 1 ]; then
      echo "NG: Backup job was fail."
      echo ${str}
      return 1
    fi
  done
}