如何通过SSH执行Bash别名 [英] How to execute a Bash alias over ssh
问题描述
这就是我想要做的:
ssh andy@<ip_address> "cat .bash_aliases; sayhello"
发生的事情是这样:
alias sayhello="echo hello"
bash: sayhello: command not found
要更具体地解决我的问题,我正在尝试调用命令
ssh上的"sudo etherwake -i eth0 <mac_address>"
-这在我的本地计算机上执行(我认为),并给出sudo: unable to resolve host [blabla]
错误.似乎所有不是标准bash命令的命令都是由我的本地计算机解析的.
To be more specific about my problem I'm trying to invoke the command
"sudo etherwake -i eth0 <mac_address>"
over ssh -- this executes (I think) on my local computer, giving a sudo: unable to resolve host [blabla]
error. It seems like any commands that are not standard bash commands are parsed by my local machine.
如果是这种情况,我该如何解决?如果没有,说明是什么?
If that's what's happening, how do I get around it? If not, what is the explanation?
推荐答案
通常在脚本中使用别名不是一个好主意.
In general this is not a good idea to use aliases in scripts.
但是我可以建议一种方法,但是请记住它是多么不安全.
Yet I can suggest one way to do it, but bare in mind how unsafe it is.
- 它依赖于
eval
, - 它将远程文件读入shell上下文.
我们到了.
ssh remote_host "shopt -s expand_aliases ; source ~/.bash_aliases ; eval sayhello"
说明:
-
默认情况下,仅对交互式Shell启用别名扩展.要打开它,请使用
shopt -s
命令.
无论如何,您都需要将别名来源到您的Shell上下文中.
You will need to source the aliases into your shell context anyway.
现在,您已设置为通过eval
命令使用别名.
Now you are set to use your aliases via the eval
command.
这篇关于如何通过SSH执行Bash别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!