Simplification of Shell Script
We may now be to write shell scripts but there is something essential we need to keep in mind when writing these shell scripts – and that is the style of our code. This one thing will always matter whether you are in school or in a professional environment because in comp sci your code often speaks for you and this is why you would want to keep it decent & in accordance with the basic rules of that script/language.
All programming languages have a different set of rules to follow.A simple example highlighting these differences would be the following : In python or java, we always care for variable names and care to put magic numbers/anything else in variables. This is often done for simplicity, for a better style, for a better understanding and in order to reduce the amount of words per line. However shell scripting does not entirely agree with the rule and it somewhat feels that putting things in variable only complicates code. Moreover, it is not only the necessity of variables that the two disagree with but there’s much more to the story
TIPS & TRICKS FOR SH- good code vs bad code
1. USAGE MESSAGES
Is your function expecting an input? If yes- is there any specific number of inputs you are expected? If yes, do not forget to write a usage message. It’s a bad habit not to consider this very basic scenario.
Are you expecting an input of a particular type? Again, should you or should you not write usage messages in this scenario? Sometimes usage messages are auto-generated in that case there’s no need to write another usage message unless your professor or supervisor wants to see a different usage message. So always double check, and be sure not to include in case it’s being auto-generated already- your script should made it quite clear.
2. DO NOT OVER-SIMPLIFY, GET THE CALCULATIONS RIGHT
When we look to shorten or simplify our code, we may over-simplify it accidentally and not realize how the meaning of the same code might have changed. Here’s a very simply example for you:
Suppose you are aiming to get 7 by your code when a=1 b=2 and c=3
Bad Code:
z =`expr b * c + a`
the line above may not always calculate it according to the basic mathematics rules. Hence we it may be better to do something like the following:
Good Code:
z=`expr $b*$c`
z=`expr $z+$a`
3. SIMPLIFY KNOWING THE FUNCTION OF THE TERMS YOU ARE USING
Suppose you are to write a shell script that must output the value of z where z=`expr b*c`
Most of you would write this in your script –> echo `expr $b *$ c`
Yes, the role of echo is that it prints, but here we are FAILING to understand what expr does. is that it displays/ prints the standard output since there is nothing else it can do with it, so a good and simple code can be something as the following:
expr $b * $c
4. DO NOT LET USAGE ERRORS AND STANDARD OUTPUTS CONFUSE YOU – KEEP IT SIMPLE WHEN YOU CAN
Suppose I have to display what a file contains, but you must also give it appropriate error message if the file doesn’t exist
Bad Code
cat myfile
if ! [ -f myfile ]
then
echo usage: No such file or directory
fi
Good Code:
cat myfile
Notice: the usage message in this case will be given automatically if the file cannot be opened – meaning it’s not even a file –> No such file or directory error message. Also notice I have not directed the usage message in the bad code to standard error in this case “>&2” since it didn’t ask me to. I should normally direct it if I am anticipating an error.
5. AVOID UNNECESSARY TESTS & CONDITIONS
Adding unnecessary test condition to your code is the worse thing that you can do to your code. Please avoid this.
What do I mean when I say avoid unnecessary tests and conditions? We know the symbol “$?” tells us whether the previous command has been successfully executed or not and I often have seen people doing something like this
Bad Code
command
if S? -ne 0
then
Please avoid this! instead just put all of this on one line, and that’s exactly what if-statements are for anyways
Good Code:
if command
then
6. SHELL SCRIPT LIKES MAGIC NUMBERS & MAGIC WORDS
Unlike python and java, in shell scripts we do not need to worry about magic numbers and magic words. We do not care where they appear from. Please do not put them in variables and complicate things for yourself and for the shell scripting, instead just write the number out unless you really do need to store it for some reason.
Bad Code:
x=5
y=6
z= ` echo $x + $ y`
echo $z
Good Code:
expr 5 + 6
7. NO NEED OF DOUBLE QUOTES IN PRINTING
Unlike in python and java, in shell scripting here’s absolutely no need to put “” around a string that needs to be printed. If you are using these quotes the you are probably misunderstanding the concepts of shell scripting, because everything is a string in sh and so there’s no need of double quotes.
Bad Code:
echo “Hello”
echo “$x”
Good Code:
echo Hello
echo Sx
8. NO NEED TO SEPARATE STRINGS BY SPACES
echo naturally prints all the arguments separated by spaces so there’s no need to add spaces with echo
Bad Code:
echo $x ” ” $y OR echo $x $y
Good Code:
echo $x $y
9. REMEMBER TO DIRECT ANTICIPATED ERRORS TO STANDARD ERRORS USING “>&2”
10. REMEMBER TO USE THE RIGHT EXIT STATUSES
(1- to exit from an error, 0- to exit not from an error)
11. REMEMBER TO COVER ALL INPUTS BY THE SIGN “$#”
- Avoid checking each input separately like if $1… if $2
- Don’t only check the last input $3 (for example) and assume that the others are correct
12. USE DOUBLE QUOTES FOR UNTRUSTED USER INPUTS
Suppose you have asked user to enter two numbers and the user decides to enter something weird which your code fails to cover, in that case it is safer to perform operations on the quoted user input
Suppose you want to perform a multiplication
Bad Code:
expr $1 * $2
Good Code:
expr “$1” * “$2”