
03-08-2005
|
|
Registered User
|
|
|
Join Date: Jun 2002
Location: Netherlands
Posts: 54
|
|
Scripting Best Practices
When I first had the need to write scripts, this was the first place I looked. Here's some of the things I always do:
- Before writing any code, know exactly what it is you want your script to do and plan for the unexpected (error-handling, false user-input,etc).
- Write clean code, use indents and use comments to explain parts of your code!!!
- Scripting works better if you feel comfortable using shell-commands on the host-system.
- Variables are your friends!
- Open another terminal and try out parts of the code to see its output on screen (good for those extra spaces that screw up IF-statements).
- In case of the above, sed is your friend.
- More friends: awk, head, tail, wc, grep, ps, etc...
- Apropos is your friend too.
- Get comfortable using a text-editor like vi, if all else fails I recommend UltraEdit which is extremely handy.
- Don't be affraid to ask someone else if you're stuck. You can find my share of stupid questions on this forum, but you know what? I just started out then. My position now? I write really really large scripts for my company and they have come to depend on them!
- Word of caution: sometimes scripts really stress a server. Try to be there the first few times they run, just in case.

- Use logging when needed. Create a log-function, so you don't need then ">" and ">>" to write to or append to a logfile. One typo can clear your logs. Using a log-function prevents this.
- Many of my scripts send me email. To stop the confusion
, I always print the name of the script in the mailmessage, you'll start to appreciate this!
|