Set variable in heredoc section

I'm new to the shell script, so I have to do something stupid why this won't 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 of them should have the same value ... but they do not. First print statement [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 to set the value to $ count?

What am I doing wrong?

thanks

+4
source share
1 answer
 #!/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 expands the variables, so $count expanded by your local shell before the remote commands were disabled ...

+7
source

Source: https://habr.com/ru/post/1339681/


All Articles