当我在非交互式 shell 中连接我的服务器时,我有一个函数需要 source,但似乎没有合适的地方。
我希望能够做到这一点
ssh user@remote.com 'my_function'
我读了Why does an SSH remote command get fewer environment variables then when run manually?并发现如果我在我的 ~/.ssh/environment 文件中放置一个变量(如 hi='hello'),我就能够做到这一点
ssh user@remote.com 'echo ${hi}'
它会回应我的“你好”值,我有点高兴;但是,我无法将函数 放入~/.ssh/environment 文件中,因为它不是那种文件:)
现在,我可以把我的东西放在哪里
function hi {
echo 'this is a function'
}
以便在非交互式 shell 中获取它?
那我可以做
ssh user@remote 'hi'
它会输出'这是一个函数'
最佳答案
如果您将函数放入 shell 的 rc 文件中,那么它取决于您的 shell。如果是 bash,请看这个:
https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
最简单的解决方案是将您的函数放入您家中某处的单独文件中,然后在调用该函数之前获取它:
ssh user@remote.com '. ~/.my_functions.sh; my_function'
这应该独立于您是否从交互式 session 中调用它。
关于linux - 我应该把函数放在哪里,以便在非交互式 ssh session 期间获取它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19067439/