grass command should read commands from stdin as an interpreter would do
|Reported by:||wenzeslaus||Owned by:|
|Keywords:||interface, CLI, script, batch job, GRASS_BATCH_JOB, init, standard input||Cc:|
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,
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)  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.)
sqlite3 distinguish between interactive invocation (
python) and input from script or stdin (
python <<EOF) and don't show anything in later case.
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.