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

16,017

Solution 1

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.

Solution 2

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
Share:
16,017
Hayra
Author by

Hayra

IT MADE ME FEARLESS! Computer engineer who is thinking that java is not only coffee type.

Updated on June 20, 2022

Comments

  • Hayra
    Hayra about 2 years

    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.