You'll to use eval for that which is unsafe.
Better redirect it on a block, and use process substitution:
{
rm aaa
# ... other commands
} 2> >(
while read -r LINE; do
echo "$LINE"
echo '<br><br>'
done >> error_file
)
Another way which would write <br><br>
only once at the end:
{
rm aaa
# ... other commands
} 2> >(
if read -r LINE; then
while
echo "$LINE"
read -r LINE
do
continue
done
echo '<br><br>'
fi >> error_file
)
This one would write <br><br>
even without an error, and only once at the end:
{
rm aaa
# ... other commands
} 2>> error_file
echo '<br><br>' >> error_file
Note if you're only using one command like rm
, you don't have to place it inside a block, just rm aaa 2>> >( ...
. And I think you'll only need one line:
rm aaa 2>> >(read -r LINE && echo "$LINE"$'\n''<br><br>' >> error_file)
Another:
EFILE=/path/to/error_file
rm aaa 2> >(read -r LINE && echo "$LINE"$'\n''<br><br>' >> "$EFILE")
Command-specific function:
EFILE=/path/to/error_file
function log_error_to_file {
read -r LINE && echo "$LINE"$'\n''<br><br>' >> "$EFILE"
}
rm aaa 2> >(log_error_to_file)
another_command 2> >(log_error_to_file)
Multiple lines:
function log_error_to_file {
while read -r LINE; do
echo "$LINE"
echo '<br><br>'
done >> error_file
}