摘要:为什么会失败
$ssh foo 'R --version | head -n 1' bash: R: command not found
但这成功了
$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)
?细节:
我是2个集群上的(无根)用户.一个使用环境模块,因此该集群上的任何给定服务器都可以提供(通过模块添加)几乎相同的资源.我不得不工作的另一个集群,单独管理服务器,所以我养成了做的习惯,例如:
EXEC_NAME='whatever' for S in 'foo' 'bar' 'baz' ; do ssh ${SERVER} "${EXEC_NAME} --version" done
这适用于正常/一致安装的软件包,但通常(由于我不知道的原因)软件包不是:例如: (比较下面的别名和上面的别名),
$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)
当我交互式shell进入服务器时,使用别名可以很好地应对这些安装差异,但是当我尝试编写ssh命令脚本时失败(如上所述);即,
# interactively $ssh foo ... foo> R --version
在远程host = foo上调用R的别名,但是
# scripting $ssh foo 'R --version'
没有.我需要做什么才能使ssh foo“< command />”在远程主机上加载我的别名?
解决方法
从bash手册页:
Aliases are not expanded when the shell is not interactive,unless the expand_aliases shell option is set using shopt (see the description of
shopt under SHELL BUILTIN COMMANDS below).
你需要在远程.bashrcs中购买-s expand_aliases,并检查那些.bashrc是否在非交互时被处理;您的发行版的默认.bashrc可能包含一个检查,以便在非交互式时提前终止,例如: Ubuntu使用:
# If not running interactively,don't do anything [ -z "$PS1" ] && return