1

I'm trying to increment indx and eindx values inside a ssh session.but none of commands are working..

#!/bin/bash
hosts=( #10.xx.xx.xx )
PdidPrefix1=$1
Runtime=$2
indx=$3
eindx=`expr $indx + $4`
logfilename=$5
for i in "${hosts[@]}"
do
echo $i
ssh centos@$i << EOF
for var in {1..4}
do
indx=`expr $eindx + 1` // not working
eindx=$((indx+eindx))  // not working
done
EOF
done
exit

tried using let also

let "indx=indx+1" //not working

Kindly suggest best way to handle variables.

4
  • Have you tried quoting the heredoc sigil, (e.g. 'EOF') to prevent variable expansion on the local host? Commented Oct 9, 2017 at 7:18
  • @DavidC.Rankin..Tried 'EOF' getting error. Commented Oct 9, 2017 at 7:46
  • Quoting the sigil prevents both pathname and variable expansion. It is a standard way to prevent both. It appears the problem you are having is you want the variables expanded locally and then the results pass via ssh to the host at $i. While not the cause, using $((...)) arithmetic is correct, leave expr a + b in the past. Have you tried with $((...)) arithmetic in all instances where you are doing math? Commented Oct 9, 2017 at 8:05
  • @DavidC.Rankin used $((..)) expression ,however it did not work.If I am using EOF,it is throwing an error that it cannot resolve $var which i'm trying to use in another command Commented Oct 9, 2017 at 9:45

2 Answers 2

2
#!/bin/bash
hosts=( xx.10.20.30 )
PdidPrefix1=$1
Runtime=$2
indx=$3
eindx=`expr $indx + $4`
logfilename=$5
for i in "${hosts[@]}"
do
echo $i
ssh centos@$i "bash -s --  '$indx' '$eindx'" <<\EOF

echo "In remote shell with parameters $1 $2..."
indx=$1
eindx=$2

for (( var=1; var<=4; var++ ))
do

indx=`expr $eindx + 1`
eindx=$((indx+eindx))
echo "var=" $var " indx=" $indx " eindx=" $eindx
done
EOF
done
exit
Sign up to request clarification or add additional context in comments.

2 Comments

No Luck.If I use \EOF, inner for loop variable 'var' is not getting resolved.If I remove and try to run, I can see in debug,expr $eindx + 1 is getting executed but not getting assigned to indx variable .
my bad..bro..Its working...I copied some part of code snippet and trying..Thanks...
0

I have run the same script. And it shows the proper value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.