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?
1
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 ...
endfor
endfor
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.
1
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))
4