Operating System - HP-UX
1834437 Members
2253 Online
110067 Solutions
New Discussion

Re: Unix Developer Guide - Please post your Tips.

 
SOLVED
Go to solution
Ralph Grothe
Honored Contributor

Re: Unix Developer Guide - Please post your Tips.

Hi,

here are my suggestions (though I'm not sure if I'm in a position to suggest or even advise)

Do use shell scripts only for small scripts.
Sooner or later you will end up in copying and pasting.
Instead use a programming language which is highly modularizable and makes up for rapid development (or prototyping).
The ideal language, especially in a Unix environment, to me seems to be Perl (but Python is also neat, maybe it forces you to write more readable code;-)

If you use Perl,
DON'T REINVENT THE WHEEL!
always check here first:
http://www.cpan.org/
chances are someone came up with a nice module already that exactly fits your problem.

Then the usual Perl advice,

get into the habit to use these pragmas in your scripts:

use warnings;
use strict;

If you code for an untrustworthy environment (e.g. CGI, setuid etc.) activate Perl's taint checking -T

As so much was posted with regard to temporary files, if you really need those (I think you hardly ever need them in Perl as you can build up fancy data structures) instead of making up a filename with your PID $$ rather use

IO::File->new_tmpfile()

Always check if opening of files, pipes, sockets has been successful (e.g. die idiom) before attempting to read/write.

When there are chances for race conditions use locking
e.g.
use Fcntl ':flock';

Use shell escapes as rarely as possible
(these are those notoriuos qx, `...`, open FH, "$cmdstr |", system/exec $single_cmdstr)
Instead use Perl built-ins (or CPAN modules).
They are safer and quicker.
Also try to avoid globbing.
Avoid the shell by invoking exec/system with a list of args rather than a single string (see: perldoc -f exec)

Document your code through POD.
It is easy to learn.

Put reusable code in a module.

Share your code and submit your modules to CPAN.

Discover the wealth of POD that already was installed on your system,
start with

perldoc perl


Regards
Ralph

p.s. sorry for the Perl bias

Madness, thy name is system administration
joe_91
Super Advisor

Re: Unix Developer Guide - Please post your Tips.

Hi Team:

Thanks for all the inputs to the guide. It was very useful. Any Gurus who might have not posted to the guide, here is another chance to share your expertise. Thanks once gain. This is a great forum.

Thanks
Joe.
Vicente Sanchez_3
Respected Contributor

Re: Unix Developer Guide - Please post your Tips.

Hi Joe,

I think all is said.

Here I post a link I found time ago and I think its important.

http://www.shelldorado.com/goodcoding/cmdargs.html

Regards, Vicente.
T. M. Louah
Esteemed Contributor

Re: Unix Developer Guide - Please post your Tips.

-Suppose we have a large log file containing the following info `cat afile` :
/tmp/tarif/member
/tmp/user1
/home/tarif
failed
/usr/share
/var/tmp
failed
...

-The following script will extract only lines that are followed by failed keyword :
# for i in `awk '$0 == "failed" {print NR}' afile`
> do
> (( x = $i - 1 ))
> awk 'NR == '$x' {print}' afile
> done

the output of the script will return:
/home/tarif
/var/tmp

Cheers,
T??
Little learning is dangerous!
John Payne_2
Honored Contributor

Re: Unix Developer Guide - Please post your Tips.

Joe,

The worst thing our applications guys do to us is make their apps run in a 'debug' or 'verbose' mode in development, and then forget to turn it off when moving to production. Where possible, if you must log, make the messages that come be brief, too the point, and necessary. Lots of times, there really is not a need to log every single step of a process, every time.

Also, make error messages inituative. My favorite error the goes against this rule is 'Access Denied'

Hope it helps.


John
Spoon!!!!
joe_91
Super Advisor

Re: Unix Developer Guide - Please post your Tips.

Thanks. Can someone have some good points for packaging. I mean SD-UX or swpackage?

Thanks
Joe.