问题
I'm writing a dead simple Makefile to be used with GHCi and gedit. Essentially, I define a module to load, and a function (along with its parameters) to call/test. The Makefile would need to execute GHCi and then pass the necessary commands into it. I tried piping with echo
, and it worked in a normal shell, but fails due to the way make
handles whitespace, strings & formatting. It feeds the whole thing as as one single line, rather than individual lines of input. On top of that, it is a bit messy.
# Sorry if backslashes are messed up do to tabs...
module = somemodule
function = somefunction
params = 5 "Hello"
default: *.hs
echo \
:l $(module) \
\
$(function) $(params) \
| ghci
How could I acheive this cleanly and elegantly? :)
回答1:
Have you tried using two separate echo
commands?
( echo ":l $(module)"; echo "$(function) $(params)" ) | ghci
回答2:
An upcoming version of GNU Make will have a $(file) function to aid in similar situations, though perhaps not quite this one.
In lieu of that, the basic approach is to have the newlines not be newlines at the critical moment:
define boilerplate
:l $(module)
$(function) $(params)
endef
define newline
endef
default: *.hs
echo '$(subst $(newline),|,$(boilerplate))' | tr '|' '\n' | ghci
(The source text for that $(newline)
definition contains two empty lines, as define
eats one trailing newline before the endef
.)
If you can't assume GNU Make and therefore don't have subst
or define
, you can write the |
-encoded newlines directly in the echo command:
default: *.hs
echo ':l $(module)|$(function) $(params)' | tr '|' '\n' | ghci
...at which point it's just a matter of taste whether this is better or worse than Idelic's subshell suggestion.
来源:https://stackoverflow.com/questions/12397955/how-can-i-pass-multi-line-input-stdin-into-a-command-im-executing-in-my-makef