set variable in heredoc section

╄→гoц情女王★ 提交于 2019-11-27 07:59:13

问题


I'm a shell script newbie, so I must be doing something stupid, why won't this work:

#!/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


回答1:


#!/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...



来源:https://stackoverflow.com/questions/4994601/set-variable-in-heredoc-section

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!