Opened 5 years ago

Last modified 2 months ago

#2639 new enhancement

grass command should read commands from stdin as an interpreter would do

Reported by: wenzeslaus Owned by: grass-dev@…
Priority: normal Milestone: 8.0.0
Component: Startup Version: unspecified
Keywords: interface, CLI, script, batch job, GRASS_BATCH_JOB, init, standard input Cc:
CPU: Unspecified Platform: All

Description

GRASS GIS works as something between set of command line tools (e.g. GDAL, or POSIX utilities) and an interpreter with unique commands or syntax (Python, R). However, GRASS GIS does not behave as the former because the tools are not available in standard command line (system environment). Unfortunately, grass (grass7) command does not behave as an interpreter neither because it does not allow scripts to be executed in the same way as with the standard interpreters.

Standard interpreters allow to provide commands/script as standard input:

$ python <<EOF
> a = 5
> print(a)
> EOF
5
$ R --vanilla --silent <<EOF
a = 5
print(a)
EOF
> a = 5
> print(a)
[1] 5
> 
$ bash <<EOF
A=5
echo \$A
EOF
$ sqlite3 <<EOF
SELECT 1;
EOF

1

grass actually allows to do the same and the commands are executed.

$ grass -text -c /grassdata/nc_spm/practice3/ <<EOF             
g.list rast -p
EOF

However, the behavior is significantly different from standard interpreters: screen is reset and a lot of other text is printed. To see what is happening, we can redirect stdout to a file.

$ grass -text -c /grassdata/nc_spm/practice3/ > output.txt <<EOF
g.list rast -p
EOF

The following standard error output is shown in the command line:

Cleaning up temporary files...
Creating new GRASS GIS location/mapset...

          __________  ___   __________    _______________
         / ____/ __ \/   | / ___/ ___/   / ____/  _/ ___/
        / / __/ /_/ / /| | \__ \\_  \   / / __ / / \__ \ 
       / /_/ / _, _/ ___ |___/ /__/ /  / /_/ // / ___/ / 
       \____/_/ |_/_/  |_/____/____/   \____/___//____/  

Welcome to GRASS GIS 7.1.svn (r64884)
GRASS GIS homepage:                      http://grass.osgeo.org
This version running through:            Bash Shell (/bin/bash)
Help is available with the command:      g.manual -i
See the licence terms with:              g.version -c
Start the GUI with:                      g.gui wxpython
When ready to quit enter:                exit

Cleaning up temporary files...
Done.

Goodbye from GRASS GIS

Output file as displayed by less output.txt contains characters to reset/clean the terminal:

ESC[HESC[2J----------------------------------------------

raster files available in mapset <PERMANENT>:
aspect                    elevation_shade           lsat7_2002_70
basin_50K                 facility                  lsat7_2002_80
...
elev_state_500m           lsat7_2002_61             zipcodes_dbl
elevation                 lsat7_2002_62

ESC[HESC[2J

The question is also if there should be the screen cleaning/reset at all; neither Python nor R are using it:

$
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$
$
$ R

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> q()
Save workspace image? [y/n/c]: n
$

Regarding the welcome text (version, copyright, help, etc.) python and sqlite3 distinguish between interactive invocation (python) and input from script or stdin (python script.py, python <<EOF) and don't show anything in later case.

With R we need to use --silent to suppress the welcome text but it actually checks the type of interaction too and requires additional parameters (e.g. --vanilla) when running in non-interactive mode. It must be noted that R has Rscript which is dedicated to running R scripts but it does not support input from stdin.

What should be enhanced

When standard (non-interactive) input is provided or a specific command line option set, grass command should not perform cleaning of the terminal and perhaps should be less verbose.

This is of course partially overlapping with #2579 but it is an important use case which we should take care of explicitly. Perhaps this behavior can be activated when the grass batch / grass exec / grass run / grass script / grass --batch syntax is used.

Change History (10)

comment:1 Changed 4 years ago by neteler

Milestone: 7.1.07.2.0

Milestone renamed

comment:2 Changed 4 years ago by neteler

Milestone: 7.2.07.2.1

Ticket retargeted after milestone closed

comment:3 Changed 3 years ago by martinl

Milestone: 7.2.17.2.2

comment:4 Changed 3 years ago by martinl

Milestone: 7.2.27.4.0

All enhancement tickets should be assigned to 7.4 milestone.

comment:5 Changed 2 years ago by neteler

Milestone: 7.4.07.4.1

Ticket retargeted after milestone closed

comment:6 Changed 2 years ago by neteler

Milestone: 7.4.17.4.2

comment:7 Changed 22 months ago by wenzeslaus

Milestone: 7.4.28.0.0

comment:8 Changed 7 months ago by wenzeslaus

The situation is much better then when the ticket was created. It is actually usable even with > file.txt. It still could be less verbose:

$ grass79 -text --tmp-location XY <<EOF             
g.region -g
EOF
Starting GRASS GIS...
Creating new GRASS GIS location <tmploc>...
Cleaning up temporary files...

          __________  ___   __________    _______________
         / ____/ __ \/   | / ___/ ___/   / ____/  _/ ___/
        / / __/ /_/ / /| | \__ \\_  \   / / __ / / \__ \
       / /_/ / _, _/ ___ |___/ /__/ /  / /_/ // / ___/ /
       \____/_/ |_/_/  |_/____/____/   \____/___//____/

Welcome to GRASS GIS 7.9.dev (8635409)
GRASS GIS homepage:                      https://grass.osgeo.org
This version running through:            Bash Shell (/bin/bash)
Help is available with the command:      g.manual -i
See the licence terms with:              g.version -c
See citation options with:               g.version -x
Start the GUI with:                      g.gui wxpython
When ready to quit enter:                exit

projection=0
zone=0
n=1
s=0
w=0
e=1
nsres=1
ewres=1
rows=1
cols=1
cells=1
Cleaning up temporary files...
Done.

Goodbye from GRASS GIS

comment:9 in reply to:  8 Changed 7 months ago by neteler

Replying to wenzeslaus:

It still could be less verbose:

A new flag --quiet comes to mind...

comment:10 Changed 2 months ago by wenzeslaus

Using --exec bash, this starts to be almost all right:

Input:

$ grass79 --tmp-location XY --exec bash <<EOF
g.region -g
g.proj -g
EOF

Output:

Starting GRASS GIS...
Creating new GRASS GIS location <tmploc>...
Cleaning up temporary files...
Executing <bash> ...
projection=0
zone=0
n=1
s=0
w=0
e=1
nsres=1
ewres=1
rows=1
cols=1
cells=1
name=xy_location_unprojected
Execution of <bash> finished.
Cleaning up temporary files...

With redirecting stdout to a file:

$ grass79 --tmp-location XY --exec bash > test.txt <<EOF
g.region -g
g.proj -g
EOF

Output:

Starting GRASS GIS...
Cleaning up temporary files...
Executing <bash> ...
Execution of <bash> finished.
Cleaning up temporary files...

File content:

projection=0
zone=0
n=1
s=0
w=0
e=1
nsres=1
ewres=1
rows=1
cols=1
cells=1
name=xy_location_unprojected
Note: See TracTickets for help on using tickets.