Suppress echo of command invocation in makefile?

2020-05-13 03:55发布

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.

My problem is that my makefile:

...
run:
     java myprogram

also prints the command which runs my program (e.g. java myprogram) to the output file, so that my file includes this extra line causing the fingerprint to be wrong.

Is there any way to execute a command without the command invocation echoing to the command line?

4条回答
▲ chillily
2楼-- · 2020-05-13 04:11

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:

@printf "..."
查看更多
ら.Afraid
3楼-- · 2020-05-13 04:20

Even simpler, use make -s (silent mode)!

查看更多
Rolldiameter
4楼-- · 2020-05-13 04:33

Add @ to the beginning of command to tell gmake not to print the command being executed. Like this:

run:
     @java myprogram

As Oli suggested, this is a feature of Make and not of Bash.

On the other hand, Bash will never echo commands being executed unless you tell it to do so explicitly (i.e. with -x option).

查看更多
姐就是有狂的资本
5楼-- · 2020-05-13 04:34

You can also use .SILENT

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

In this case, make hi will output command, but make run will not output.

查看更多
登录 后发表回答