0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

I did try to use the single quotes but I get an error. I have also pushed a script in the remote then call it via SSH but not working too.
@emyatsuna can you copy/paste the terminal session including both the code and the result?
@emyatsuna can you also copy/paste the contents of file?
cat testfile 1 2 3 4 5 6
hi @rtx13, I just had to recopy the script to new one. it solves the problem! thank you! :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.