after each command, if success: show a custom message, if error: show error, and send to file
<p>A bash script executes several commands.
After each command, how to: </p>

<ul>
<li>if success: show a custom message in the console and append it to a log file</li>
<li>if error: show the error message in the console, append error message to the log file and resume the entire script</li>
</ul>

<p>Here is where I am: </p>

<pre><code>log_file="log.txt"
output() {
  echo $@
  echo $@ 2&gt;&amp;1 &gt;&gt; $log_file;
  if [ $@ -eq 0 ]
  then
    exit 1
  fi
}
</code></pre>

<p>and then after each command: </p>

<pre><code>if [ $? -eq 0 ] 
then 
  output "Custom message"
else
  output $?
fi
</code></pre>

<p>Which makes lots of repetitions… </p>