Difference between revisions of "Script portability"

From GRASS-Wiki
Jump to: navigation, search
Line 26: Line 26:
 
</source>
 
</source>
  
== Arithmetics ==
+
==== Arithmetics ====
  
 
Expression with $(( ... ))                 
 
Expression with $(( ... ))                 
Line 40: Line 40:
  
  
== The $(command) expression versus `command` ==
+
==== 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.
 
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.

Revision as of 05:34, 4 May 2012

Make GRASS shell scripts 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