3

I am trying to write a script, where I have a loop to login to multiple remote machines and execute a script inside each machine. Here is an example:

for ((j=1; j < 2; j++)); do 

  mchname="n"$j

  ssh -T $mchname <<'ENDSSH'    
    ./run_script < input > output &    
  ENDSSH

done

Whenever I try to execute the above script I get: "warning: here-document at line 37 delimited by end-of-file (wanted `ENDSSH')"

I am new to ssh, so I am sure I am making a silly mistake. Can anyone suggest me a solution? Thanks.

1 Answer 1

13

There's a problem in your bash script. The heredoc end tag (ENDSSH in your script) cannot be indented.

Try this instead:

  ssh -T $mchname <<'ENDSSH'    
    ./run_script < input > output &    
ENDSSH
# ^ no indentation for that line

Edit:

Also, you can run a command on the remote system by passing it as an argument to ssh, rather than providing it as standard input. The command will be executed by the user's remote shell:

ssh -T $mchname './run_script < input > output &'
Sign up to request clarification or add additional context in comments.

3 Comments

And -T is the default when you give ssh an explicit command to run, so ssh $mchname './run_script < input > output &'.
Thanks a lot! I would have never caught that!
If you use '<<-' instead of '<<' for the here document, any leading tabs (just tabs, not arbitrary whitespace) from the here document and the end tag will be stripped, allowing you to indent them more naturally.

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.