在ssh命令中使用远程服务器的环境变量(use environment variables of remote server in ssh command)

编程入门 行业动态 更新时间:2024-10-20 05:49:36
在ssh命令中使用远程服务器的环境变量(use environment variables of remote server in ssh command)

我有两台服务器X(主机)和Y(远程)。 脚本abc.sh保存在Y服务器上。 我使用ssh命令从服务器X运行abc.sh。

脚本运行成功,但包含服务器Y的环境变量(保存在/.bash_profile中)的命令给出空白输出。

当我从服务器Y本身运行abc.sh时,所有命令都成功运行。

我的问题是如何包含远程服务器(Y)的环境变量,以便完整的脚本将成功执行。

注意:我没有对etc目录的写入权限,所以我无法更改任何内容。

提前致谢!

I have two servers X(host) and Y(remote). A script abc.sh is saved on Y server. I am running abc.sh from server X using ssh command.

Script is running successfully but the commands which contain the environment variables(kept in /.bash_profile) of server Y are giving blank output.

When I am running abc.sh from server Y itself, all commands running successfully.

My question is how to include the environment variables of remote server(Y) so that full script will execute successfully.

NOTE : I don't have write access to etc directory so I can't change anything in it.

Thanks in advance!

最满意答案

您可以包含您的环境变量,如下所示:

ssh user@host_y 'source ~/.bash_profile; /path/to/your/script/abc.sh'

由于直接命令运行不是交互式shell,因此您的变量将无法在此运行。 source将在当前shell中运行脚本,并将该文件中的可见环境变量发送到脚本。

You can include your environment variables like following:

ssh user@host_y 'source ~/.bash_profile; /path/to/your/script/abc.sh'

Since direct command run is not an interactive shell, your variable will not work there. source will run script in your current shell and make visible environment variables in that file to your script.

更多推荐

本文发布于:2023-07-06 07:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境变量   命令   服务器   ssh   environment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!