makefile - 将命令输出保存到Makefile变量时如何保留空格?

标签 makefile whitespace gnu-make

我知道像this one这样的问题,但是我的问题专门关于makefile,尤其是gnumake。

我有一个命令可以打印换行符和其他空格以标准化。我想将输出捕获到变量中,然后再将变量打印到文件中。

示例Makefile:

OUTPUT=${shell cowsay hello}

all:
    @echo "$(OUTPUT)" > output.txt

运行make之后,output.txt包含:
 _______  < hello >  -------          \   ^__^          \  (oo)\_______             (__)\       )\/\                 ||----w |                 ||     ||

我希望它保留空格,而包含:
 _______
< hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

我实际上使用的命令不是说说的,但是它会输出类似的空格和换行符。

最佳答案

I want to capture that output into a variable and then later print the variable to a file.


make的机制似乎没有办法将shell命令输出中的换行符转换为空格。与原始方法非常接近的一点技巧是,将输出分配给变量时,shell将换行符转换为一些不常见的字符(例如\1),然后在echo-将变量转换为变量时将其转换回原样。文件。像这样的东西:
OUTPUT=$(shell cowsay hello | tr '\n' '\1')

all:
        @echo "$(OUTPUT)" | tr '\1' '\n' > output.txt

对我来说,这导致
$ make
$ cat output.txt 
 _______ 
< hello >
 ------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

关于makefile - 将命令输出保存到Makefile变量时如何保留空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067438/

相关文章:

Makefile 和 rsync 错误 : Failed to exec ssh: No such file or directory

PHP:使用正则表达式从字符串中删除多余的空格

c++ - GNU 使 : generate list of source files

c - 如何在 ubuntu 14.04 上安装 .c 和 Makefile 驱动程序

c++ - 尝试安装用于发送电子邮件的 Qxt 时出错

java - 如何读取仅由空格分隔的一行?

c - 使 `main.c' 需要 : *** No rule to make target `main.o' , 。停止

python - SWIG 安装升压错误

makefile - 为什么 make 提示循环依赖?

bash - 正确处理文件名中带有空格的文件列表