I am maintaing several data process shell scripts which are full of if-else statements and for loops . I try to make the scritps tidy and easy to debug.

When I read some suggestions about shell code best practice, it seems that is is not a best practice to have many if-else and for loop statements.

So what should I do, to write a python or c++ script instead of those logical process?

  • 10
    The same rule that applies to all other kinds of programs applies to shell scripts: don't make a mess. – Blrfl Oct 14 '12 at 16:03

It's never a good idea to have too many embedded if/else or loops, but in their own right, lots aren't inherently a bad thing.

Consider splitting out the loops unto utility functions. For example, if you have:

for stuff ..
   for stuff ...


It would be much better read as

findAllPricesForProducts( extractNamePriceTuple( data ) )

with the utility functions defined appropriately. If you then document each function and give an example input/output, your code should be both readable and maintainable.


As a perl coder (this could easily be adjusted to python) I have heard said and said myself "any shell script longer than 10 lines should be a perl script." Shell scripts above a certain threshold in size/complexity often become nightmarish to maintain. Switching to a preferred scripting language can drastically simplify this (and get away from using repeated calls grep, awk, and sed (or other lesser known applications).

Language choice aside, another comment I have heard is "if the function requires the word 'and' to describe its functionality, it is too big/complex." It isn't a problem to have nested statements, but if the logic around the statements becomes complex one should consider to extract parts of it so that it is something one can keep in their head while debugging and be able to simply test the functionality of it.


There's no issue with using if- and for-statements in a shell script. Is your code deeply nested in many if/for-statements?

Don't forget that shell languages have user-defined functions should you with to make your code more modular for maintainability.


The main argument against writing complex shell scripts is that they are difficult to debug and easy to break. That said, simply taking the same methodology into another language wouldn't be an improvement - it's pretty much inherent if what you are doing is stitching together a bunch of other programs.


If you should rewrite the scripts in a language like python depends much on

  • how many scripts with how many lines of code you have

  • how many new requirements you expect to be added to them in the nearby future (if there are none, why bother?)

  • how messy they really are

  • how sure you are that you can implement them really better without introducing new bugs

And before doing any rewrite, even it is a small one like it seems to be in your case, please read this famous article of Joel Spolsky why a rewrite is often a bad choice.

  • Do the scripts that you have work? Are they broken? What are the changes in requirements. Does the tool fit the task? – ChuckCottrill Oct 11 '13 at 0:14

As in any language...

  • Try never to write the same logic more than once
    • Maybe a script with many if-then statements can be reduced to a few functions.
  • Loops (do, for, while, repeat, until), by their nature, are semi-self-contained engines. They're analogous to electric motors in a complex machine. Errors in them are almost universally the cause of all "freeze", "lockup", "100% CPU" and similar catastrophic bugs.
    • Use sparingly, I would suggest never to have multiple similar loops. "Genericize" the code so the same loop can be put in a function and reused with different variables and conditions. (from one who has taken days to see an 'l' (el) variable that should have been an 'i' (eye))
  • Did not exactly get the motor analogy – Dimitrios Mistriotis Feb 2 '13 at 12:24
  • A lot of complex machines like laser printers and disk drives utilize multiple electric motors which are devices that work by spinning (looping). The repetitious action is turned into non-repetitious action (loading paper, moving the read/write head) by components that actuate only every X number of turns or in some other exceptional way that is analogous to an if-then statement inside a loop. In OOP terms, we'd call this an "exception". – DocSalvager Feb 2 '13 at 12:46
  • @DocSalvage Doing something every N iterations in a loop isn't an exception, since exceptions are for when the code cannot continue normally. Also, exceptions don't necessarily require OOP. – user76704 Feb 2 '13 at 15:32
  • If-then, try-except, for-loop, event-processing are forms of "software interrupts" which are extensions of the CPU's "branch instruction" which is itself an extension of true hardware interrupts. Some form of interrupt processing exists in most languages though it may only be as limited as the lowly shell script 'trap' command. – DocSalvager Feb 3 '13 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.