Script portability

From GRASS-Wiki
Revision as of 04:32, 3 April 2013 by ⚠️HamishBowman (talk | contribs) (→‎Make GRASS shell scripts portable)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Make GRASS shell scripts portable

For the most part, avoid Bashisms, test with /bin/sh linked to 'dash', follow the advice in the SUBMITTING files, and have a look at the existing GRASS shell scripts for examples.

You might also consult the IEEE SUS/POSIX specification to check which features will be available with the BSD power tools, as they are fewer than the GNU power tools. (e.g. 'sed -i' and 'tac' do no exist on Mac OSX)

Some of what follows only really pertains to really ancient portability issues, and shouldn't be much of an issue for UNIX systems newer than ~ the mid 90s. Please file a bug if you notice that any of the current GRASS scripts fail on exotic or classic UNIX variants, as we aim to be widely portable.



Portable shell scripting is something of a black art, since with the evolution and derivation of the UNIX shell, the definition of "portable" is perhaps ambiguous.
_Simon__Nattrass_

Guidelines

Conditionals

Conditional with [ ... ] should replaced by __test__

               	         
if [ -f foo.c ] 
then
...
fi

as here:

 
if test -f foo.c
then
...
fi

Arithmetics

Expression with $(( ... ))

 
x=$(($x+1))

are more portable if using __expr__

              
x=`expr $x + 1`


The $(command) expression versus `command`

The $(command) may be supported in many modern Bourne shells but for pure Bourne shells use of `command` is favored, although this method tends to lead to confusion when the backquotes contain the characters $, ` and \. In such cases remember to use the character \ to escape these sequences.


References