我正在嘗試通過 Jenkins 作業為 aws 運行一系列命令,但是每當涉及 if 條件時,它都會引發錯誤
這是命令串列
echo '****** Updating ASG ********'
aws autoscaling update-auto-scaling-group --auto-scaling-group-name test-asg --desired-capacity 2 --min-size 2 --max-size 6
sleep 200
echo '****** Initiating Instance Refresh ********'
aws autoscaling start-instance-refresh --auto-scaling-group-name test-asg --preferences '{"InstanceWarmup": 300, "MinHealthyPercentage": 100}'
while true;
do
instance_refresh_status=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name test-asg --query "InstanceRefreshes[0].Status" --output text --region us-east-1)
if (( "$instance_refresh_status" == "Successful" )); then
break
fi
sleep 10
done
echo '****** Finished Instance Refresh ********'
echo '****** Updating ASG back to normal ********'
aws autoscaling update-auto-scaling-group --auto-scaling-group-name test-asg --desired-capacity 1 --min-size 1
我得到的錯誤是詹金斯輸出中的這個
instance_refresh_status=InProgress
InProgress == Successful
/tmp/jenkins8251579523029552853.sh: 10: /tmp/jenkins8251579523029552853.sh: InProgress: not found
無論我嘗試將 if 條件包含在 ()、(())、[]、[[]] 中,我都會遇到與上述相同的錯誤。
你能幫我解決這個問題嗎?
uj5u.com熱心網友回復:
使用if [ "$instance_refresh_status" = "Successful" ]; then(注意單個方括號和單個等號)。
你的版本有兩個問題:第一,(( ))是 bashism,你的腳本沒有在 bash 下運行。在任何符合 POSIX 的 shell 中,( )在子 shell 中運行其內容(作為普通的 shell 命令)。bash(和其他一些 shell)處理方式(( ))不同,但在像 dash 這樣的基本 POSIX shell(我懷疑你正在運行)中,它只是在兩層子 shell 中運行其內容。所以它的運行"InProgress" == "Successful"在雙子殼,它把InProgress為命令,且==與Successful作為引數傳遞給它,并得到一個“未找到”尋找命令錯誤。
如果你想使用 bash 特性(或者不知道哪些特性是 bashisms,哪些特性可以在其他 shell 中作業),你應該用一個適當的 bash 特定的 shebang 行來啟動腳本,比如#!/bin/bashor #!/usr/bin/env bash(并且不要覆寫它)使用sh命令運行腳本)。
第二個問題是,在bash,(( ))確實整數運算的評價。鑒于(( "InProgress" == "Successful" )),它將嘗試評估InProgress和Successful作為整數。細節在這里并不重要,但本質上它們都會被評估為 0,所以既然0 == 0是真的,即使它們是完全不同的字串,測驗結果也會是真的。
要進行字串比較,請使用[ ]or [[ ]],并且因為[[ ]]是另一種 bashism(就像==它們內部一樣),我建議[ = ]至少使用直到您確定腳本在哪個 shell 下運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318833.html
標籤:linux 猛击 亚马逊网络服务 詹金斯 aws-cli
