[GRASSLIST:3420] Re: GRASS scripting question

Glynn Clements glynn.clements at virgin.net
Sun May 16 06:01:55 EDT 2004

Thomas Adams wrote:

> I have generated 8760 (hourly) maps of radar precipitation fields that I 
> need to sum to monthly (and daily) totals using r.mapcalc within a Perl 
> script. I have tried *EVERYTHING* I can do with this but I keep getting 
> an error related to the shell not properly handling '+'. Namely:
> $cmd = "r.mapcalc ". $newmap . "=". $map1; 
> system $cmd;    <--- this works just fine.
> But when I try:
> $cmd = "r.mapcalc ". $newmap . "=". $map1 ."+". $map2; 
> system $cmd;    <--- this fails.
> saying that "+map2" is an invalid command, but the "r.mapcalc 
> ".$newmap."=".$map1 part suceeds. I have also tried using:
> $cmd = "r.mapcalc ". $newmap . "=". $map1 ."\+". $map2; 
> When I try using Perl's 'join' command I get the same error.
> How do I do this in a script? Obviously, at the GRASS prompt, 
> interactively, r.mapcalc works for me.

I don't know perl well enough to follow exactly what you are trying to
do, however there are two ways in which you can bypass the shell
altogether, and hence not have to worry about shell syntax.

1. If run without arguments, r.mapcalc will read the expression from

2. Perl's system function only uses the shell when passed a scalar or
a single-element array. If you pass it a array with more than one
element, it will bypass the shell. r.mapcalc allows the expression to
be split across multiple arguments; it will just join the arguments
with spaces in between then evaluate the resulting string.

Glynn Clements <glynn.clements at virgin.net>

More information about the grass-user mailing list