简体   繁体   English

抑制 makefile 中命令调用的回显?

[英]Suppress echo of command invocation in makefile?

I wrote a program for an assignment which is supposed to print its output to stdout.我为一项作业编写了一个程序,该程序应该将其输出打印到标准输出。 The assignment spec requires the creation of a Makefile which when invoked as make run > outputFile should run the program and write the output to a file, which has a SHA1 fingerprint identical to the one given in the spec.分配规范需要创建一个 Makefile,当以make run > outputFile调用时,它应该运行程序并将输出写入一个文件,该文件的 SHA1 指纹与规范中给出的指纹相同。

My problem is that my makefile:我的问题是我的makefile:

...
run:
     java myprogram

also prints the command which runs my program (eg java myprogram) to the output file, so that my file includes this extra line causing the fingerprint to be wrong.还将运行我的程序的命令(例如 java myprogram)打印到输出文件,以便我的文件包含导致指纹错误的额外行。

Is there any way to execute a command without the command invocation echoing to the command line?有没有办法在没有命令调用回显到命令行的情况下执行命令?

Add @ to the beginning of command to tell gmake not to print the command being executed.@添加到命令的开头告诉 gmake 不要打印正在执行的命令。 Like this:像这样:

run:
     @java myprogram

As Oli suggested, this is a feature of Make and not of Bash.正如 Oli 所建议的,这是 Make 的一个特性,而不是 Bash 的特性。

On the other hand, Bash will never echo commands being executed unless you tell it to do so explicitly (ie with -x option).另一方面,Bash 永远不会回显正在执行的命令,除非您明确告诉它这样做(即使用-x选项)。

更简单的是,使用make -s (静默模式)!

You can also use .SILENT您也可以使用.SILENT

.SILENT: run
hi:
     echo "Hola!"
run:
     java myprogram

In this case, make hi will output command, but make run will not output.在这种情况下, make hi会输出命令,但make run不会输出。

The effect of preceding the command with an @ can be extended to a section by extending the command using a trailing backslash on the line.通过在行上使用尾随反斜杠扩展命令,可以将命令前面带有@的效果扩展到一个部分。 If a .PHONY command is desired to suppress output one can begin the section with:如果需要.PHONY命令来抑制输出,可以使用以下内容开始该部分:

@printf "..."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM