linux - 我应该把函数放在哪里,以便在非交互式 ssh session 期间获取它们?

标签 linux shell ssh environment

当我在非交互式 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/

相关文章:

linux - 如何编写 shell 脚本通过 2 台计算机到第三台计算机的隧道

git - 在动态 DNS 后面使用 git

linux - 打开 ssh 连接卡住

Git 子模块最佳实践 - 始终使用 ssh 协议(protocol)而不是 https?

linux - 在 Jenkins 中执行 shell 命令不识别管道字符

linux - 在没有getopts的shell脚本中添加使用内容

linux - 如何为 mkusb 设置密码?

linux - 在 x86 Linux 程序集中手动添加换行符到堆栈变量

c++ - Linux C++ GUI编程工具

Linux 和 Primesense