如何在Makefile中转义`&>`?

用户3313834

我使用一个:

python notation.py &> notation.log

命令行。

我尝试从 Makefile 运行它但没有成功:

$ cat Makefile 
run:
    python notation.py &> notation.log
$ make
$ ls -la notation.log 
-rw-rw-r-- 1 me me 0 juin   8 08:15 notation.log
$
疯狂的科学家

问题不是逃避。问题是这不是 shell 中的合法语法。

Make 总是调用(默认情况下)/bin/sh作为 shell 来运行它的配方。/bin/sh是一个 POSIX 标准的外壳。令牌&>不是有效的 POSIX shell 语法。当您在 shell 提示符下登录时,您没有运行 shell /bin/sh,您正在运行更强大的 shell,可能bash(在 Linux 上)或可能zsh(在较新的 MacOS 系统上)。这些 shell 具有 POSIX 中未定义的额外功能,并且在 POSIX 兼容版本中不可用/bin/sh

您有两个选择:在您的配方中使用正确的 POSIX 语法:

run:
        python notation.py > notation.log 2>&1

或者告诉 make 在运行食谱时使用你的 shell:

SHELL := /bin/bash

run:
        python notation.py &> notation.log

(当然,这假设您要在其中运行 makefile 的所有系统实际上都已/bin/bash安装)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章