我知道像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/