简体   繁体   中英

Shell Script Looping not working while on remote SSH

I have been trying to make a looping work while accessing via SSH Remote command but it doesn't work. The command works when ran directly in host.

scp file root@${SERVER}:/tmp
ssh ${SERVER} "
while IFS= read -r line; do
   echo "$line"
done </tmp/file
"

I have tried using the single quotes in main script but it causing errors.

bash: line n: warning: here-document at line n delimited by end-of-file 

Any advise will be much appreciated.

UPDATE

testfile

1
2
3
4
5
6

Script test

SERVER='client'
ssh ${SERVER} '
echo "inside remote ClientServer"
echo "cat testfile"
cat /tmp/testfile

while read line; do
   echo "${line}"
done <</tmp/testfile
'
echo "Back to MasterServer..."

Terminal Result

root@server]# ./test
S
Kernel 4.14.35-1902.10.7.el7uek.x86_64 on an x86_64

inside remote ClientServer
cat testfile
1
2
3
4
5
6
bash: line 8: warning: here-document at line 8 delimited by end-of-file (wanted `/tmp/testfile')
Back to MasterServer...

Thank you.

You will probably want to use single quotes to pass the remote commands verbatim:

scp file root@${SERVER}:/tmp
ssh ${SERVER} '
while IFS= read -r line; do
   echo "$line"
done </tmp/file
'

Ensure you're using </tmp/file , not <</tmp/file . The sequence << is used to start a here-document which is not what you want in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM