0

So I have a process that moves and renames files using multiple scripts. I want to have a script that runs all of these scripts. Firstly I SSH into my server then cd to the appropriate directory then run the script. Looks like this.

ssh [email protected]<EOF
cd ../../path/to/files/
sh script1.sh
EOF 

This script1.sh then asks for a directory name for which to run the script and uses it in it's path. Once this script is complete I have another script I'd like to run on the same server, so I would modify my calling script to look like this.

ssh [email protected]<EOF
cd ../../path/to/files/
sh script1.sh
cd ../../diff/directory
sh script2.sh
EOF 

This doesn't work... I guess there's an issue with STDIN using SSH in a nested script? Hope someone knows of an easy fix without changing too much.

3
  • A heredoc uses <<EOF -- using a single angle bracket reads from the file named "EOF" Commented Mar 21, 2014 at 21:00
  • When script1 asks for a directory name, do you want this to be interactive or automated? Commented Mar 21, 2014 at 21:02
  • Interactive. Different directory name daily. Commented Mar 21, 2014 at 21:03

2 Answers 2

1

This is how you take user's input and echo back in remote shell using ssh:

ssh -t user@myhost "read -p \"enter val: \" a && echo \"Entered val is: \$a\""
Sign up to request clarification or add additional context in comments.

Comments

0

Let's consider what can be done continuing on with the here document approach.

This script1.sh then asks for a directory name. ...I guess there's an issue with STDIN

Yes. Have a look at your script:

ssh [email protected] <<EOF
cd ../../path/to/files/
sh script1.sh
cd ../../diff/directory
sh script2.sh
EOF 

When script1.sh runs, its stdin is not from your terminal; it is from the here document. The following demonstrates this:

$ mydir="some/path/" ; ssh home <<EOF
date
read var
$mydir
echo "I got var=\$var"
EOF

This produces the output:

Fri Mar 21 14:34:03 PDT 2014
I got var=some/path/

What happened is that the read statement asked stdin for another line. It got the next line of the here document.

If you want script1.sh to ask for a path on stdin, you need to supply that path in the next line of your here script.


P.S. Complications like this are one reason are why *nix programmers usually prefer to put parameters as arguments on the command line rather than asking for them as input.

Comments

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.