`ssh foo "<command/>"` 没有加载远程别名?

Tom*_*che 5 linux ssh bash bashrc login-script

总结:为什么会失败

$ ssh foo 'R --version | head -n 1'
bash: R: command not found
Run Code Online (Sandbox Code Playgroud)

但这成功了

$ ssh foo 'grep -nHe 'bashrc' ~/.bash_profile'
/home/me/.bash_profile:3:# source the users .bashrc if it exists
/home/me/.bash_profile:4:if [ -f "${HOME}/.bashrc" ] ; then
/home/me/.bash_profile:5:  source "${HOME}/.bashrc"
$ ssh foo 'grep -nHe "\WR\W" ~/.bashrc'
/home/me/.bashrc:118:alias R='/share/linux86_64/bin/R'
$ ssh foo '/share/linux86_64/bin/R --version | head -n 1'
R version 2.14.1 (2011-12-22)
Run Code Online (Sandbox Code Playgroud)

? 细节:

我是 2 个集群的(无根)用户。一个使用环境模块,因此该集群上的任何给定服务器都可以(通过module add)提供几乎相同的资源。不幸的是,我也必须在另一个集群上工作,它有单独管理的服务器,所以我养成了这样做的习惯,例如,

EXEC_NAME='whatever'
for S in 'foo' 'bar' 'baz' ; do
  ssh ${SERVER} "${EXEC_NAME} --version"
done
Run Code Online (Sandbox Code Playgroud)

这适用于正常/一致安装的软件包,但通常(出于我不知道的原因)软件包不是:例如(将下面的别名与上面的别名进行比较),

$ ssh bar 'R --version | head -n 1'
bash: R: command not found
$ ssh bar 'grep -nHe 'bashrc' ~/.bash_profile'
/home/me/.bash_profile:3:# source the users .bashrc if it exists
/home/me/.bash_profile:4:if [ -f "${HOME}/.bashrc" ] ; then
/home/me/.bash_profile:5:  source "${HOME}/.bashrc"
$ ssh bar 'grep -nHe "\WR\W" ~/.bashrc'
/home/me/.bashrc:118:alias R='/share/linux/bin/R'
$ ssh bar '/share/linux86_64/bin/R --version | head -n 1'
R version 2.14.1 (2011-12-22)
Run Code Online (Sandbox Code Playgroud)

当我以交互方式进入服务器时,使用别名可以很好地处理这些安装差异,但是当我尝试编写 ssh 命令脚本时失败(如上);IE,

# interactively
$ ssh foo
...
foo> R --version
Run Code Online (Sandbox Code Playgroud)

调用我的别名 for Ron remote host= foo,但是

# scripting
$ ssh foo 'R --version'
Run Code Online (Sandbox Code Playgroud)

没有。我需要做什么才能ssh foo "<command/>"在远程主机上加载我的别名?

And*_*rew 6

bash联机帮助页:

当 shell 不是交互式时,别名不会扩展,除非使用 shopt 设置 expand_aliases shell 选项(请参阅下面的 SHELL BUILTIN COMMANDS 下的 shopt 描述)。

你需要shopt -s expand_aliases在你的远程.bashrcs 中,并检查它们 .bashrc是否在非交互式时被处理;.bashrc您的发行版的默认设置可能包括在非交互式时提前终止的检查,例如 Ubuntu 使用:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return
Run Code Online (Sandbox Code Playgroud)