It’s funny how through small daily tasks sometimes it happen to find new features or commands that you do not knew not, and today this thing happened to me.
In particular, I had to do something trivial on the shell of a server, run : command1 | tail-n 2
i use tail
to keep, from a significant long output, only the last 2 lines which then i use in another function, but beyond this, I needed to understand if command1 was terminated with an exit code of 0 or if the code was not 0 which number it was.
A simple:
.... comando1 | tail -n 2 if [ $? -ne 0 ] then echo "comando1 fallito." EXIT_CODE=1 fi .... |
Does not work because the exit code comes from the command tail
, which in my case is always 0.
So I Googled a bit and found more than a solution for this simple problem.
Continue reading »