Scripting: Difference between revisions
m (→General) |
|||
(7 intermediate revisions by 6 users not shown) | |||
Line 3: | Line 3: | ||
=== General === | === General === | ||
* See the | * See the {{cmd|g.parser}} module help page. It's an easy way to make your script look and act like a GRASS module, including creating a GUI and help page template for it automatically. See also [[Module command line parser]] | ||
* There are a number of | * There are a number of other:{{cmd|general}} g.* modules which are specifically useful for scripting use. | ||
==== Verbosity level ==== | ==== Verbosity level ==== | ||
Use the "--quiet" (alias --q) and "--verbose" (alias --v) command line arguments to make the script produce more or less messages and info like percent done. This works for almost all modules. The big exception to that is r.mapcalc which doesn't use the standard parser. | Use the "--quiet" (alias --q) and "--verbose" (alias --v) command line arguments to make the script produce more or less messages and info like percent done. This works for almost all modules. The big exception to that is {{cmd|r.mapcalc}} which doesn't use the standard parser in GRASS 6 (in GRASS 7 it does). | ||
In GRASS 6, for r.mapcalc, or if you want to set a blanket --quiet directive for the whole script, you can set the GRASS_VERBOSE environmental variable. Python's os module has a putenv() function to do that. | |||
These are the levels you can use: | These are the levels you can use: | ||
Line 27: | Line 27: | ||
# for everything that follows | # for everything that follows | ||
GRASS_VERBOSE=1 | GRASS_VERBOSE=1 | ||
export | export GRASS_VERBOSE | ||
# override for just one module | # override for just one module | ||
Line 34: | Line 34: | ||
Another nice thing about using the | Another nice thing about using the environmental variable method is that it is silently ignored for earlier versions of GRASS. For earlier versions (<6.2.x) of GRASS an error happens when "--quiet" isn't recognized as a valid command line option. | ||
silently ignored for earlier versions of GRASS. For earlier versions of GRASS | |||
an error happens when "--quiet" isn't recognized as a valid command line | |||
option. | |||
Line 47: | Line 44: | ||
whole lot easier. | whole lot easier. | ||
=== | === Shell scripts === | ||
See the [http://grass. | See the [http://grass.osgeo.org/grass64/source/snapshot/SUBMITTING_SCRIPTS SUBMITTING_SCRIPTS] file for some tips. | ||
See the existing GRASS scripts in the scripts/ directory for examples of using tempfiles, etc. | See the existing GRASS scripts in the scripts/ directory for examples of using tempfiles, etc. | ||
Line 56: | Line 53: | ||
* [[GRASS_and_Shell|Running GRASS as a batch job]] | * [[GRASS_and_Shell|Running GRASS as a batch job]] | ||
* [[Script_portability|Script portability]] | |||
=== Tcl/Tk === | === Tcl/Tk === | ||
See the [http://grass. | See the [http://grass.osgeo.org/grass64/source/snapshot/SUBMITTING_TCLTK SUBMITTING_TCLTK] file for some tips. | ||
=== Python === | === Python === | ||
* See the [[GRASS and Python]] page | * See the [[GRASS and Python]] page | ||
{{Python}} | {{Python}} |
Latest revision as of 21:04, 9 December 2013
Scripting
General
- See the g.parser module help page. It's an easy way to make your script look and act like a GRASS module, including creating a GUI and help page template for it automatically. See also Module command line parser
- There are a number of other:general g.* modules which are specifically useful for scripting use.
Verbosity level
Use the "--quiet" (alias --q) and "--verbose" (alias --v) command line arguments to make the script produce more or less messages and info like percent done. This works for almost all modules. The big exception to that is r.mapcalc which doesn't use the standard parser in GRASS 6 (in GRASS 7 it does).
In GRASS 6, for r.mapcalc, or if you want to set a blanket --quiet directive for the whole script, you can set the GRASS_VERBOSE environmental variable. Python's os module has a putenv() function to do that.
These are the levels you can use:
(from lib/gis/verbose.c) 0 - module should print nothing but errors and warnings (G_fatal_error, G_warning) Triggered by "--q" or "--quiet". 1 - module will print progress information (G_percent) 2 - module will print all messages (G_message) [default level] 3 - module will be very verbose. Triggered by "--v" or "--verbose".
"--quiet" will set the level to 0.
shell script example
# for everything that follows GRASS_VERBOSE=1 export GRASS_VERBOSE
# override for just one module GRASS_VERBOSE=0 \ r.mapcalc "only_set = for_this_process"
Another nice thing about using the environmental variable method is that it is silently ignored for earlier versions of GRASS. For earlier versions (<6.2.x) of GRASS an error happens when "--quiet" isn't recognized as a valid command line option.
see "Verbosity levels" on the Development_Specs page.
The advantage of these methods versus sending all stderr to /dev/null is that
warning and error messages are not lost, making prototyping and debugging a
whole lot easier.
Shell scripts
See the SUBMITTING_SCRIPTS file for some tips.
See the existing GRASS scripts in the scripts/ directory for examples of using tempfiles, etc.
Newer versions of Ubuntu use Dash as the default /bin/sh, so don't assume that sh will handle Bashisms.
Tcl/Tk
See the SUBMITTING_TCLTK file for some tips.
Python
- See the GRASS and Python page