等到 bash 脚本中满足条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51974418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:00:32  来源:igfitidea点击:

Wait until a condition is met in bash script

bashshellaws-clissmaws-ssm

提问by chandra

until [ $(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text) = *"InProgress"* ];
do
  echo "Automation is running......"
  sleep 1m
done
status=$(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text)
if [ "$status" == "Success" ]; then
    echo "Automation $status"
elif [ "$status" == "Failed" -o "$status" == "Cancelled" -o "$status" == "Timed Out" ]; then
    echo "Automation $status"
fi

here the loop is never exiting, it keeps printing "Automation is running......" even after automation has been executed and status is not inprogress what i want to do is wait until status is " inprogress", print "Automation is running......" on screen. once its finished, i want to print the status of automation on screen if it failed or succeeded.

这里循环永远不会退出,它会继续打印“自动化正在运行......”即使在自动化已经执行并且状态不是进行中我想要做的是等到状态为“进行中”,打印“自动化是运行......”在屏幕上。完成后,如果失败或成功,我想在屏幕上打印自动化的状态。

回答by chandra

adding an if else until helped me get out of the loop.

添加一个 if else 直到帮助我摆脱循环。

until [ $(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text) = *"InProgress"* ];
do
  echo "Automation is running......"
  sleep 10s
  if [ $(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text) != "InProgress" ]; then
     echo "Automation Finished"
     status=$(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text)
     echo "Automation $status"
     if [$status != "Success"]; then
        exit 3
        echo "Automation $status"
     fi   
    break
  fi
done