Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 我应该将函数放在哪里,以便在非交互式ssh会话期间将它们来源于哪里?_Linux_Shell_Ssh_Environment - Fatal编程技术网

Linux 我应该将函数放在哪里,以便在非交互式ssh会话期间将它们来源于哪里?

Linux 我应该将函数放在哪里,以便在非交互式ssh会话期间将它们来源于哪里?,linux,shell,ssh,environment,Linux,Shell,Ssh,Environment,当我在一个非交互式的shell中连接服务器时,我有一个需要来源的函数,但似乎没有合适的地方 我希望能够做到这一点 ssh user@remote.com 'my_function' ssh user@remote.com 'echo ${hi}' 我阅读并发现,如果我在~/.ssh/environment文件中放入一个类似hi='hello'的变量,我就能够做到这一点 ssh user@remote.com 'my_function' ssh user@remote.com 'echo $

当我在一个非交互式的shell中连接服务器时,我有一个需要来源的函数,但似乎没有合适的地方

我希望能够做到这一点

ssh user@remote.com 'my_function'
ssh user@remote.com 'echo ${hi}'
我阅读并发现,如果我在~/.ssh/environment文件中放入一个类似hi='hello'的变量,我就能够做到这一点

ssh user@remote.com 'my_function'
ssh user@remote.com 'echo ${hi}'
它会呼应我的“你好”价值观,我有点高兴;但是,我无法将函数放入~/.ssh/environment文件中,因为它不是那种文件:

现在,我可以把我的手机放在哪里

function hi {
    echo 'this is a function'
}
因此,它将在非交互式shell期间进行源代码获取

那我就可以了

ssh user@remote 'hi'

它会输出“这是一个函数”

如果您将函数放入shell的rc文件中,那么它取决于shell。如果是bash,请参见以下内容:

最简单的解决方案是将函数放在家中某个单独的文件中,然后在调用函数之前对其进行源代码转换:

ssh user@remote.com '. ~/.my_functions.sh; my_function'

这应该独立于您是否从交互式会话调用它而工作。

您需要将函数放在服务器中,因为它将通过ssh在那里执行。。。并调用函数的完整路径…好的,这是可行的;但是,当我在sh文件中使用alias时,它们对我的非交互式shell不可用?但它是有效的。即使这样做有效,实际上也不必在ssh命令中设置脚本。我已将BASH_ENV='$HOME/script.sh'添加到我的~/.ssh/environment文件中,但它似乎没有来源。我是否缺少一些东西,因为每次尝试ssh时都应该对该脚本进行源代码分析user@server.com'如果[-n$BASH_ENV];那么$BASH_ENV;fi'I get bash:“$HOME/script.sh”:没有这样的文件或目录。。。我找到了一种方法,借助于:在那里,我发现我可以放置[-f$HOME/script.sh]&&&$HOME/script.sh到my/etc/profile,我的脚本来源: