[GRASS-user] Batch Processing in GRASS 6.2 w/ Cygwin

I am trying to write a batch file for Cygwin that will run multiple GRASS r.le.pixel commands, change the region, and rerun commands on the new region. I have a simplified version based on a previous posting to this listserv. I have tried using it under #!/bin/sh and #!/bin/ash and receive error with both. Please help. Thank you.

Under #!/bin/sh - Does not produce the testing.txt file

$ ./batch-grass6v3.sh
: No such file or directory4: testing.txt
: No such file or directory5: testing.txt
: No such file or directory6: testing.txt
': not a valid identifier 21: export: GISBASE ': not a valid identifier 22: export: GISRC
./batch-grass6v3.sh: line 25: $‘g.version\r’: command not found
/etc/clean_temp: No such file or directoryrass-6.2.2

Under #!/bin/ash

Produces the testing.txt file but nothing else.

$ ./batch-grass6v3.sh
export: Files/ATI: bad variable name
g.version: not found

Here is the current version of the script:

#!/bin/sh

Customize this:

#------------------------------------------------------------------------------

Nothing to change below (I think)

#==============================================================================

Usage Message

GISRC=“~/testing.txt”
GISDBASE=“/cygdrive/c/Data/WBEA”
GISBASE=“/usr/local/grass-6.2.2”

generate GRASS settings file:

#------------------------------------------------------------------------------

the file contains the GRASS variables which define the LOCATION etc.

echo $GISDBASE > testing.txt
echo “LOCATION_NAME: newset” >> testing.txt
echo “MAPSET: C1” >> testing.txt
#------------------------------------------------------------------------------

exports

#------------------------------------------------------------------------------
export LOCATION_NAME=newset
export GISBASE
export GISRC
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts
export GIS_LOCK=$$ # use PID as lock file number
g.version

Cleanup and Exit

#------------------------------------------------------------------------------
$GISBASE/etc/clean_temp # GRASS’ cleanup routine
rm -rf /cygdrive/c/cygwin/tmp/grass6-$USER-$GIS_LOCK # remove session tmp directory
rm -f $GISRC

Steve Hanser
Wildlife Biologist / GIS Specialist
USGS, FRESC, Snake River Field Station
970 Lusk St. Boise, ID 83706
PH: 208-426-2892 FAX: 208-426-5210

Steve Hanser wrote:

I am trying to write a batch file for Cygwin that will run multiple GRASS
r.le.pixel commands, change the region, and rerun commands on the new
region. I have a simplified version based on a previous posting to this
listserv. I have tried using it under #!/bin/sh and #!/bin/ash and
receive error with both. Please help. Thank you.

Under #!/bin/sh - Does not produce the testing.txt file

$ ./batch-grass6v3.sh
: No such file or directory4: testing.txt
: No such file or directory5: testing.txt
: No such file or directory6: testing.txt
': not a valid identifier 21: export: `GISBASE
': not a valid identifier 22: export: `GISRC
./batch-grass6v3.sh: line 25: $'g.version\r': command not found
/etc/clean_temp: No such file or directoryrass-6.2.2

Your script appears to be using CRLF line termination; you need to use
LF (the CR will be treated literally, causing all kinds of errors).

This rules out using Notepad to edit scripts. Cygwin has packages for
vim, emacs and xemacs.

Under #!/bin/ash

Produces the testing.txt file but nothing else.

$ ./batch-grass6v3.sh
export: Files/ATI: bad variable name
g.version: not found

Here is the current version of the script:

GISRC="~/testing.txt"

The ~ character isn't expanded inside quotes. Use $HOME instead, i.e.:

  GISRC="$HOME/testing.txt"

echo $GISDBASE > testing.txt

I suggest adding quotes here, i.e.

  echo "$GISDBASE" > testing.txt

export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts

I strongly suggest adding quotes here. This is probably where the
"Files/ATI" comes from (i.e. /cygdrive/c/Program Files/ATI gets split
at the space between Program and Files).

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

I need more help. I implemented the suggestions from Glynn and edited the file in emacs. I no longer receive any error messages but the script does not run any GRASS processes. Here is the current version of the script. Obviously I am new to this so I appreciate any help I can get. I have about 100 processes to complete and a batch script is the only way I think I’ll get it done. Thank you.

#!/bin/sh
#customize this:

GISRC=“$HOME/.grassrc6”
GISDBASE=“/cygdrive/c/Data/WBEA”
GISBASE=“/cygdrive/c/cygwin/usr/local/grass-6.2.2”

Generate Grass settings:

echo “GISDBASE: $GISDBASE
LOCATION_NAME: newset
MAPSET: PERMANENT” > $HOME/.grassrc6

Exports

export LD_LIBRARY_PATH=/cygdrive/c/cygwin/lib:$GISBASE/lib:$LD_LIBRARY_PATH
export GISRC=$HOME/.grassrc6
export PATH=“$PATH:$GISBASE/bin:$GISBASE/scripts”
export GIS_LOCK=$$
r.le.pixel map=bigsage sam=m att=b4 te1=m7 te2=t1,t2 edg=e1

CleanupandExit

$GISBASE/etc/clean_temp
rm -rf /cygdrive/c/cygwin/tmp/grass6-$USER-$GIS_LOCK
rm -f $GISRC

Steve Hanser
Wildlife Biologist / GIS Specialist
USGS, FRESC, Snake River Field Station
970 Lusk St. Boise, ID 83706
PH: 208-426-2892 FAX: 208-426-5210

Steve Hanser
Wildlife Biologist / GIS Specialist
USGS, FRESC, Snake River Field Station
970 Lusk St. Boise, ID 83706
PH: 208-426-2892 FAX: 208-426-5210

Glynn Clements <glynn@gclements.plus.com>

08/08/2007 02:05 PM

To

Steve Hanser <shanser@usgs.gov>

cc

grassuser@grass.itc.it

Subject

Re: [GRASS-user] Batch Processing in GRASS 6.2 w/ Cygwin

Steve Hanser wrote:

> I am trying to write a batch file for Cygwin that will run multiple GRASS
> r.le.pixel commands, change the region, and rerun commands on the new
> region. I have a simplified version based on a previous posting to this
> listserv. I have tried using it under #!/bin/sh and #!/bin/ash and
> receive error with both. Please help. Thank you.
>
> Under #!/bin/sh - Does not produce the testing.txt file
>
> $ ./batch-grass6v3.sh
> : No such file or directory4: testing.txt
> : No such file or directory5: testing.txt
> : No such file or directory6: testing.txt
> ': not a valid identifier 21: export: GISBASE > ': not a valid identifier 22: export: GISRC
> ./batch-grass6v3.sh: line 25: $'g.version\r': command not found
> /etc/clean_temp: No such file or directoryrass-6.2.2

Your script appears to be using CRLF line termination; you need to use
LF (the CR will be treated literally, causing all kinds of errors).

This rules out using Notepad to edit scripts. Cygwin has packages for
vim, emacs and xemacs.

> Under #!/bin/ash
>
> Produces the testing.txt file but nothing else.
>
> $ ./batch-grass6v3.sh
> export: Files/ATI: bad variable name
> g.version: not found
>
> Here is the current version of the script:

> GISRC="~/testing.txt"

The ~ character isn't expanded inside quotes. Use $HOME instead, i.e.:

GISRC="$HOME/testing.txt"

> echo $GISDBASE > testing.txt

I suggest adding quotes here, i.e.

echo "$GISDBASE" > testing.txt

> export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts

I strongly suggest adding quotes here. This is probably where the
"Files/ATI" comes from (i.e. /cygdrive/c/Program Files/ATI gets split
at the space between Program and Files).

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

Steve Hanser wrote:

I need more help. I implemented the suggestions from Glynn and edited the
file in emacs. I no longer receive any error messages but the script does
not run any GRASS processes. Here is the current version of the script.
Obviously I am new to this so I appreciate any help I can get. I have
about 100 processes to complete and a batch script is the only way I think
I'll get it done. Thank you.

If you just need to script GRASS commands, it's easier to just run a
script from within a GRASS session than to script the creation of the
session.

GISBASE="/cygdrive/c/cygwin/usr/local/grass-6.2.2"

This probably won't harm anything, but it's better to use Cygwin
pathnames for files and directories within the Cygwin hierarchy, i.e.:

  GISBASE="/usr/local/grass-6.2.2"

# Generate Grass settings:
#
echo "GISDBASE: $GISDBASE
LOCATION_NAME: newset
MAPSET: PERMANENT" > $HOME/.grassrc6

Multi-line strings aren't robust; it's better to use either a
here-document:

  cat > "$HOME/.grassrc6" <<-EOF
  GISDBASE: $GISDBASE
  LOCATION_NAME: newset
  MAPSET: PERMANENT
  EOF

or a subshell:

  (
  echo "GISDBASE: $GISDBASE"
  echo "LOCATION_NAME: newset"
  echo "MAPSET: PERMANENT"
  ) > "$HOME/.grassrc6"

or multiple commands appending to the file:

  echo "GISDBASE: $GISDBASE" > "$HOME/.grassrc6"
  echo "LOCATION_NAME: newset" >> "$HOME/.grassrc6"
  echo "MAPSET: PERMANENT" >> "$HOME/.grassrc6"

export LD_LIBRARY_PATH=/cygdrive/c/cygwin/lib:$GISBASE/lib:$LD_LIBRARY_PATH

LD_LIBRARY_PATH is specific to Linux and SunOS/Solaris. Windows
(including Cygwin) uses PATH to located DLLs:

  export PATH="$GISBASE/lib:$PATH"

[Cygwin's DLLs are in its "bin" directory, which will already be in $PATH.]

It's probably the fact that $GISBASE/lib is missing from $PATH that's
preventing your script from working. If a Cygwin binary cannot find
its DLLs, it simply terminates without error.

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

Glynn Clements wrote:

Your script appears to be using CRLF line termination; you need to use
LF (the CR will be treated literally, causing all kinds of errors).

This rules out using Notepad to edit scripts. Cygwin has packages for
vim, emacs and xemacs.

FWIW,

TextPad is a nice editor for Windows which lets you choose newline
format when you Save As..

nedit is a nice editor for UNIX that will run in Cygwin. (there's a pkg)
It also lets you choose newline format.

Hamish