[colug-432] Portable Shell
Rick Troth
rmt at casita.net
Mon Oct 19 11:06:21 EDT 2015
On 10/18/2015 08:58 PM, jep200404 at columbus.rr.com wrote:
> I would like to write scripts that would work on
> as many Unix and Linux releases as possible of the past 25 years
> with the shells installed by default.
Awesome!
This is a great goal and generally achievable.
Three vital starting points:
1 -- use the script magic "#!/bin/sh" for all generic shell scripts.
Whatever shell that points to is expected to be Bourne compliant.
2 -- check return codes and be prepared to bail out or to accommodate
new and wonderful environments
3 -- try it! run your script on as many Unix systems as possible. CYGWIN
and the Berzerkeley variants and Minix are cheap to acquire. Use them.
if you can get AIX or Solaris or HP-UX, so much the better.
On 10/19/2015 09:44 AM, Rick Hornsby wrote:
> One of the biggest shell script portability issues I've run into - going between Linux and OS X (BSD variant) - is not with bash itself, but with tools like sed being slightly different. The solution on OSX is to use the gnu tools (i.e. gsed) from homebrew - but it's confusing until you know that's what's happening.
Right.
And this is one reason for my #2 above. Some of the differences will
actually throw an error. (Kind of them to alert you; not all utilities do.)
Maybe this is obvious, but avoid some of the fancier features, not only
of whatever shell you're in but also of the supporting programs. I can't
count the number of times I found a script which assumed that some cool
option would be available.
Don't be afraid to add steps and over-simplify.
Use 'if [ condition ]' which is plain but more readable.
Set RC=$? and then use $RC for checking and possible for actual 'exit'.
Stuff like that.
-- R; <><
More information about the colug-432
mailing list