Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source environment variables and execute bash before running local script on remote machine [duplicate]

Tags:

bash

shell

ssh

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.

like image 775
Hayra Avatar asked Aug 26 '14 08:08

Hayra


People also ask

Can bash script access environment variables?

In Bash, environment variables define many different things : your default editor, your current username or the current timezone. Most importantly, environment variables can be used in Bash scripts in order to modify the behaviour of your scripts.

How do I run a script on a remote machine?

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.


1 Answers

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
like image 161
Chris Avatar answered Nov 10 '22 04:11

Chris