Shaunyl
Shaunyl

Reputation: 594

Execute FOR loop over ssh

I am trying to execute this code on several Solaris 10 remote boxes over ssh. I am reading some connection information from the local file ipa.txt:

#!/bin/ksh

while read hostn sid; do

   v_sid=$sid;
   ssh utest@$hostn << EOF

   for D in $(df -k |grep ora | grep -i $v_sid | awk '{print $6}')
   do
       printf "$(df -h $D |tail -1|awk '{print "FS:", $6, " usage:", $5"%"}')\n"
   done

EOF

done < ipa.txt

If I only execute the FOR loop directly into the terminal, it works. But If I put the whole script into a .ksh file and I execute that one, I receive the following error at line 3 (the line where is do, next line after FOR statement).

ksh[2]: syntax error at line 3 : `newline or ;' unexpected

There is a problem with the FOR loop but I don't understand where it is.

Any suggestion?

Thank you

Upvotes: 2

Views: 5593

Answers (2)

chepner
chepner

Reputation: 531235

The command substitution is expanded before ssh reads from the here document. If that pipeline is empty, the remote shell sees

for D in
do

which would cause your error. To send the exact text to the remote shell, escape any dollar signs that can trigger local expansions and use a format specifier for printf.

ssh utest@$hostn << EOF

   for D in \$(df -k |grep ora | grep -i \$v_sid | awk '{print \$6}')
   do
       printf '%s\n' "\$(df -h $D |tail -1|awk '{print "FS:", \$6, " usage:", \$5"%"}')"
   done

EOF

Upvotes: 3

conFusl
conFusl

Reputation: 939

can you try to put the condition into double-brackets?

while [[ read hostn sid ]]; do

Upvotes: 0

Related Questions