best practises for scripting + a few unclear points


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting best practises for scripting + a few unclear points
# 1  
Old 11-08-2011
best practises for scripting + a few unclear points

Hi guys,

Besides the points bellow, what would best practices for scripting be ?

1) set the PATH
2) unset the current environment (set -u ?)
3) (re)set the IFS to default value - space (IFS="" <- is this correct ?)
4) check the return code for each action inside the script (cd, rsync, cp, mv, etc)
5) use quotes around variables (avoid white spaces - ex: "$var")
6) always indent (for, while, until, case, etc) loops


Thx for taking the time to answer.


da1,
script kiddie trying to learn the correct way of scripting Smilie
# 2  
Old 11-12-2011
I don't know why ou'd really need to do #2, and I'm not totally sure what a portable way to do it would be. Normally, you need PATH, LD_LIBRARY_PATH, but scores of environment variables are needed by different commands.

#3 unset IFS

#4 you dont have to check the return code per se; you can set up an execution chain, and if anyone breaks, there's been an error:

Code:
cd /var/www && rsync . /new/dir && cp /something /new/dir

#5 yes. Use "$@" when referring to the entire argument list and "$var" in almost every case. (Sometimes you need to use single-quotes, sometimes you want to let it expand with spaces and you want no quotes).

#6 uh, whatever. Smilie syntax and indentation aren't standard. Two common ways:
Code:
if [ "go" = "$1" ] ; then
   while [ -f /var/lock/status ]; do 
        run_command
   done
fi

And

Code:
if [ "go" = "$1" ] 
   then
      while [ -f /var/lock/status ]
        do 
           run_command
        done
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Unclear pointer and array

Hello, The purpose of the program is to print a sub string from the prompt inputs. I do not understand why char pointer does not work but char array will for line 40 and Line 41. ./a.out thisisatest 0 8 substring = "thisisat"And my code is: #include <stdio.h> #include <stdlib.h> #include... (29 Replies)
Discussion started by: yifangt
29 Replies

2. Red Hat

Mount Points? How?

Hi folks, I have been asked to performed the following: Add the following new moint points systemA:/avp and SystemB:/usr/sap/trans to be the new linux server ZZZ How can I add those mount points and how those mount points can become another linuz server?:wall::wall::wall: (2 Replies)
Discussion started by: 300zxmuro
2 Replies

3. Shell Programming and Scripting

Aggregated points

Combine points of specific key (a1) based on user defined size (lets say 200 in this example). so a1 191 and 191+200 and sum of all the values (4th column) and vice versa... Thanx a bunch! a1 191 201 1 a1 201 211 2 a1 211 221 1 a1 ....... .... a2......... ........ (7 Replies)
Discussion started by: quincyjones
7 Replies

4. Post Here to Contact Site Administrators and Moderators

Points?

Has any thought been given to assigning points to threads much in the way the HP ITRC forums do? This might not be possible, just a thought. (1 Reply)
Discussion started by: candlejack
1 Replies

5. UNIX for Advanced & Expert Users

mount points

hi, I believe a mount point does not have to be a physical disk, but rather a logical one? Is this correct? if so, how can I find out if my mount points are on different physical disks? thanks (9 Replies)
Discussion started by: JamesByars
9 Replies

6. Shell Programming and Scripting

How can I get entries between two points

Hi, I am trying to write a script to get entries between two points lets say start and end points from a log file, the log file time format is as follows Start - 07/Aug/2008:18:26:43 End - 07/Aug/2008:19:36:43 I tried using the following awk command but it couldnt pick up the entries... (3 Replies)
Discussion started by: openspark
3 Replies

7. UNIX and Linux Applications

Gnuplot question: how to plot 3D points as colored points in map view?

I have a simple gnuplot question. I have a set of points (list of x,y,z values; irregularly spaced, i.e. no grid) that I want to plot. I want the plot to look like this: - points in map view (no 3D view) - color of each point should depend on its z-value. - I want to define my own color scale -... (0 Replies)
Discussion started by: karman
0 Replies

8. UNIX for Dummies Questions & Answers

Best practises for backing up

Hi, I'm about to start a regular backup schedule for my Linux system. I need some pointers if I may :) The system is *mainly* used as a personal home computer (it's actually a laptop running SuSE 9.2) although I do host some client material from it being a PHP developer. I know that in... (2 Replies)
Discussion started by: d11wtq
2 Replies

9. UNIX for Advanced & Expert Users

mount points

sometimes in Solaris 8 when I go to mount filesystems using either the mount command or by editing the /etc/vfstab, i get a nice little error message saying the the number of allowable mount points has been exceeded. I have read man pages until I am blue in the face and no where can I find what the... (3 Replies)
Discussion started by: manderson19
3 Replies
Login or Register to Ask a Question