8

I'm trying to execute the remote local script with ssh connection. I've read a document about the syntax of it. But my issue is that, before running the script, I need to execute bash and source environment variables.

This looks appropriate for me but it has not a source command :

ssh [user]@[server] 'bash -s' < [local_script]

I've tried such a thing with EOF but it didn't work for me too :

#!/bin/bash
/usr/bin/ssh  "$user@$$host" <<EOF
bash -s
source /dir/to/profile/.profile
source /dir/to/env/set/env.sh
/path/to/script/script.sh stop
EOF

Do you have an idea for this type of implementation of remote commands ? I have to source profile before the environment settings otherwise it gives an exception. But the main problem is about source.

Maybe it was an easy question but I don't have any ideas. Thank you in advance for your all answers.

Hayra
  • 456
  • 2
  • 7
  • 22
  • Maybe `-t` option will help to initialize environment as in login shell. `ssh user@server 'bash -t -c local_script'` – user3132194 Aug 26 '14 at 08:44
  • The difference between your first example and your second is that the first example runs `bash -s` as the remote command and sends a script to it as input whereas the second example is sending four lines of text as standard input to the default command/shell on the remote host. If you move the `bash -s` to the command position in the second example does it work? – Etan Reisner Aug 26 '14 at 10:19
  • Two of these solutions won't work, I'v tried many things but it doesn't work!! :( – Hayra Aug 26 '14 at 11:39
  • Do files like `/dir/to/profile/.profile` exist on the local machine or on the remote? - cause the way they are used now assumes they exist on the **remote**. – ArnonZ Aug 26 '14 at 12:08
  • They exist on remote – Hayra Aug 27 '14 at 05:50
  • "... trying to execute the remote local script... ". Wut? Is the script remote? Or local? It seems to me it can't be both... – twalberg Aug 27 '14 at 14:19
  • @twalberg The script is local, but it's being run remotely (see the syntax in the question : `ssh [user]@[server] 'bash -s' < [local_script]`). This functionality is built into SSH. Also, for anyone finding this via a search engine, check out this question: [Source a script remotly via ssh](http://stackoverflow.com/q/25870899/320399) – blong Aug 13 '15 at 18:12
  • Another discussion I think it's worthy of linking to is [this](http://stackoverflow.com/q/216202/320399) one. Specifically, [this](http://stackoverflow.com/a/1472444/320399) answer. – blong Aug 13 '15 at 18:28

3 Answers3

8

eval can accomplish this for you:

eval $(cat /path/to/environment) ./script.sh

You can source multiple files this way too if you want if you know there path:

eval $(cat /path/to/environment1 /path/to/environment2) ./script.sh

Or iterate over a directory:

eval $(cat $(find -type f /path/to/environments)) ./script.sh

Stick SSH in front of it if you're doing this remotely to solve your specific problem:

# note the quotes otherwise we'll source our local environment
ssh user@host "'eval $(cat /path/to/environment)' ./remote_script.sh"

# If it's a local environment you want to sort, then do the same
# command without the quotes:
ssh user@host "eval $(cat /path/to/environment)" ./remote_script.sh

If you want to source a remote environment into your own then use eval locally as so:

eval "$(ssh user@host cat /path/to/environment)" ./local_script.sh

This alls you to source an external file setting it's environment variables in the same forked instance that will calls your script (making them available).

Consider a script file that looks like this:

#!/bin/sh
echo "$VAR1"
echo "$VAR2"
test_function

Now consider your environment file looks like this:

# Environment Variables
VAR1=foo
VAR2=bar
test_function()
{
   echo "hello world"
}

You'd see the output if you use the eval example:

foo
bar
hello world

Alternatively, if you just open up your script you wrote, you can source these environment variables directly from within it and then you can just call the script normally without any tricks:

#!/bin/sh

# Source our environment by starting with period an then following
# through with the full path to the environment file. You can also use
# the 'source' keyword here too instead of the period (.).
. /path/to/environment

echo "$VAR1"
echo "$VAR2"
test_function
Chris
  • 491
  • 7
  • 14
0

I fixed the problem by writing another template script that sources the environment variables and runs the script:

PROFILE=/dir/to/profile/.profile
source $PROFILE
cd /dir/to/script
/bin/bash script $1

If you use the source command with bash shell, #!/bin/bash doesn't work for the source command.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Hayra
  • 456
  • 2
  • 7
  • 22
0

I know it is old but just wanted to add that it can be done without an extra file - use '\' to escape local variables and remote command substitution - ie:

ssh me@somehost "RMTENV=\$(ls /etc/profile) && source \$RMTENV"

I use this to execute remote java commands and need the ENV to find java.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47