make 忽略我的 python bash 别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7897549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:59:58  来源:igfitidea点击:

make ignores my python bash alias

makefilebash

提问by Kev

My CentOS 5.5 server has both Python 2.4 and Python 2.7 installed (to /opt/python2.7.2). In my ~/.bash_profileI have two aliases pointing to my Python 2.7 install and my PATHconfigured as:

我的 CentOS 5.5 服务器同时安装了 Python 2.4 和 Python 2.7(到/opt/python2.7.2)。在我的~/.bash_profile我有两个别名指向我的 Python 2.7 安装和我PATH配置为:

alias python=/opt/python2.7.2/bin/python
alias python2.7=/opt/python2.7.2/bin/python
PATH=$PATH:/opt/python2.7/bin

There's also a symbolic link I created as well:

我还创建了一个符号链接:

ln -sf /opt/python2.7.2/bin/python /usr/bin/python2.7

I have a Makefilewhich has the following lines:

我有一个Makefile有以下几行:

pythonbuild:
        python setup.py build

To my surprise I found that Python 2.4 is being invoked and not Python 2.7.

令我惊讶的是,我发现调用的是 Python 2.4 而不是 Python 2.7。

I have to explicitly specify python2.7:

我必须明确指定python2.7

pythonbuild:
        python2.7 setup.py build

Are bash aliases ignored by make? I am guessing makeuses PATHto locate the first pythonexecutable (which happens to be Python 2.4) instead?

bash 别名是否被忽略make?我猜测make用于PATH定位第一个python可执行文件(恰好是 Python 2.4)?

采纳答案by sarnold

From bash(1):

来自bash(1)

   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).

While you might be able to use something like SHELL=/bin/bash -O expand_aliasesin your Makefile, I think keeping an explicit dependency upon the newer Python in your Makefileis muchbetter than keeping the dependency hidden in your user~/.bash_profilefile.

虽然你也许能够使用的东西,像SHELL=/bin/bash -O expand_aliasesMakefile,我想保持在较新的Python的显式依赖于你的Makefile多少不是让藏在你的依赖更好的用户~/.bash_profile文件。

Instead, put PYTHON=/opt/python2.7/bin/pythoninto your Makefile, and then you can just use:

相反,放入PYTHON=/opt/python2.7/bin/python您的Makefile, 然后您就可以使用:

pythonbuild:
    $(PYTHON) setup.py build

in your rules.

在你的规则中。

The best part is you can easily change which Python interpreter you use on the command line:

最好的部分是您可以轻松更改在命令行上使用的 Python 解释器:

make PYTHON=/tmp/python-beta/bin/python pythonbuild

If you deploy it to another site, it is just oneline in the Makefilethat needs to be updated.

如果您将其部署到另一个站点,则只需更新其中的一行Makefile

回答by Markus Dutschke

Workaround with grep and awk:

使用 grep 和 awk 的解决方法:

The advantage of this solution is that if I change the alias in the ~/.bash_profil or ~/.bashrc, it is automatically adopted by my makefile as well.

这种解决方案的优点是,如果我更改 ~/.bash_profil 或 ~/.bashrc 中的别名,我的 makefile 也会自动采用它。

Description:

描述:

I want to use the alias lcwin my makefile, which is defined like in my ~/.bashrc file.

我想在我的 makefile 中使用别名 lcw,它的定义类似于我的 ~/.bashrc 文件。

.bashrc

.bashrc

...
alias lcw='/mnt/disk7/LCW/productiveVersion/lcw.out'
...

I use also the definition of a varialble as presented in the other solutions, but I directly read its value from the bashrc by using grep and awk.

我还使用了其他解决方案中介绍的变量的定义,但我直接使用 grep 和 awk 从 bashrc 读取了它的值。

makefile

生成文件

LCW= $(shell grep alias\ lcw= ~/.bashrc | awk -F"'" '{print $}')

.PHONY: std
std:
    $(LCW)

As you see the lcw alias is called by the command $(LCW)from the makefile.

如您所见, lcw 别名是由$(LCW)makefile 中的命令调用的。

Note:

笔记:

My solution assumes that the alias in the bashrc is defined within ' ' characters.

我的解决方案假设 bashrc 中的别名是在 ' ' 字符内定义的。

回答by nhed

aliases are typically just used by interactive shells

别名通常只由交互式 shell 使用

Note only that, I think that makedoes not always invoke the shell Your best bet is to be explicit about the paths you want to use

只需要注意,我认为这make并不总是调用 shell 你最好的办法是明确你想要使用的路径