I have a template code:
<html>
<head>
<title> My page </title>
</head>
<body>
CODEWOULDCOMEHERE
</BODY>
</HTML>
And I have a shell script (ksh) that is suppose to generate some more code (to be placed in the CODEWOULDCOMEHERE palce).
I just don’t know how to place that code there… I tried sed, but it didn’t work… What’s the most robust way to do that?
Worth to say that the code generated is quite long…
thanks!
[EDIT]
What I’ve tried so far:
HTMLCODE=$(genReportStatCellHtml)
HTMLOUT=$(cat report_template.html|sed -e "s/CODEWOULDCOMEHERE/\'$HTMLCODE\'/g")
which yields: sed: 0602-404 Function s/GENERATEDHTMLCODE/\'a lot of html'/g cannot be parsed.
And:
HTMLCODE=$(genReportStatCellHtml)
HTMLOUT=$(cat report_template.html)
echo ${HTMLOUT/CODEWOULDCOMEHERE/$HTMLCODE}
which yields: ./test.sh[8]: ${HTMLOUT/CODEWOULDCOMEHERE/$HTMLCODE}: 0403-011 The specified substitution is not valid for this command.
A simple way of doing this is with here docs. This is what that looks like when entered interactively:
Or a shell script that does it
Edit: There’s not really a good way to use an external template file with nothing more than the basic abilities of the shell, but it’s pretty easy to do in other languages. Here’s a python one liner that’ll do it.
We need to use a specific template key for the one-liner, so
CODEWOULDCOMEHEREgets replaced by{0}.All together, your script would look something like this