【问题标题】:make ignores my python bash aliasmake 忽略了我的 python bash 别名
【发布时间】:2011-10-26 00:47:05
【问题描述】:

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

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

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

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

我有一个Makefile,其中包含以下几行:

蟒蛇构建: python setup.py 构建

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

我必须明确指定python2.7

蟒蛇构建: python2.7 setup.py 构建

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

【问题讨论】:

    标签: makefile 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).
    

    虽然您可能可以在您的 Makefile 中使用类似 SHELL=/bin/bash -O expand_aliases 的东西,但我认为在您的 Makefile 中保持对较新 Python 的显式依赖比保持依赖更好很多隐藏在您的用户 ~/.bash_profile 文件中。

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

    pythonbuild:
        $(PYTHON) setup.py build
    

    在你的规则中。

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

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

    如果您将它部署到另一个站点,它只是Makefile 中的一个 行需要更新。

    【讨论】:

      【解决方案2】:

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

      请注意,我认为 make 并不总是调用 shell 最好的办法是明确说明您要使用的路径

      【讨论】:

        【解决方案3】:

        使用 grep 和 awk 的解决方法:

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

        说明:

        我想在我的 makefile 中使用 alias lcw,它的定义就像在我的 ~/.bashrc 文件中一样。

        .bashrc

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

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

        制作文件

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

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

        注意:

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

        【讨论】:

        • 赞赏并将保留此技术。
        猜你喜欢
        • 2019-11-16
        • 2018-10-23
        • 2011-12-07
        • 1970-01-01
        • 2012-03-26
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 2019-08-10
        相关资源
        最近更新 更多