Skip to content
Advertisement

How to preceed the output generated by “exec &” with a time/Date in linux bash scripts?

I have the following script file that writes files to s3 from a local file system:

JavaScript

And the output is coming out in a specific grok pattern needed for me to parse this output as logs into Elastic Search, however the line 27 output is as follows:

JavaScript

that upload and 0 of 0 0%… Line is created by the exec & command executed on line 16.

How can I get that output to not go to the next line without the date, time and script name preceeding it in order to not break the log pattern I am trying to create?

Advertisement

Answer

Rather than redirect output on each line, you can wrap the body of the script in a single block and then handle the output of the entire block in one place. You can then process that output with the stream editor sed. For example:

JavaScript

The sed expression means: Substitute (s) the beginning of each line (^) with the contents of the LOG variable.

Using 2>&1 at the end also eliminates the need for the exec &>> $LOG_PATH command.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement