How to raise and take care of shell scripts: Style
How to write shell scripts?
The most important: Visualize the script's structure, that means: Use indents, spend an empty line between code blocks to show their common contexts. It's on your own, HOW you show the structure (spaces, tabs, opening braces at the end of lines or at the beginning of the next line, the depth of indents, ...) but it's important, THAT you do it.
# bad style: no structure visible, errors are "pre programmed" if [ $a = "A" ] then if [ $b = "B" ] then c=C elif [$b = "X" ] then c=D else c=E fi elif [ $d = "D" ] then if [ $e = "E" ] then c=W fi fi # using indents, a possible way if [ $a = "A" ] then if [ $b = "B" ] then c=C elif [$b = "X" ] then c=D else c=E fi elif [ $d = "D" ] then if [ $e = "E" ] then c=W fi fi # using indents, another way if [ $a = "A" ]; then if [ $b = "B" ]; then c=C elif [$b = "X" ]; then c=D else c=E fi elif [ $d = "D" ]; then if [ $e = "E" ]; then c=W fi fi
Comments: Write down, what the next code block should do. Not this way: # now copy files, if the next code line starts with a cp command - that's obviously. But write, WHY your script does the job exactly in this way and not in another. Describe more complex commands (for instance a large regular expression in a sed) - not only for the poor people forced to use your scripts but mainly for your future yourself! Otherwise you will look at your code 2 weeks or 4 years later and will get mad trying to get used with your previous thoughts. My own experience ;-)
At beginning of all your scripts put together the reason, why you created the script, how to call it (which command line arguments are expected), what the script does and what result it returns, the date and reason of each change. Define a standard for yourself and use it consistantly.
Use a self describing name for your script, avoid using names like "test" - it's confusing to have to call scripts with names used by system or standard programs. Use variable names describing their use and expected content (not as shown above in the example). It's on your own, how long your names will be - it depends on your laziness and the readability of your script ;-)
Don't write too long code lines. If you want others to be able to read and understand your scripts: keep in mind, that they probably use another editor. If you're in doubt, split long commands or continue a command at the next line (place a backslash at the line's end). And if your script contains the 5th nested if-structure, you should wonder if you could do the job using more suitable tools.