是否使make响应标准错误而无需重定向? [英] Have `make` echo to standard error without re-direction?

查看:145
本文介绍了是否使make响应标准错误而无需重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Makefile中的某些目标运行其输出(它们发送到stdout)感兴趣的程序.出于我不知道的原因,make的作者决定回显已执行的命令到stdout,这会污染后者.

Some of the targets in my Makefile run programs whose output (which they send to stdout) I am interested in. For a reason not known to me, the authors of make decided to echo the executed commands to stdout, which pollutes the latter.

建议在此处来解决涉及交换文件描述符的问题.我想知道是否有一种更简单的方法将make回声强制为stderr.

A hard way around this problem that involves swapping file descriptors was suggested here. I am wondering if there is a simpler way to force make echo to stderr.

我浏览了makeman页面,但是除了-s选项之外,没有找到任何目的.我更喜欢保留命令的回显,但是将其保存在stderr中.

I looked through the man page of make, but did not find anything to this end besides the -s option. I prefer to preserve the echo of commands, but have it in stderr.

我还尝试制定了一个辅助目标(我将其作为所有其他目标的前提),

I also tried making an auxiliary target (which I made a prerequisite of all other targets), in which I put:

exec 3>&2
exec 2>&1
exec 1>&3

但是bash抱怨3不是有效的文件描述符.我只尝试了exec 1>&2,但是没有任何效果...

but bash complained that 3 wasn't a valid file descriptor. I tried only exec 1>&2, but that did not have any effect...

推荐答案

Makefile中完全可以做的是这样:

What you can do entirely in the Makefile is this:

define REDIR
@printf 1>&2 "%s\n" '$(1)'; $(1)
endef

.PHONY: all
all:
        $(call REDIR,echo updating .stamp)
        $(call REDIR,touch .stamp)

也就是说,控制通过宏回显自己的命令.不幸的是,它涉及以$(call ...)语法编写配方行.

That is to say, take control of the command echoing yourself via a macro. Unfortunately, it involves writing your recipe lines as `$(call ...) syntax.

REDIR现在实现了通过宏扩展来回显命令并执行该命令的语义.

REDIR now implements the semantics of echoing the command, and executing it, via macro expansion.

1>&2是Bash特定的语法,用于将标准错误文件描述符复制到标准输出,因此该命令然后有效地打印到标准输出.

The 1>&2 is Bash-specific syntax for duplicating the standard error file descriptor to standard out, so the command then effectively prints to standard output.

试运行:

$ make
echo updating .stamp
updating .stamp
touch .stamp

$ make 2> /dev/null
updating .stamp

正如您所看到的,是我们显式编码的echo行的输出,很好地进入了标准输出.这些命令会隐式发送到标准错误.

As you can see, updating .stamp, which is the output of our explicitly coded echo line, nicely goes to standard output. The commands are implicitly sent to standard error.

这篇关于是否使make响应标准错误而无需重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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