简体   繁体   English

Makefile:echo不正确打印

[英]Makefile: echo not properly printing

I have below command 我有以下命令

$ echo \\newcommand{\\coverheight}{11.0in} > tmp
$ cat tmp
echo \\newcommand{\\coverheight}{11.0in} > tmp

But when I'm using same echo command in make file, it's not properly writing to file. 但是当我在make文件中使用相同的echo命令时,它没有正确写入文件。

# Makefile
all:
       printf '\\newcommand{\\coverheight}{11.0in}' > tmp

After running `make', the output is: 运行`make'后,输出为:

$ cat tmp 

ewcommand{

How to write to a file properly using Makefile using echo ? 如何使用echo使用Makefile正确写入文件?

make just sends a recipe (except for splitting long lines) to your shell and do not interpret it. make只是发送一个配方(除了分割长行)到你的shell并且不解释它。 So it is your shell that interprets it. 所以你的shell解释它。

So your shell run this echo and printf commands. 所以你的shell运行这个echoprintf命令。 And shells like bash or zsh use builtin commands for echo and printf (if you don't say to use the /bin/echo command explicitly). 像bash或zsh这样的shell使用echo和printf的内置命令(如果你没有明确地使用/bin/echo命令)。

And there is a difference between behaviour of builtin commands between shells. shell之间的内置命令行为之间存在差异。 What is more is that you can use one shell for running interactive commands and make use different shell (by default /bin/sh) for handling recipies. 更重要的是,您可以使用一个shell来运行交互式命令, make使用不同的shell(默认情况下为/ bin / sh)来处理收件人。

Here is an example of difference between shells. 以下是shell之间差异的示例。 When I run in echo \\\\newcommand in bash I get: 当我在bash运行echo \\\\newcommand ,我得到:

$ echo \\newcommand
\newcommand

And when I run echo \\\\newcommand in zsh I get: 当我在zsh运行echo \\\\newcommand ,我得到:

$ echo \\newcommand

ewcommand

I suspect that you get different result because of it. 我怀疑你因此而得到不同的结果。 And actually printf '\\\\newcommand{\\\\coverheight}{11.0in}' must be more correct because it uses strong quoting. 实际上printf '\\\\newcommand{\\\\coverheight}{11.0in}'必须更正确,因为它使用强引号。

Anyway one way to print in makefile seems to be using external command /bin/echo: 无论如何,在makefile中打印的一种方法似乎是使用外部命令/ bin / echo:

all:
       command echo '\\newcommand{\\coverheight}{11.0in}' > tmp

Or use strong quote as you already have done: 或者使用您已经完成的强引用:

all:
       printf '\\newcommand{\\coverheight}{11.0in}' > tmp

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

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