Why should I learn Shell Programming at all? What can be done with it in the real world? Could you show me some powerful things that can be done with it or some special features so as to convince me that I should start learning shell programming right now?
Share
There are a billion and one reasons to learn shell programming. One of the major reasons is systems administration.
Here is an example of somebody who needed to rename ~750 files based upon another file in that same directory. This was accomplished with 3 lines of shell scripting. To get many more examples just search for questions with the tags [bash], [sed] or [awk].
When somebody asks me to show them a cool example of “shell programming” I always show them this awk 1-liner (maybe even a 1-worder?). It will filter a list to only show unique values without changing the original order. This is significant because most other solutions require you to sort the list first which destroys the original order.
Explanation of awk command
The non-sorting unique magic happens with
!a[$0]++. Since awk supports associative arrays, it uses the current record (aka line)$0as the key to the arraya[]. If that key has not been seen before,a[$0]evaluates to0(zero) which is awk’s default value for unset indices. We then negate this value to returnTRUEon the first occurrence of that key.a[$0]is then incremented such that subsequent hits on this key will returnFALSEand thus repeat values are never printed. We also exploit the fact that awk will default toprint $0(print the current record/line) if an expression returnsTRUEand no further{ commands }are given.If you still don’t understand, don’t worry, this is a very terse and optimized version of what could be a much longer awk script.