AKS
AKS

Reputation: 17326

Until loop not terminating or holding incremented variable's value

Why is this loop not breaking(ending due to if statement) or using the newly incremented value of $c variable? Tried c=expr $c + 1 with and without " double quote.

export c=1; ssh auser@someserver "until [[ 1 -eq 2 ]]; do echo \"Value of 'c' == $c\"; c=`expr $c + 1`; echo \"$c - Incremented value: `expr $c + 1`\"; if [[ $c -gt 5 ]]; then break; fi; sleep 2 ; done;"

or

$ export c=1; ssh root@$CHEF_SERVER \
> "until [[ 1 -eq 2 ]]; do \
>   echo \"Value of 'c' == $c\"; c=`expr $c + 1`; \
>   echo \"$c - Incremented value: `expr $c + 1`\"; \
>   if [[ $c -gt 5 ]]; then break; fi; \
>   sleep 2; \
>   echo; \
> done;"

Output is infinte: I had to ^c it.

Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Value of 'c' == 1
1 - Incremented value: 2
Killed by signal 2.

Upvotes: 1

Views: 66

Answers (2)

Inian
Inian

Reputation: 85690

You need to understand that shell variables needs to be properly escaped before using them in conditionals or assignments inside ssh under double quotes. Not doing so will let the variables expand even before the commands are executed over the remote machine. So in your case, no matter if you increment c or not the variable is expanded and condition remains always as below in the remote machine.

if [[ 1 -gt 5 ]]; then
    break
fi

Also expr is pretty much outdated, use a traditional C style loop increment using ((..)) as and escape all the invocations of variable inside to defer the variable expansion

ssh root@$CHEF_SERVER "
until [[ 1 -eq 2 ]]; do
    ((c = c+1))
    echo "Incremented value: \$c"
    if [[ \$c -gt 5 ]]; then
        break
    fi
    sleep 2
done"

Upvotes: 2

jhnc
jhnc

Reputation: 16771

Variables are expanded inside double-quoted strings. So are backticks.

You declare a variable c and assign it the value 1.

You then call ssh and pass it a double-quoted string.

The shell invoked by ssh sees:

until [[ 1 -eq 2 ]]; do 
   echo "Value of 'c' == 1"; c=2; 
   echo "1 - Incremented value: 2"; 
   if [[ 1 -gt 5 ]]; then break; fi; 
   sleep 2; 
   echo; 
done;

Upvotes: 0

Related Questions