Home > Mobile >  Shell/Bash - send cli command until desired value returned
Shell/Bash - send cli command until desired value returned

Time:11-02

My script needs to run aws cli command until getting success status. The below script doesn't work and throws a syntax error. I'm aiming also to get it re-try CLI call until it gets successful up to (for example) 5 retires:

[0: command not found

Big thanks to @Ярослав Рахматуллин!!

retry_times=5
counter=0

ri_status=`aws dms describe-connections --filter Name=replication-instance-arn,Values=$ri_arn --query=Connections[0].Status --region us-east-1`

#test $ri_status = '"successful"'
#echo $?

#until test $ri_status = '"successful"'
until [[ $ri_status =~ successful ]]
    do 
    ri_status=`aws dms describe-connections --filter Name=replication-instance-arn,Values=$ri_arn --query=Connections[0].Status --region us-east-1`
    [[ counter -eq $retry_times ]] && echo "Failed!" && exit 1
    echo "Trying again. Try #$counter"
    ((counter  ))
    sleep 5
done

CodePudding user response:

try this:

until [ $? -eq 0 ]
do
    # foobar...
    sleep 5
done

notice the spaces. [ and ] are separate commands and need spaces around them do be distinguishable from other commands. 0] is confusing to bash, so is [0 which is "[" "0".

Just test for the condition that you want directly instead of looking at the exit value of the previous command.

until test $ri_status = '"successful"'
do
    ri_status=`aws dms describe-connections --filter Name=replication-instance-arn,Values=$ri_arn --query=Connections[0].Status --region us-east-1`
    sleep 5
done
  • Related