The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Basic Java Persistence API Best Practices iBot Oracle Updates (RSS) 0 06-06-2008 07:10 PM
Oracle 10G best practices on Power6 AIX5.3 JodyTek AIX 1 05-07-2008 07:41 AM
Korn Shell Best Practices mtravis Shell Programming and Scripting 1 02-14-2008 03:11 PM
scripting guru's pls help me with scripting on AIX thatiprashant Shell Programming and Scripting 1 01-20-2006 07:58 PM
User generated FAQ and Best Practices section kduffin Post Here to Contact Site Administrators and Moderators 5 11-21-2003 09:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 03-04-2005
toddjameslane toddjameslane is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 2
Scripting Best Practices

Hi - I am new to this and was wondering if some of you can help me out. I am just starting to write scripts and need some guidelines on creating scripts. I'm calling them "Best Practices"...what should I do and not do when creating scripts.

All I know so far is that I should avoid putting usernames/passwords in scripts and I should avoid hard-coding a host name in a script. I would think there are a lot more of things like this, so if you could help me out, I would greatly appreciate it.

Thanks!
Todd
  #2 (permalink)  
Old 03-08-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Quote:
Originally Posted by toddjameslane
Hi - I am new to this and was wondering if some of you can help me out. I am just starting to write scripts and need some guidelines on creating scripts. I'm calling them "Best Practices"...what should I do and not do when creating scripts.

All I know so far is that I should avoid putting usernames/passwords in scripts and I should avoid hard-coding a host name in a script. I would think there are a lot more of things like this, so if you could help me out, I would greatly appreciate it.

Thanks!
Todd
Use this link for advanced scripting as,
http://www.tldp.org/LDP/abs/html/index.html

HTH.
  #3 (permalink)  
Old 03-08-2005
indo1144's Avatar
indo1144 indo1144 is offline
Registered User
  
 

Join Date: Jun 2002
Location: Netherlands
Posts: 54
Red face 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!
  • Use functions.
  • 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!
  #4 (permalink)  
Old 03-10-2005
indo1144's Avatar
indo1144 indo1144 is offline
Registered User
  
 

Join Date: Jun 2002
Location: Netherlands
Posts: 54
Log-function

In a private message, I was asked how the log-function worked that I described above. Since this might be helpful to other people too, I'm answering the question in public.

First of all, I use Bash, don't know if this trick works in other Shells.

Content of script:

Code:
#!/bin/bash
. lib/functions.conf # mind the SPACE between the DOT and the path/filename!!! 
logfile=scheduler.log
func_eventlog "STARTING ABG JOB"
-=script runs some more, but that's irrelevant, the example is clear=-
Content of file: lib/functions.conf which by the way is executable :

Code:
################################################
#                                              #
# Log functie                                  #
#                                              #
# Usage:                                       #
#                                              #
# func_eventlog "Dit wil ik loggen"            #
#                                              #
# Variabelen:                                  #
#                                              #
# $logfile (logfile inclusief pad)             #
#                                              #
################################################

func_eventlog()
{
	echo -e "`date` - $1" >> $logfile
}
So whenever I want something logged, I just use:

func_eventlog "Send this text to logfile"

and I never ever have to worry about accidentally overwriting my logs instead of appending to them.
  #5 (permalink)  
Old 03-16-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
and ...

* keep the same form throughout for your code ... inconsistent coding styles make debugging a lot harder regardless of who's doing it ... look at the 3 "if" forms below that people use in ksh scripting --- they do the same things but debugging could be prolonged if you used all of them in the same script instead of just 1 as the patterns are different ...

Code:
form 1: 

if [ cond ]; then
    command
fi

form 2:

if [ cond ]
then
    command
fi

form 3:

[ cond ] && command
* keep the code as simple as possible and then add to it when the basic form works ... write a quick script to test out the functionality first ... if that works, put in error checking and test ... more error checking and test ... you don't want to spend hours on the "perfect" script to learn later that it doesn't really do what you want it to do

* try not to hardcode file and/or directory paths ... makes it easier to test if you could use this or that file instead of the production file or directory ... like indo says --- variables are your friends!

* test as much as possible on a non-production server that closely mirrors the production environment ... unless you want to risk losing live production data --- that could be detrimental to your employment
  #6 (permalink)  
Old 03-26-2005
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
I think all of this leads to a few words that my prof in college taught me.


Modularity...

Write code in modules/sections so that nothing is hard coded where possible and that sections of the code can be excluded/replaced/deleted and the program will still function with no other modification.

Documentation....

If your programming out lives you, the next person will need to know what the heck you were doing. reading the code can be confusing, but comments help greatly.

Use of echo...

When troubleshooting your scripts insert echo commands in loops and wherever a decision is made in a forking statement like if/then/else or while/true or in case statements..... this aids in making sure to test all your logic down every path.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0