shell scripting coding guidelines /checklist

Hi ,

Can you please share any coding guidelines / standards available for shell scripting ?

Thanks,
Magento
LVL 5
magentoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jsaunConnect With a Mentor Commented:
If your script expects input, test it with invalid input (as well as valid).  Typos, nulls, negative numbers, spelling errors.

If your script connects to a resource, how does it behave if the resource is not available?

If your script might be called by a hacker, check for stuff like SQL injection.   The best rule is: only accept data you are expecting to see.  If the data looks bad, better to fail safe.

That sort of thing.

And in a related note:  write out lots of logging data.  Logs help you find and fix problems later.  If you want to get fancy, you can put a debug switch in and use that to turn off most of the logging later on, but rarely will you regret having good logs
0
 
ozoCommented:
Which shell?
0
 
magentoAuthor Commented:
Ozo ,

I am planning to use BASH.

Thanks
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
jsaunConnect With a Mentor Commented:
There are some style guides out there that you can read.  Take the good parts, and have good reasons for what you disagree with.   Mostly check with your client or employer and see what they require.

My top tenets:

Make your code read like prose.  Long, descriptive variable names are more clear for others to understand when they come along years later to tweak your code.  This means never use a loop iterator named "i".  If you're looping over servers in a list, then do something like  
for currentServer  in $serverList

Plan it out before you start writing it.  Remember doing flowcharts in college?  It's still a good idea.  Diagrams help you make sensible functions, and help you discover corner cases before you're coding.

Have someone buddy check as you go along, and test frequently.  

Write your test cases before you write your code.

Here are some style guide links:

http://wiki.bash-hackers.org/scripting/style
https://google-styleguide.googlecode.com/svn/trunk/shell.xml
http://tldp.org/LDP/abs/html/scrstyle.html 

Cheers and good luck
0
 
magentoAuthor Commented:
Hello Jsaun,

Thanks for the ideas, i will follow it.
I am new here, can you please share knowledge on writing test cases ?

Thanks
0
 
magentoAuthor Commented:
Exactly what i'm looking for . Thank you so much !!!
0
 
magentoAuthor Commented:
jsaun,Thanks again!!!
0
All Courses

From novice to tech pro — start learning today.