开发者

set variable in heredoc section

I'm a shell script newbie, so I must be doing something stupid, why won't this w开发者_如何学Goork:

#!/bin/sh

myFile=$1

while read line
do
ssh $USER@$line <<ENDSSH
ls -d foo* | wc -l 
count=`ls -d foo* | wc -l`
echo $count
ENDSSH
done <$myfile

Two lines should be printed, and each should have the same value... but they don't. The first print statement [the result of ls -d foo* | wc -l] has the correct value, the second print statement is incorrect, it always prints blank. Do I need to do something special to assign the value to $count?

What am I doing wrong?

Thanks


#!/bin/sh

while read line; do
  echo Begin $line
  ssh $USER@$line << \ENDSSH
  ls -d foo* | wc -l 
  count=`ls -d foo* | wc -l`
  echo $count
ENDSSH
done < $1

The only problem with your script was that when the heredoc token is not quoted, the shell does variable expansion, so $count was being expanded by your local shell before the remote commands were shipped off...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜