Issue
In bash, output is on 1 line from command, so <pre>
is not working in html
tag.
log=`cat ../local/log/$(ls -t ../local/log | head -1 )`
echo '
<tr style="background-color: rgb(250,240,240);">
<td style="padding: 10px; text-align: center;"><pre>'$log'</pre></td>
</tr>
' > itf-check-article.html
How can I do get output to variable as classic line by line, not in one line?
Thanks.
Update.
Command.
log=`cat ../local/log/$(ls -t ../local/log | head -1 )`; echo $log.
Output (1 line).
Start of processing orders. 0 order(s) in this run. No orders in this run
But this command shows me this (with 2 lines).
itfstage@vm14258:~/sofimon$ cat ../local/log/UNIHOBBY_ITF_ORDERS.log
Start of processing orders. 0 order(s) in this run.
No orders in this run
itfstage@vm14258:~/sofimon$
When a Bash variable contains multiple lines and you want to output it with
echo
as it is; you need to put double quotes ("
) around the variable. For example:If you put double quotes, the
echo
command will consider it as a single (whose contents happen to contain a multi-line string) parameter. If you don't put double quotes the contents of the string will be split (by Bash) into multiple parameters whenever a white-space (space, tab, newline, etc.) occurs.For example:
Note, the first
echo
will get three parameters ('a'
,'b'
, and'c'
) and will output them by separating with a single space. The secondecho
will get a single parameter ('a b c'
) and will output it as it is.special-purpose code block (here doc).
output:
Using += operator to append to variable.
Your pre-tag is working fine. Your problem is that $log contains the output of cat on one line. Instead of trying to fix this, I would suggest you split this code into simpler steps.
First of all, this line:
Is way to complicated because it nests two command substitutions with two different syntaxes. You might understand it today, but why not split this into two steps so you or one of your co-workers will understand it a year from now:
Or even better (assuming the logfiles end in .log):
Now you see that you just put the contents of $logile into $logtext. You seem to want to output the content of $logfile between the pre-tags, just like it appears in the file. With the simplified commands above, it may appear obvious that putting it into $logtext is an extra step that isn't even needed. Just
cat
the file where you want it to appear. The backticks or the $()-construct is meant to feed one command with arguments that are created from the output of another. In this case that just messes it all up.So what you could do is this:
or simply run the commands between brackets
()
to start a subshell with one redirect statement instead: