在Fabric脚本中调用bash别名 [英] Invoking bash aliases in Fabric scripts

查看:119
本文介绍了在Fabric脚本中调用bash别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在远程ubuntu服务器上的〜/ .bash_aliases 文件中有别名。该文件以标准方式从〜/ .bashrc 文件加载,如下所示:

I have aliases in a ~/.bash_aliases file on a remote ubuntu server. The file is loaded in the standard way from a ~/.bashrc file, like so:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

那个〜/ .bashrc 文件又以一种标准方式从〜/ .profile 文件加载(如下所示):

That ~/.bashrc file in turn is loaded (also in a standard way) from a ~/.profile file which looks like this:

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    source ~/.bashrc
    echo ".profile loaded .bashrc"
  fi
fi
mesg n

我发现我的结构命令中没有别名。当我在结构脚本中执行此操作时:

I've found that my aliases were unavailable in my fabric commands, e.g. when I do this in a fabric script:

run("aliased_command")

我得到以下输出:

run: aliased_command
out: .profile loaded .bashrc
out: /bin/bash: aliased_command: command not found
Fatal error: run() encountered an error (return code 127) while executing 'aliased_command'

现在,我通过使用ssh登录到服务器并运行:

Now I managed to reproduce this outside of fabric by logging into the server with ssh, and running :

~# /bin/bash -l -c aliased_command 

从外壳(nb / bin / bash -l -c 是fabric的默认值,请参见此处),我得到的输出是相同的:

from the shell (n.b. /bin/bash -l -c is fabric's default, see here) I get the same output:

.profile loaded .bashrc:
/bin/bash: aliased_command: command not found

在对相关主题进行了大量搜索之后,我在t不为非交互式shell导出别名,然后我设法使用 / bin / bash -l -c -i -i 将bash设置为交互模式。

After a fair bit of searching on related topics, I read somewhere that aliases aren't exported for non-interactive shells, and I then managed to fix this using /bin/bash -l -c -i (-i sets bash to interactive mode).

然后我将以下内容添加到fabfile.py中:

I then added the following to my fabfile.py:

env.shell = "/bin/bash -l -c -i" 

现在我可以在结构命令中使用别名了……太棒了!

Now I can use aliases in my fabric commands... just great!

所以我的问题是:


  • 此解决方案是否有问题?如果是这样,我应该怎么做?

  • Are there any problems with this solution? If so, what should I be doing?

其他人在fabfile中运行别名是否有问题?

Does anyone else have a problem running aliases in their fabfiles?

我有这个问题而其他人没有的明显原因吗?

Is there any obvious reason why I might have this issue and others wouldn't?

有人可以将我指向链接等吗?描述这个问题和解决方案?并解释他们是如何找到它们的...:)

Can anyone point me to links etc. that describe this problem and a resolution? And also explain how they found them... : )

推荐答案

是对主要问题的快速解答,是为了避免有人读我的长篇论文,只需
添加

Here is the quick answer to the main issue, to save someone reading my long question, just add

env.shell = "/bin/bash -l -i -c" 

到fabfile.py,您应该能够很好地在结构命令中使用别名!

to your fabfile.py and you should be able use aliases in your fabric commands just great!

这篇关于在Fabric脚本中调用bash别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆