Retrieve the exit code of all the commands in a pipeline with “PIPESTATUS”

When developing shell scripts, it is common usage to check the exit code of a command in order to check if its execution was successful or not. It is pretty easy when you execute only one command, because the exit code can be find using the special parameter “$?”. But if you use pipelines (“command1 | command2 | …”), it is not that simple because “$?” will only give you the exit code of the last command of your pipeline.

So, today, I’m going to show you how to get the exit code for each command in a pipeline. Unfortunately, this only works in bash.

Continue reading “Retrieve the exit code of all the commands in a pipeline with “PIPESTATUS””

COUNT and “GROUP BY” with awk

I want to share with you a small shell script that I often use for reporting purposes, which allows you to simulate the famous “count and group by” well known from DBAs. No database is required. It only use “awk” which is installed with each Linux / Unix distribution (I think?), so you can use it “out of the box”.

In other words: the script allows you to count the number of time a word appears in a text file and display a formatted result.

Continue reading “COUNT and “GROUP BY” with awk”