源环境变量并在远程机器上运行本地脚本之前执行 bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25501252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Source environment variables and execute bash before running local script on remote machine
提问by Hayra
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.
我正在尝试使用 ssh 连接执行远程本地脚本。我已经阅读了有关它的语法的文档。但我的问题是,在运行脚本之前,我需要执行 bash 和源环境变量。
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 :
我已经用 EOF 尝试过这样的事情,但它对我也不起作用:
#!/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.
也许这是一个简单的问题,但我没有任何想法。预先感谢您的所有回答。
采纳答案by Hayra
i fixed problem as writing another template script which source the environment variables and run the script. And it hopefully works.
我解决了编写另一个模板脚本的问题,该脚本提供环境变量并运行脚本。它希望有效。
PROFILE=/dir/to/profile/.profile
source $PROFILE
cd /dir/to/script
/bin/bash script
If you use source command with bash shell, #!/bin/bash doesn't work for source command.
如果您在 bash shell 中使用 source 命令,#!/bin/bash 不适用于 source 命令。
Thank you for all your comments.
感谢您的所有评论。
回答by Chris
evalcan accomplish this for you:
eval可以为您完成此操作:
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:
如果您要远程执行此操作以解决您的特定问题,请将 SSH 放在它前面:
# 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 ,如下所示:
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:
如果您使用 eval 示例,您将看到输出:
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
回答by Jason Lytle
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.
我用它来执行远程 java 命令并需要 ENV 来查找 java。