john stamos
john stamos

Reputation: 1125

Shell script Calling multiple shell scripts that use STDIN via SSH

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.

Upvotes: 0

Views: 324

Answers (2)

John1024
John1024

Reputation: 113994

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.

Upvotes: 0

anubhava
anubhava

Reputation: 786291

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\""

Upvotes: 1

Related Questions