views:

86

answers:

8

H all,

Hope all is well,

Can any one please let me know the coding guidelines along with code samples of unix shell scripting by using which the code can run on most of the current shells like ksh, bash, csh etc. Most of the times some of my code written for ksh would not work on normal sh. I want to make my code maximum portable. Most of my code will be around [ifs, elifs] and [whiles, fors] and return code capturing of child shell (which will mostly be running sql scripts for which i want to get some of the return codes like number of rows processed, return codes, error codes of the sql script).

Also can any one let me know which shell specific code can be easily ported to other shells. Can any one point me to right tutorials and/or sample codes?

Thanks in advance.

+2  A: 

why not write your scripts in perl or python?

joni
That was my first choice. Business reasons... they are not agreeing to use perl/python and they are not telling good reasons for that... so had to start with shell and shell is also a good choice... isn't it.... its also having good features.
Enjoy coding
yeah for many tasks shell script is really simple and efficient to use. But as soon as it comes to more complicated things, specially dealing with data, it's not my first opinion anymore. Just tell them that it will cost a lot more because you'll need much more time :P
joni
This choice is not in my hands... sorry friend... but guidelines to follow for good portable unix shell scripts is what i am currently looking for ... for the time being... thanks
Enjoy coding
a bourne shell sh should be installed on _every_ unix system. Sometimes /bin/sh is only a symlink to a more feature-rich, but backwards-compatible shell. So what you need are the scripting guidelines for the Bourne shell e.G. here http://www.ooblick.com/text/sh/
joni
+3  A: 

Get yourself a 7th Edition Unix Programmer's Manual (Vol 1) and program to the (Bourne) shell notation described there. You can actually upgrade to a System V (Bourne) shell if you can find an appropriate manual; it has a few extra features, like functions, that the 7th Edition shell did not have, and they are essentially available everywhere. (This is a very conservative stance - but it will give you maximal portability.)

The other issue is choosing which commands you use, and which options you use to those commands. For example, GNU grep has numerous useful options that are not available elsewhere. GNU sed similarly has extensions that are not available elsewhere. If you write your shell script to use those extensions, your code will be unportable, even if the shells all understand the syntax correctly.

I recommend being aware of what the POSIX standard says is portable. Most of the POSIX features are usually supported by most systems, but each system usually adds some extra options and features that are not always widely available. Note that the POSIX shell does have some features, notably $(...) command substitution in place of back-ticks that you were not in original Bourne shells. You'll have to decide whether the clarity that the more modern notation brings is worth the (nominal) loss of portability.

Also, the Autoconf shell guidelines concentrate on maximal portability. However, the result is a rather stilted version of shell scripting, not least because it has to interwork with the m4 macro processor, which leads to some additional constraints.

Jonathan Leffler
+1 for mentioning utility options and POSIX.
Dennis Williamson
+1 for POSIX and the autoconf guidelines (the relevant section of the autoconf manual is http://www.gnu.org/software/autoconf/manual/html_node/Portable-Shell.html
Norman Gray
A: 

There's some portability talk between shells in Bash Hackers Wiki. See, if that helps.

Veivi
+1  A: 

Get yourself Beginning Portable Shell scripting and see what the author have to say.

ghostdog74
A: 

There's also Steve Parker's Bourne / Bash online shell scripting tutorial or books such as "Shell Scripting Recipes: A Problem-Solution Approach" by Chris F.A. Johnson.

To test for maximum portability you may use bournesh.

http://freshmeat.net/projects/bournesh/

latto
Here's [the direct link](http://heirloom.sourceforge.net/sh.html) to the Heirloom Bourne Shell.
Dennis Williamson
+2  A: 

The short answer for portability is to write scripts for the lowest common denominator which is the Bourne shell (sh) and to avoid C shell which has an incompatible syntax and serious limitations. The Bourne shell should be universally available and most system startup and administration scripts are written for it.

All of the capabilities you mentioned as requirements are available in sh. One of the main things that is missing is support for arrays, but that can be worked around.

One thing to keep in mind is that much of the functionality of a shell script is provided by external utilities which are accessible from any shell.

Dennis Williamson
A: 

I've a few links from here: http://www.pixelbeat.org/programming/shell_script_mistakes.html#portability

pixelbeat
A: 

You can use Loker for checking for syntactic conformance to the POSIX standard.

Roman Cheplyaka