Note that there are some explanatory texts on larger screens.

plurals
  1. POlogging blocks of code to log files in bash
    text
    copied!<p>I have a huge bash script and I want to log specific blocks of code to a specific &amp; small log files (instead of just one huge log file).</p> <p>I have the following two methods: </p> <pre><code># in this case, 'log' is a bash function # Using code block &amp; piping { # ... bash code ... } | log "file name" # Using Process Substitution log "file name" &lt; &lt;( # ... bash code ... ) </code></pre> <p>Both methods may interfere with the proper execution of the bash script, e.g. when assigning values to a variable (like the problem presented <a href="https://stackoverflow.com/questions/6255303/bash-piping-prevents-global-variable-assignment">here</a>).<br> How do you suggest to log the output of commands to log files? </p> <hr> <p><strong>Edit:</strong> This is what I tried to do (besides many other variations), but doesn't work as expected:</p> <pre><code>function log() { if [ -z "$counter" ]; then counter=1 echo "" &gt;&gt; "./General_Log_File" # Create the summary log file else (( ++counter )) fi echo "" &gt; "./${counter}_log_file" # Create specific log file # Display text-to-be-logged on screen &amp; add it to the summary log file # &amp; write text-to-be-logged to it's corresponding log file exec 1&gt; &gt;(tee "./${counter}_log_file" | tee -a "./General_Log_File") 2&gt;&amp;1 } log # Logs the following code block { # ... Many bash commands ... } log # Logs the following code block { # ... Many bash commands ... } </code></pre> <p>The results of executions varies: sometimes the log files are created and sometimes they don't (which raise an error).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload