views:

510

answers:

6

Hi All,

I know this question has kind-a started "religious" wars in past and there might not be one correct answer. But after working with ksh and csh for last 3-4 years and going through the pain of porting from one to another or applying a common piece of logic to multiple versions (read as legacy code), if I am writing a new script, I would go for ksh, but out of compulsion rather than choice. Is there a better option other than ksh/csh? Also something that is portable across Unixes (Solaris/HP/IBM/FreeBSD) and Linux (and if I am not asking too much or it if does make sense all Linux flavors)

Waiting for suggestions ...

Peace :) Devang Kamdar

+2  A: 

If you really want it to be portable (I don't know that any shell-script is maintainable), I would specify #!/bin/sh and test with dash and if possible other shells.

Matthew Flaschen
Debian-derivatives will also have the script `checkbashims` in the package 'devscripts' which will check for non-sh-compliant behavior in scripts.
Porges
+6  A: 

I would suggest plain old sh, which is available everywhere.

Also, it is worth noting that portability involves not only shell but also other commands used in a script such as awk, grep, ps or echo.

mouviciel
Definitely stick to the POSIX-compliant subset of functionality for commands. At least under Debian-derived distros, you can get the POSIX manpages as manpages-posix{-dev}, and you can then access (e.g.) POSIX awk's manpage via `man 1posix awk`.
Porges
You should note that "portability" doesn't automatically mean "POSIX". A portable script runs on most /bin/sh, even if that means that POSIX is not strictly followed. The only example shell with non-POSIX (but ''portable'') behaviour I currently can think of is maybe Sun's sh.
TheBonsai
Another standard worth mentioning is Single Unix Specification.
mouviciel
I think sh is pretty safe way to go but as pointed out by Stephen Darlington below, it might not have a lot of friendly features ... For now, I would like to take away from all suggestions that unless you are modifying an existing script, there is no reason to code a new one in shell. Perl/Python/Ruby alternatives make sense as they would be largely portable naturally and speed should not be a big concern here - thanks to TheMarko's post for this fresh perspective :)
Devang Kamdar
While portability is important; the question also contained "maintainable" as a keyword which you completely disregarded. "sh" scripts are NOT maintainable. They often rely on hacks to try and get the things you need done because POSIX sh or Bourne just doesn't provide the necessary features. I recommend bash if you *need* shell scripting, and perl/python/ruby if you can afford the dependency.
lhunath
sh scripts are maintainable the same way C source code is maintainable: write them with maintainability in mind. I have already used TDD on shell scripts.
mouviciel
+2  A: 

I would expect BASH to be the widest spread shell at the moment since it is the default for many Linux distributions (it can even run on Windows with cygwin, but that's probably true for the other shells, too). An alternative might be to not use the shell itself for scripting but one of the scriping languages out there like perl, python, ruby, ...

TheMarko
makes sense :) ...
Devang Kamdar
I meant using perl/python etc to use for scripting really makes sense :)
Devang Kamdar
+3  A: 

I usually use ksh. I find that it's a good compromise between features and portability. It's there (or a compatible version is available) on most Linux boxes and Solaris. It's a while since I used HP-UX (thankfully) but I'm pretty sure it was available there too.

If all the machines you need to support are modern, bash might be an option. Solaris 10 comes with a copy. It's the default on most Linux machines.

Your lowest common denominator is going to be Bourne (sh), so that's worth considering if portability is your main concern. It's missing some of the more friendly features of ksh and bash though.

It's still worth steering clear of csh/tcsh for scripting. Csh Programming Considered Harmful is an oldie but still largely relevant.

Stephen Darlington
I had read that one (Csh Programming Considered Harmful) before ... Its indeed a very good article ... Thanks for sharing
Devang Kamdar
ksh is *almost* a proper superset of Posix shell, and remains my choice for an interactive shell today. I also spent a lot of time on HP-UX, and ksh is the best shell that is available on some of the older versions. Had I started with Linux, I would likely have used bash.
semiuseless
+1  A: 

My answer would be perl.

Does everything 'sh' 'bash' etc can do in a nicer more elegant manner.

Also it is actually more portable. A given version perl is very consistant accross all platforms. There are no significant differences between the Linux, Solaris and AIX distributions whereas porting shell scripts between these platforms is a real pain.

And it works on all windows paltforms! Provided you avoid backticks and "system()" your script has a good chance of running.

James Anderson
+1  A: 

Python! Check out iPython, which is an enhanced Python interpreter. Also: Python for Unix and Linux System Adminitration.

You can write great portable scripts, and it's fun.

ShawnMilo