Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I learn Shell Programming? [closed]

Tags:

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?

like image 712
fabio Avatar asked Dec 16 '10 18:12

fabio


People also ask

Why should we learn shell programming?

Shell scripting gives you the ability to automate commands and repetitive tasks. Armed with the knowledge of shell scripting, you'll be hunting down repetitive tasks in your life and scripting them out of existence. Learning to script your shell is the way to make it happen—don't be afraid to give it a shot.

Is it worth to learn shell scripting?

It's definitely worthwhile learning. You can use bash shell scripting on Linux, Cygwin, Windows (yes they have a bash shell - How to Install and Use the Linux Bash Shell on Windows 10 ), and Mac OS X. That said, if you want to do more sophisticated stuff, you can also use Groovy as a shell scripting language (Shebang!

When should shell programming not be used?

Q #11) When should shell programming/scripting not be used? Answer: Generally, shell programming/scripting should not be used in the below instances. When the task is very much complex like writing the entire payroll processing system. Where there is a high degree of productivity required.

Is shell scripting in demand?

While the demand continues to grow for Unix shell scripting talent, there remains a shortage of experienced professionals in the market. There are unlimited career advancement opportunities for developers and systems administrators who are skilled in Unix Shell Scripting.


1 Answers

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.

$ echo -e "apple\npear\napple\nbanana\nmango\npear\nbanana" | awk '!a[$0]++' apple pear banana mango 

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) $0 as the key to the array a[]. If that key has not been seen before, a[$0] evaluates to 0 (zero) which is awk's default value for unset indices. We then negate this value to return TRUE on the first occurrence of that key. a[$0] is then incremented such that subsequent hits on this key will return FALSE and thus repeat values are never printed. We also exploit the fact that awk will default to print $0 (print the current record/line) if an expression returns TRUE and 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.

like image 118
SiegeX Avatar answered Nov 11 '22 05:11

SiegeX