[GRASS-dev] [bug #5499] (grass) bash scripts starting with #!/bin/sh

Another bunch of possible bashism, in regard too Hamish'es hints on how to
avoid them.

Hamish wrote:

use "=" not "==" for string compares

These do not conform:

d.correlate: if [ "$name" == "" ] ; then
d.correlate:if [ "$ok" == "no" ] ; then
d.polar:if [ $TOTALVALIDNUMBER == 0 ] ; then
d.vect.thematic: if [ "`db.columns table=$table database=$database
driver=$driver| grep -i grassrgb`" == "" ] ; then
g.mlist: if [ "$itype" == "all" ] ; then
r.fillnulls:r.mapcalc "MASK=if($TMP1.buf==2,1,null())"
r.plane: if (dist==0) {
r.plane: if (typeflag==""){
v.rast.stats: r.mapcalc "MASK=if(${VECTOR}_${TMPNAME} == $i, 1, null())" 2>
/dev/null

Also, I guess (and only guess) that -+ and += would fail for non-Bash. There
are some in r.tileset:

r.tileset: for ((min_i=0;min_i<min_n;min_i+=1)) ; do
r.tileset: for ((max_i=0;max_i<max_n;max_i+=1)) ; do
r.tileset: for ((pp_i=0;pp_i<pp_max;pp_i+=2)) ; do
r.tileset: for ((sl_i=0;sl_i<sl_max;sl_i+=2)) ; do
r.tileset:for ((xi=0;xi<ximax;xi+=1)); do
r.tileset: for ((yi=0;yi<yimax;yi+=1)); do

Should min_i+=1 be replaced with `expr $min_i + 1` etc.?

Maciek

-------------------------------------------- Managed by Request Tracker

Maciek Sieczka via RT wrote:

Also, I guess (and only guess) that -+ and += would fail for non-Bash. There
are some in r.tileset:

r.tileset: for ((min_i=0;min_i<min_n;min_i+=1)) ; do

The "for ((...))" syntax is a bash-ism. If you want portability, you
have to use e.g.:

  min_i=0
  while $min_i -lt min_n ; do
    ...
    min_i=`expr $min_i + 1`
  done

Should min_i+=1 be replaced with `expr $min_i + 1` etc.?

Hamish decided that r.tileset was too bash-specific to fix, and just
put #!/bin/bash at the top.

--
Glynn Clements <glynn@gclements.plus.com>

[this matters as the new Ubuntu uses [d]ash for #!/bin/sh not bash]

Maciek Sieczka via RT wrote:

Another bunch of possible bashism, in regard too Hamish'es hints on
how to avoid them.

(as collected from this list mostly)

Hamish wrote:
> use "=" not "==" for string compares

These do not conform:

d.correlate: if [ "$name" == "" ] ; then

# use -z to test if string is zero-length, -n to test if non-zero length
if [ -z "$name" ] ; then

d.correlate:if [ "$ok" == "no" ] ; then

# only use a single "=" for string compares
if [ "$ok" = "no" ] ; then

d.polar:if [ $TOTALVALIDNUMBER == 0 ] ; then

# use -eq integer compare
if [ "$TOTALVALIDNUMBER" -eq 0 ] ; then

d.vect.thematic: if [ "`db.columns table=$table database=$database
  driver=$driver | grep -i grassrgb`" == "" ] ; then

Use -z. Note d.vect.thematic also uses "let" in a several places which
needs to be changed to `expr`.

g.mlist: if [ "$itype" == "all" ] ; then

=

r.fillnulls:r.mapcalc "MASK=if($TMP1.buf==2,1,null())"

"==" is ok within a r.mapcalc expression. Not a bug.

r.plane: if (dist==0) {

-eq

r.plane: if (typeflag==""){

-z

v.rast.stats: r.mapcalc "MASK=if(${VECTOR}_${TMPNAME} == $i, 1,
null())" 2> /dev/null

ok within r.mapcalc. Not a bug.

Also, I guess (and only guess) that -+ and += would fail for non-Bash.
There are some in r.tileset:

As Glynn already noted, r.tileset is written as a bash program, not a
unix shell script, and it is more work than I want to undertake to
rewrite it.

Hamish

Hamish wrote:

> r.plane: if (dist==0) {

-eq

> r.plane: if (typeflag==""){

-z

These two are part of an "awk" program, so should be left unchanged.

--
Glynn Clements <glynn@gclements.plus.com>

Ok, I've changed d.correlate, d.polar, d.vect.thematic and g.mlist.
The other hits were either awk, r.mapcalc, or r.tileset :slight_smile:

BTW there are lots of 'if test "$GISBASE" = ""; then' ; was this a
preferred method to -z for some reason? Is there some risk involved?

Daniel.

On 3/18/07, Hamish <hamish_nospam@yahoo.com> wrote:

[this matters as the new Ubuntu uses [d]ash for #!/bin/sh not bash]

Maciek Sieczka via RT wrote:
> Another bunch of possible bashism, in regard too Hamish'es hints on
> how to avoid them.

(as collected from this list mostly)

> Hamish wrote:
> > use "=" not "==" for string compares
>
> These do not conform:
>
> d.correlate: if [ "$name" == "" ] ; then

# use -z to test if string is zero-length, -n to test if non-zero length
if [ -z "$name" ] ; then

> d.correlate:if [ "$ok" == "no" ] ; then

# only use a single "=" for string compares
if [ "$ok" = "no" ] ; then

> d.polar:if [ $TOTALVALIDNUMBER == 0 ] ; then

# use -eq integer compare
if [ "$TOTALVALIDNUMBER" -eq 0 ] ; then

> d.vect.thematic: if [ "`db.columns table=$table database=$database
> driver=$driver | grep -i grassrgb`" == "" ] ; then

Use -z. Note d.vect.thematic also uses "let" in a several places which
needs to be changed to `expr`.

> g.mlist: if [ "$itype" == "all" ] ; then

=

> r.fillnulls:r.mapcalc "MASK=if($TMP1.buf==2,1,null())"

"==" is ok within a r.mapcalc expression. Not a bug.

> r.plane: if (dist==0) {

-eq

> r.plane: if (typeflag==""){

-z

> v.rast.stats: r.mapcalc "MASK=if(${VECTOR}_${TMPNAME} == $i, 1,
> null())" 2> /dev/null

ok within r.mapcalc. Not a bug.

> Also, I guess (and only guess) that -+ and += would fail for non-Bash.
> There are some in r.tileset:

As Glynn already noted, r.tileset is written as a bash program, not a
unix shell script, and it is more work than I want to undertake to
rewrite it.

Hamish

_______________________________________________
grass-dev mailing list
grass-dev@grass.itc.it
http://grass.itc.it/mailman/listinfo/grass-dev

--
-- Daniel Calvelo Aros

Daniel Calvelo wrote:

Ok, I've changed d.correlate, d.polar, d.vect.thematic and g.mlist.
The other hits were either awk, r.mapcalc, or r.tileset :slight_smile:

Cool! Thanks Daniel.

BTW there are lots of 'if test "$GISBASE" = ""; then' ; was this a
preferred method to -z for some reason? Is there some risk involved?

I don't know myslef. Maybe Glynn knows?

Maciek

Daniel Calvelo wrote:

Ok, I've changed d.correlate, d.polar, d.vect.thematic and g.mlist.
The other hits were either awk, r.mapcalc, or r.tileset :slight_smile:

BTW there are lots of 'if test "$GISBASE" = ""; then' ; was this a
preferred method to -z for some reason? Is there some risk involved?

If there's a shell out there which has a problem with -z, it isn't
going to like the configure scripts which autoconf generates, as they
use -z to test for an empty string.

--
Glynn Clements <glynn@gclements.plus.com>