[GRASSLIST:9977] RE: bash scripting

Aaron Racicot aaronr at ecotrust.org
Tue Jan 24 16:32:07 EST 2006


James,
I have used the batch-grass.sh script with great success in the past,
and more recently (last year) was using a php script that would write
out an equivalent file and exec it from PHP for some Mapserver
integration.  Basically they set up the needed environment variables to
run scripted grass routines.  The PHP I have included below is just a
snip of a larger class specification, so you will see things like
this->xxx.  I just wanted to include it as an example for you.  Anyway,
I am not sure if there is a newer or preferred batch-grass.sh version
out there.  Let me know if this stuff helps!

Aaron

+----------------------------------------+
| Aaron Racicot  | aaronr at ecotrust.org   |
| GIS Programmer | 503.467.0759          |
+----------------------------------------+
| e c o t r u s t                        |
| Jean Vollum Natural Capital Center     |
| 721 NW Ninth Avenue                    |
| Suite 200                              |
| Portland, OR 97209                     |
| www.ecotrust.org                       |
+----------------------------------------+

+++++++++++++++++++++++++++++++++++++++++++++++++++
 batch-grass.sh
+++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/sh
#Author: Andrew E Long
#        aelon at sph.umich.edu
# ael, Mon Nov 16 08:54:15 EST 1998
# update by A. Prasad 20. Jan. 2000
#           aprasad/ne_de at fs.fed.us
# $Date: 2002/06/27 13:29:12 $

### Installation: Just edit paths for GISBASE, GISDBASE and GISRC
#customize this:
GISDBASE=/home/aaronr/GRASSDATA
GISBASE=/usr/local/grass-6.0.0
GISRC=/home/aaronr/.grassrc6

############ nothing to change below (I think)

if test $# -eq 0
then
    cat <<EOF

                            batch-grass usage:

    batch-grass runs a command (or executes all commands in a file) in
the
grass environment. It simply sets some environmental variables and
executes
the commands.

        batch-grass location_name mapset command
        batch-grass location_name mapset -file filename

e.g.,

        batch-grass usgs PERMANENT -file make-quads

EOF
        exit 0
fi


ETC=$GISBASE/etc
PATH=$GISBASE/bin:$GISBASE/scripts:$GISBASE/garden/bin:$PATH:/usr/bin:/u
sr/local
/bin/
cat << EOF > $GISRC
GISDBASE: $GISDBASE
LOCATION_NAME: $1
MAPSET: $2
PAINTER: ppm
MAPLP: stuff.ppm
EOF

export GISBASE GISDBASE ETC PATH GISRC
export LOCATION_NAME=$1
export MAPSET=$2
export LOCATION=${GISDBASE?}/${LOCATION_NAME?}/${MAPSET?}

if test "$3" = "-file"
then
    cat $4 | sh
else
    # strip off the location and mapset,
    shift 2
    # then execute the command which remains:
    $*
fi

##/bin/rm -f $GISRC
exit 1




+++++++++++++++++++++++++++++++++++++++++++++++++++
 batch-grass.php
+++++++++++++++++++++++++++++++++++++++++++++++++++
function run_command($cmd)
{
  $std_output = "";
  $grass_exec = "";
  // Check that all the required info is available..
  if ($this->data_dir != NULL &&
      $this->rc_dir != NULL &&
      $this->bin_dir != NULL &&
      $this->location != NULL &&
      $this->mapset != NULL)
    {
      // Here we will write out the script to run...
      $fp_data = fopen($this->data_dir."/grass_run.sh","w");
      // write out text header
      fwrite($fp_data,"export GISBASE=".$this->bin_dir." \n");
      fwrite($fp_data,"export GISDBASE=".$this->data_dir." \n");
      fwrite($fp_data,"export GISRC=".$this->rc_dir."/.grassrc6 \n");
      fwrite($fp_data,"export ETC=".$this->bin_dir."/etc \n");
      fwrite($fp_data,"export
PATH=".$this->bin_dir."/bin:".$this->bin_dir."/scripts:".$this->bin_dir.
             "/garden/bin:\$PATH:/usr/bin:/usr/local/bin/ \n");
      fwrite($fp_data,"export LOCATION_NAME=".$this->location." \n");
      fwrite($fp_data,"export MAPSET=".$this->mapset." \n");
      fwrite($fp_data,"export
LOCATION=".$this->data_dir."/".$this->location."/".$mapset." \n");
      fwrite($fp_data,"\n");
      fwrite($fp_data,$cmd."\n");
      fflush($fp_data);
      fclose($fp_data);

      $fp_data = fopen($this->rc_dir."/.grassrc6","w");
      fwrite($fp_data,"GISDBASE: ".$this->data_dir."\n");
      fwrite($fp_data,"LOCATION_NAME: ".$this->location."\n");
      fwrite($fp_data,"MAPSET: ".$this->mapset."\n");
      fwrite($fp_data,"PAINTER: ppm\n");
      fwrite($fp_data,"MAPLP: stuff.ppm\n");
      fflush($fp_data);
      fclose($fp_data);

      $grass_exec = sprintf("chmod u+x
".$this->data_dir."/grass_run.sh");
      exec($grass_exec,$std_output,$std_error);

      $grass_exec = sprintf($this->data_dir."/grass_run.sh");
      exec($grass_exec,$std_output,$std_error);
      for ($ii=count($std_output)-1; $ii>=0; $ii--)
        {
          //echo $std_output[$ii]."<br>\n";
        }
      for ($ii=count($std_error)-1; $ii>=0; $ii--)
        {
          //echo $std_error[$ii]."<br>\n";
        }
    }
  else
    {
      echo "<b> There was an error executing the following
cmd:</b><br>\n";
      echo $cmd."<br>\n";
      echo "Make sure the following environment variables look
correct:<br>\n";
      echo "\$this->data_dir = $this->data_dir <br>\n";
      echo "\$this->rc_dir = $this->rc_dir <br>\n";
      echo "\$this->bin_dir = $this->bin_dir <br>\n";
      echo "\$this->location = $this->location <br>\n";
      echo "\$this->mapset = $this->mapset <br>\n";
    }
  return $std_output;
}



-----Original Message-----
From: owner-GRASSLIST at baylor.edu [mailto:owner-GRASSLIST at baylor.edu] On
Behalf Of James Conolly
Sent: Tuesday, January 24, 2006 12:56 PM
To: grasslist at baylor.edu
Subject: [GRASSLIST:9975] bash scripting


Hello all,

I need to run several hundred instances of r.los, and I'd like to do
this in a bash script that calls and runs GRASS (so that I can prefix
the script with a nohup command and logout and leave it running). If
somebody with more knowledge of bash scripting could please provide me
with some advice on how to best do this, I'd appreciate it very much. As
is stands, if the following script:

#!/bin/sh
GRASS60 -text /grassdata/mylocation/PERMANENT
r.los input=dem.v1 output=los.1 coordinate=1234,1234 max_dist=10000
r.los input=dem.v1 output=los.1 coordinate=6789,6789 max_dist=10000 end
0

is run, GRASS60 opens, then sits there waiting for a command. Only if I
then exit GRASS normally (i.e., exit), will it then move onto executing
the r.los commands, but obviously as I'm no longer in the GRASS shell, I
get a command not found. I understand all this, but my knowledge of bash
scripting is too limited to work out how to get the script to move onto
r.los to execute while in the GRASS shell...

Any/all help appreciated,

With thanks,

James Conolly
Trent University, Canada




More information about the grass-user mailing list