GRASS and Shell: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(→‎See also: add link to windows console page)
m (colorized syntax highlighting)
Line 10: Line 10:
You have to set a couple of variables to enable GRASS command to run from outside GRASS:
You have to set a couple of variables to enable GRASS command to run from outside GRASS:


<source lang="bash">
   # Example in bash shell syntax:
   # Example in bash shell syntax:
   
   
Line 36: Line 37:
   export GRASS_PNG_COMPRESSION=1
   export GRASS_PNG_COMPRESSION=1
   export GRASS_MESSAGE_FORMAT=plain
   export GRASS_MESSAGE_FORMAT=plain
</source>


Define a GRASS session with the last used GISDBASE, LOCATION_NAME, and MAPSET
Define a GRASS session with the last used GISDBASE, LOCATION_NAME, and MAPSET


<source lang="bash">
   # path to GRASS settings file
   # path to GRASS settings file
   export GISRC=$HOME/.grassrc7
   export GISRC=$HOME/.grassrc7
</source>


Define a GRASS session with a different GISDBASE, LOCATION_NAME, and/or MAPSET
Define a GRASS session with a different GISDBASE, LOCATION_NAME, and/or MAPSET


<source lang="bash">
   # path to GRASS settings file
   # path to GRASS settings file
   export GISRC=/tmp/grass7-${USER}-$GIS_LOCK/gisrc
   export GISRC=/tmp/grass7-${USER}-$GIS_LOCK/gisrc
Line 56: Line 62:
   # start in text mode
   # start in text mode
   echo "GRASS_GUI: text" >>$GISRC
   echo "GRASS_GUI: text" >>$GISRC
</source>


The following three settings are only recommended if you will be calling the script from another program - e.g. a PHP web page using system() or exec()
The following three settings are only recommended if you will be calling the script from another program - e.g. a PHP web page using system() or exec()


<source lang="bash">
   export HOME=/var/www
   export HOME=/var/www
   export USER=www-data
   export USER=www-data
   export GROUP=www-data
   export GROUP=www-data
</source>


Now you can test:
Now you can test:


<source lang="bash">
   # this should print the GRASS version used:
   # this should print the GRASS version used:
   g.version
   g.version
Line 70: Line 80:
   g.gisenv
   g.gisenv
   # other calculations go here ...
   # other calculations go here ...
</source>


If this works, you can launch other GRASS commands. The approach works within Shell scripts and also in the command line terminal.
If this works, you can launch other GRASS commands. The approach works within Shell scripts and also in the command line terminal.
Line 75: Line 86:
When done, you should cleanup internal tmp files like this:
When done, you should cleanup internal tmp files like this:


<source lang="bash">
   # run GRASS' cleanup routine
   # run GRASS' cleanup routine
   $GISBASE/etc/clean_temp
   $GISBASE/etc/clean_temp
Line 80: Line 92:
   # remove session tmp directory:
   # remove session tmp directory:
   rm -rf /tmp/grass7-${USER}-$GIS_LOCK
   rm -rf /tmp/grass7-${USER}-$GIS_LOCK
 
</source>


'''Changing the prompt:'''
'''Changing the prompt:'''


<source lang="bash">
   PS1_BACKUP="$PS1"
   PS1_BACKUP="$PS1"
   export PS1="GRASS 6> "
   export PS1="GRASS 6> "
</source>


Once you are done, you can set it back (see also next hint about unsetting variables):
Once you are done, you can set it back (see also next hint about unsetting variables):
<source lang="bash">
   export PS1="$PS1_BACKUP"
   export PS1="$PS1_BACKUP"
</source>


'''Unsetting variables after usage:'''
'''Unsetting variables after usage:'''


<source lang="bash">
   Using the "unset VARNAME" command you get rid of it:
   Using the "unset VARNAME" command you get rid of it:
   unset GISBASE
   unset GISBASE
   unset GISRC
   unset GISRC
   ...
   ...
 
</source>


=== Example ===
=== Example ===

Revision as of 09:03, 2 February 2021

This page contains information about GRASS GIS' own shell setup and use.

For information about scripting for GRASS, please refer to the page Shell scripting.

Automated batch jobs: Setting the GRASS environmental variables

Main article: Working with GRASS without starting it explicitly

This section applies to jobs which shall set the entire GRASS environment. You have to set a couple of variables to enable GRASS command to run from outside GRASS:

   # Example in bash shell syntax:
 
   # path to GRASS binaries and libraries:
   export GISBASE=/usr/lib64/grass74
 
   export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts
   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GISBASE/lib
   
   # set PYTHONPATH to include the GRASS Python lib
   if [ ! "$PYTHONPATH" ] ; then
      PYTHONPATH="$GISBASE/etc/python"
   else
      PYTHONPATH="$GISBASE/etc/python:$PYTHONPATH"
   fi
   export PYTHONPATH
 
   # use process ID (PID) as lock file number:
   export GIS_LOCK=$$
 
   # settings for graphical output to PNG file (optional)
   export GRASS_PNGFILE=/tmp/grass6output.png
   export GRASS_TRUECOLOR=TRUE
   export GRASS_WIDTH=900
   export GRASS_HEIGHT=1200
   export GRASS_PNG_COMPRESSION=1
   export GRASS_MESSAGE_FORMAT=plain

Define a GRASS session with the last used GISDBASE, LOCATION_NAME, and MAPSET


   # path to GRASS settings file
   export GISRC=$HOME/.grassrc7

Define a GRASS session with a different GISDBASE, LOCATION_NAME, and/or MAPSET

   # path to GRASS settings file
   export GISRC=/tmp/grass7-${USER}-$GIS_LOCK/gisrc
   # remove any leftover files/folder
   rm -fr /tmp/grass7-${USER}-$GIS_LOCK
   mkdir /tmp/grass7-${USER}-$GIS_LOCK
   export TMPDIR="/tmp/grass7-${USER}-$GIS_LOCK"
   # set GISDBASE, LOCATION_NAME, and/or MAPSET
   echo "GISDBASE: /path/to/some/grassdata" >>$GISRC
   echo "LOCATION_NAME: some_location" >>$GISRC
   echo "MAPSET: some_mapset" >>$GISRC
   # start in text mode
   echo "GRASS_GUI: text" >>$GISRC

The following three settings are only recommended if you will be calling the script from another program - e.g. a PHP web page using system() or exec()

   export HOME=/var/www
   export USER=www-data
   export GROUP=www-data

Now you can test:

   # this should print the GRASS version used:
   g.version
   # check GISDBASE, LOCATION_NAME, and MAPSET
   g.gisenv
   # other calculations go here ...

If this works, you can launch other GRASS commands. The approach works within Shell scripts and also in the command line terminal.

When done, you should cleanup internal tmp files like this:

   # run GRASS' cleanup routine
   $GISBASE/etc/clean_temp
 
   # remove session tmp directory:
   rm -rf /tmp/grass7-${USER}-$GIS_LOCK

Changing the prompt:

   PS1_BACKUP="$PS1"
   export PS1="GRASS 6> "

Once you are done, you can set it back (see also next hint about unsetting variables):

   export PS1="$PS1_BACKUP"

Unsetting variables after usage:

   Using the "unset VARNAME" command you get rid of it:
   unset GISBASE
   unset GISRC
   ...

Example

Parallel GRASS jobs

See Parallel GRASS jobs for Grid Engine, PBS etc.

GRASS Batch jobs

Since GRASS GIS 6.4, there is an alternative method to easily run jobs in GRASS from a collection of commands in a shell script file. Just define the environmental variable GRASS_BATCH_JOB with the shell script file containing GRASS (or whatever) commands, preferably with full path. Then launch GRASS and it will be executed. It is best to launch GRASS in -text mode and to provide gisdbase/location/mapset as parameters. The job script needs executable file permissions (chmod on Unix). In order to get readable percentage output during the processing (0..2..4... 100%), we set the environment variable GRASS_MESSAGE_FORMAT to "plain":

Example:

#### 1) PREPARATION
# First we generate a script which contains the command(s) to be executed:
# for convenience, we save the file in our HOME directory
## You may use also a text editor for this, here we use the "echo" shell command

echo "export GRASS_MESSAGE_FORMAT=plain
# set computational region, here: UTM32N coordinates
g.region n=4900000 s=4800000 w=700000 e=800000 res=100
v.random mymap3000 n=3000
v.out.ogr input=mymap3000 output=mymap3000.shp" > $HOME/my_grassjob.sh

# verify the content of the file
cat $HOME/my_grassjob.sh

# make it user executable (this is important, use 'chmod' or via file manager)
chmod u+x $HOME/my_grassjob.sh

# create a directory (may be elsewhere) to hold the location used for processing
mkdir -p $HOME/grassdata

# create new temporary location for the job, exit after creation of this location
grass70 -c epsg:32632 $HOME/grassdata/mytemploc_utm32n -e

#### 2) USING THE BATCH JOB
# define job file as environmental variable
export GRASS_BATCH_JOB="$HOME/my_grassjob.sh"

# now we can use this new location and run the job defined via GRASS_BATCH_JOB
grass70 $HOME/grassdata/mytemploc_utm32n/PERMANENT

#### 3) CLEANUP
# switch back to interactive mode, for the next GRASS GIS session
unset GRASS_BATCH_JOB

# delete temporary location (consider to export results first in your batch job)
rm -rf $HOME/grassdata/mytemploc_utm32n

# Now you can use the resulting SHAPE file "mymap3000.shp" elsewhere.

The grass70 command starts GRASS in the given mapset, executes the contents of the job file and leaves GRASS. Since the normal startup/closure is used, all internal tmp files are properly removed.

Note: The $HOME variable (or the ~ shortcut) cannot be used in the batch job itself since the variables are not available here.

Example

Unattended execution

  • GNU_Screen is another extremely valuable tool if you need to detatch and leave long-running processes unattended. It is well worth your time to learn how to use it if you run scripts on remote systems. There are many good tutorials on the web.
Usage:
Run "screen" in the terminal. You will reach again the command line but now in screen mode. Now start GRASS.
- To disconnect from the session press Control-A, Control-D.
- To list your screens type "screen -ls" (to find it back)
- To reconnect with a disconnected screen run "screen -r [identifier]" (the "identifier" you need only if you have several screens running)
Enjoy.
  • Along with the "nohup" (no hang-up) command you can login to your machine, launch the job and leave the machine again.

The process will continue after you logged off when you start it with nohup:

       nohup grass64 ~/grassdata/spearfish60/neteler/ &

Receive a notification when finished

Maybe put email notification at the end of 'my_grassjob.sh' using the "mail" or the "mutt" program, for example like this:

       echo "Finished at `date`" > /tmp/done.txt && \
       EDITOR=touch mutt -s "Job done" \
       me@mydomain.org < /tmp/done.txt && rm -f /tmp/done.txt

or like this:

       mail -s "GRASS job $0 finished" me@mydomain.org <<EOF
         GRASS GIS has finished the batch job $0
       EOF

or simply like this:

       echo "GRASS GIS has finished the batch job $0" | mail -s "GRASS job finished" me@mydomain.org

Stupid pet tricks (don't use them!)

Note: This is highly discouraged. Keep fingers off from manually modifying the content of a GRASS GIS mapset (at least don't complain if you break things).

;Quick cd to the MAPSET directory To make a quick little function called 'g.cd' to change into the mapset dir, add this to ~/.grass.bashrc:

g.cd()
{
  MAPSET=`g.gisenv get=MAPSET`
  LOCATION_NAME=`g.gisenv get=LOCATION_NAME`
  GISDBASE=`g.gisenv get=GISDBASE`
  LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
  cd "$LOCATION/$1"
}

With that you can also do like: "g.cd colr/" to get to the color tables directory, or "g.cd .." to get to the LOCATION directory.

Another method would be

  alias g.home='cd `dirname "$HISTFILE"`'

Simpler command completion from command history

Add this to a file called ~/.inputrc in your home dir:

set prefer-visible-bell

# -------- Bind page up/down wih history search ---------
"\e[5~": history-search-backward
"\e[6~": history-search-forward

Then you can type a bit of a command and use PgUp and PgDn to cycle through the command history which matches in a way less clumsy that Ctrl-r. Also it tells to make the shell flash on alarm instead of sending a beep to the speaker (making tab-completion compatible with your office mates).

See also

Generic Shell script tutorials:

GRASS Shell script tutorials:

Misc: