[GRASS-user] grass shell

Hi:
I want to know the shell used by grass when run it in command line ?
Because I met a problem when I tried to call grass in my java web
application,the output of the same cmd are different.
Details:
I use the grass-batch-job manner to run the grass in terminal, the
batjob file is attached as the grass.sh.
-------------------------------------------
r.in.gdal input=/home/kk/grass/data/4404.tif output=aa location=newl >
/home/kk/ttsc.txt
g.gisenv set=LOCATION_NAME=newl
r.buffer input=aa distances=10 output=ab
r.out.gdal input=ab format=GTiff output=/home/kk/grass/data/4404-a.tif
-------------------------------------------

The cmd I run and the output are shown in the attach "output-terminal.txt".
---------------------------------------
kk@ubuntu:~$ export
GRASS_BATCH_JOB=/home/kk/MavenTest/MParent/mserver/src/main/resources/org/kingxip/grass.sh

kk@ubuntu:~$ sudo chmod 777
/home/kk/MavenTest/MParent/mserver/src/main/resources/org/kingxip/grass.sh
[sudo] password for kk:

kk@ubuntu:~$ grass64 -text /home/kk/grass/GrassDataBase/spearfish60/PERMANENT
Cleaning up temporary files ...
Starting GRASS ...
Executing '/home/kk/MavenTest/MParent/mserver/src/main/resources/org/kingxip/grass.sh'
...
Welcome to GRASS 6.4.0RC5 (2009)
Location <newl> created
100%
r.in.gdal complete. Raster map <aa> created.
Reading input raster map <aa@PERMANENT>...
100%
Finding buffer zones...
100%
Writing output raster map <ab>...
100%
Exporting to GDAL data type: Byte
100%
Input raster map contains cells with NULL-value (no-data). The value 255
was used to represent no-data values in the input map. You can specify
nodata value by nodata parameter.
r.out.gdal complete.
Closing monitors ...
Cleaning up temporary files ...
Batch job '/home/kk/MavenTest/MParent/mserver/src/main/resources/org/kingxip/grass.sh'
(defined in GRASS_BATCH_JOB variable) was executed.
Goodbye from GRASS GIS
---------------------------------------

Then I tried to run it in the java, the java code is shown in the
attach RuntimeT.java:
-----------------------------------------
public class RuntimeT {
  public static void main(String args) {
    String cmd="grass64 -text
/home/kk/grass/GrassDataBase/spearfish60/PERMANENT";
    String env={"GRASS_BATCH_JOB=/home/kk/MavenTest/MParent/mserver/src/main/resources/org/kingxip/grass.sh"};
    try {
      Process p1=Runtime.getRuntime().exec(cmd, env);
      InputStream is=p1.getInputStream();
      BufferedReader br=new BufferedReader(new InputStreamReader(is));
      String line=null;
      for(line=br.readLine();line!=null;line=br.readLine()) {
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
-------------------------------
However when I run this code, the process hang up ,and the output is
not the same as the terminal, shown in the attach GrassInJava.png.

So, what is the problem?

(attachments)

output-terminal.txt (1.12 KB)
grass.sh (233 Bytes)
RuntimeT.java (723 Bytes)
GrassInJava.png

maven apache wrote:

I want to know the shell used by grass when run it in command line ?

It uses the shell specified by $SHELL.

Because I met a problem when I tried to call grass in my java web
application,the output of the same cmd are different.
Details:
I use the grass-batch-job manner to run the grass in terminal, the
batjob file is attached as the grass.sh.

If you set GRASS_BATCH_JOB, it is executed instead of the shell.

Then I tried to run it in the java, the java code is shown in the
attach RuntimeT.java:

    String env={"GRASS_BATCH_JOB=/home/kk/MavenTest/MParent/mserver/src/main/resources/org/kingxip/grass.sh"};
    try {
      Process p1=Runtime.getRuntime().exec(cmd, env);

However when I run this code, the process hang up ,and the output is
not the same as the terminal, shown in the attach GrassInJava.png.

So, what is the problem?

With java.lang.Runtime.exec(String cmd, String envp), the envp
parameter specifies the entire environment for the process (i.e. it
doesn't add the strings to the current process' environment). The
result is that HOME isn't set, which means that $HOME/.grassrc6 cannot
be found, so it displays the "first use" screen and waits for the user
to press a key:

  # First time user - GISRC is defined in the GRASS script
  if [ ! -f "$GISRC" ] ; then
  
      if [ ! -f "$GISBASE/locale/$LCL/etc/grass_intro" ] ; then
    cat "$ETC/grass_intro"
      else
    cat "$GISBASE/locale/$LCL/etc/grass_intro"
      fi
  
      echo
      echo "Hit RETURN to continue"
      read ans

You should probably retrieve the current environment (which, AFAICT,
requires 5.0 or later; 1.4.2 only appears to allow querying specific
environment variables), and add the GRASS_BATCH_JOB setting to it.

Beyond that, I'd suggest that you abandon the GRASS_BATCH_JOB approach
and set up the environment yourself, without using the grass64 script
at all.

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

Thanks for your quick reply and sorry for my late coming since I has been testing the grass command …

2010/3/4 Glynn Clements <glynn@gclements.plus.com>

maven apache wrote:

I want to know the shell used by grass when run it in command line ?

It uses the shell specified by $SHELL.

Because I met a problem when I tried to call grass in my java web
application,the output of the same cmd are different.
Details:
I use the grass-batch-job manner to run the grass in terminal, the
batjob file is attached as the grass.sh.

If you set GRASS_BATCH_JOB, it is executed instead of the shell.

Then I tried to run it in the java, the java code is shown in the
attach RuntimeT.java:

String env={“GRASS_BATCH_JOB=/home/kk/MavenTest/MParent/mserver/src/main/resources/org/kingxip/grass.sh”};
try {
Process p1=Runtime.getRuntime().exec(cmd, env);

However when I run this code, the process hang up ,and the output is
not the same as the terminal, shown in the attach GrassInJava.png.

So, what is the problem?

With java.lang.Runtime.exec(String cmd, String envp), the envp
parameter specifies the entire environment for the process (i.e. it
doesn’t add the strings to the current process’ environment). The
result is that HOME isn’t set, which means that $HOME/.grassrc6 cannot

I can not understand, my grass command is “grass64 -text /home/kk/grass/GrassDataBase/spearfish60/PERMANENT”, so I have specified the location and mapset, the gisrc file is required at any times?

be found, so it displays the “first use” screen and waits for the user
to press a key:

First time user - GISRC is defined in the GRASS script

if [ ! -f “$GISRC” ] ; then

if [ ! -f “$GISBASE/locale/$LCL/etc/grass_intro” ] ; then
cat “$ETC/grass_intro”
else
cat “$GISBASE/locale/$LCL/etc/grass_intro”
fi

echo
echo “Hit RETURN to continue”
read ans

You should probably retrieve the current environment (which, AFAICT,
requires 5.0 or later; 1.4.2 only appears to allow querying specific
environment variables), and add the GRASS_BATCH_JOB setting to it.

Beyond that, I’d suggest that you abandon the GRASS_BATCH_JOB approach
and set up the environment yourself, without using the grass64 script
at all.

God!, in fact I tied to set up the env vars by myself, however many people suggest me to use the grass_batch _job for easily using. Now I can not make a choice.
Here I can show you my requirements, can you give me some advise?
—my requirements----

I am working with the Web Processing Service, and I want to wrap some grass operation ( for example the buffer , the interpolation and ect…) as processes in the web service, And my web application is developed by java. I changed my work platform from win xp to ubuntu beacuase of the unexpected errors when I call the grass command(Actually I can not do the interpolation operation in the windows-xp command line, but it can work in the Ubuntu shell).
I have a general idea:
Create a class named GrassManager, and it contain a method to set the env (a gisrc file for each user) and a method to call the concrete grass command (for example r.info …) basing on the set env.

However I need to solve the following problems:

  1. how to set up the grass env in java?

  2. after set up the env ,how to call the grass command in java, because it reffed to command interpreter and separate executable, I do not know the grass command is either?

  3. As a web application, I should consider the multiple user situation, create a unique gisrc file for each user?


Glynn Clements <glynn@gclements.plus.com>

maven apache wrote:

> > So, what is the problem?
>
> With java.lang.Runtime.exec(String cmd, String envp), the envp
> parameter specifies the entire environment for the process (i.e. it
> doesn't add the strings to the current process' environment). The
> result is that HOME isn't set, which means that $HOME/.grassrc6 cannot

I can not understand, my grass command is "grass64 -text
/home/kk/grass/GrassDataBase/spearfish60/PERMANENT", so I have specified the
location and mapset, the gisrc file is required at any times?

The grass64 script creates a copy of $HOME/.grassrc6 and sets GISRC to
refer to the copy. If $HOME/.grassrc6 doesn't exist, the copy won't
exist either, and the first-use screen will be shown.

> Beyond that, I'd suggest that you abandon the GRASS_BATCH_JOB approach
> and set up the environment yourself, without using the grass64 script
> at all.

God!, in fact I tied to set up the env vars by myself, however many people
suggest me to use the grass_batch _job for easily using. Now I can not make
a choice.
Here I can show you my requirements, can you give me some advise?
---my requirements----

I am working with the Web Processing Service, and I want to wrap some grass
operation ( for example the buffer , the interpolation and ect...) as
processes in the web service, And my web application is developed by java. I
changed my work platform from win xp to ubuntu beacuase of the unexpected
errors when I call the grass command(Actually I can not do the interpolation
operation in the windows-xp command line, but it can work in the Ubuntu
shell).
I have a general idea:
Create a class named GrassManager, and it contain a method to set the env (a
gisrc file for each user) and a method to call the concrete grass command
(for example r.info .....) basing on the set env.

However I need to solve the following problems:
1) how to set up the grass env in java?

As for what variables need to be set, refer to:

1. My answer to your previous post in the thread "grass env".

2. The thread "can I access mapset outside of grass, by using python?"
by Nikos Alexandris.

3. The variables.html file in the GRASS distribution.

You first need to make a copy of the existing environment, then add or
replace any environment settings required by GRASS.

2) after set up the env ,how to call the grass command in java, because it
reffed to command interpreter and separate executable, I do not know the
grass command is either?

Once you have created the environment, I suggest using:

  Java.lang.Runtime.exec(String cmdarray, String envp)
or:
  Java.lang.Runtime.exec(String cmdarray, String envp, File dir)

for each GRASS command. The versions which take the command as a
single string are problematic if an argument contains spaces.

3) As a web application, I should consider the multiple user situation,
create a unique gisrc file for each user?

Ideally, you should use a separate $GISRC file and a separate mapset
for each session. If you use a single mapset, the WIND and VAR files
will be shared by all sessions. The VAR file probably won't be an
issue (it only holds the database connection information). You can get
around the WIND issue by setting WIND_OVERRIDE to the name of a
specific region (created with e.g. "g.region save=..."), which will be
used instead of the WIND file.

But the main thing for a web application is to validate all inputs.
Don't pass values from form fields directly to commands, as the code
uses fixed-size buffers extensively and doesn't perform bounds
checking. Also, shell scripts (and some compiled programs which use
system() or popen()) may misbehave if arguments contain shell
metacharacters.

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

Hi, thanks for your detailed reply.
And I have write a java class according to your instruction,however it can not give the right output. I test with the spearfish60 data and the r.info process.
Attach my code, can you check it ?
Thanks anyway.

2010/3/5 Glynn Clements <glynn@gclements.plus.com>

maven apache wrote:

So, what is the problem?

With java.lang.Runtime.exec(String cmd, String envp), the envp
parameter specifies the entire environment for the process (i.e. it
doesn’t add the strings to the current process’ environment). The
result is that HOME isn’t set, which means that $HOME/.grassrc6 cannot

I can not understand, my grass command is “grass64 -text
/home/kk/grass/GrassDataBase/spearfish60/PERMANENT”, so I have specified the
location and mapset, the gisrc file is required at any times?

The grass64 script creates a copy of $HOME/.grassrc6 and sets GISRC to
refer to the copy. If $HOME/.grassrc6 doesn’t exist, the copy won’t
exist either, and the first-use screen will be shown.

Beyond that, I’d suggest that you abandon the GRASS_BATCH_JOB approach
and set up the environment yourself, without using the grass64 script
at all.

God!, in fact I tied to set up the env vars by myself, however many people
suggest me to use the grass_batch _job for easily using. Now I can not make
a choice.
Here I can show you my requirements, can you give me some advise?
—my requirements----

I am working with the Web Processing Service, and I want to wrap some grass
operation ( for example the buffer , the interpolation and ect…) as
processes in the web service, And my web application is developed by java. I
changed my work platform from win xp to ubuntu beacuase of the unexpected
errors when I call the grass command(Actually I can not do the interpolation
operation in the windows-xp command line, but it can work in the Ubuntu
shell).
I have a general idea:
Create a class named GrassManager, and it contain a method to set the env (a
gisrc file for each user) and a method to call the concrete grass command
(for example r.info …) basing on the set env.

However I need to solve the following problems:

  1. how to set up the grass env in java?

As for what variables need to be set, refer to:

  1. My answer to your previous post in the thread “grass env”.

  2. The thread “can I access mapset outside of grass, by using python?”
    by Nikos Alexandris.

  3. The variables.html file in the GRASS distribution.

You first need to make a copy of the existing environment, then add or
replace any environment settings required by GRASS.

  1. after set up the env ,how to call the grass command in java, because it
    reffed to command interpreter and separate executable, I do not know the
    grass command is either?

Once you have created the environment, I suggest using:

Java.lang.Runtime.exec(String cmdarray, String envp)
or:
Java.lang.Runtime.exec(String cmdarray, String envp, File dir)

for each GRASS command. The versions which take the command as a
single string are problematic if an argument contains spaces.

  1. As a web application, I should consider the multiple user situation,
    create a unique gisrc file for each user?

Ideally, you should use a separate $GISRC file and a separate mapset
for each session. If you use a single mapset, the WIND and VAR files
will be shared by all sessions. The VAR file probably won’t be an
issue (it only holds the database connection information). You can get
around the WIND issue by setting WIND_OVERRIDE to the name of a
specific region (created with e.g. “g.region save=…”), which will be
used instead of the WIND file.

But the main thing for a web application is to validate all inputs.
Don’t pass values from form fields directly to commands, as the code
uses fixed-size buffers extensively and doesn’t perform bounds
checking. Also, shell scripts (and some compiled programs which use
system() or popen()) may misbehave if arguments contain shell
metacharacters.

Glynn Clements <glynn@gclements.plus.com>

(attachments)

GrassMa.java (2.05 KB)

maven apache wrote:

Hi, thanks for your detailed reply.
And I have write a java class according to your instruction,however it can
not give the right output.

What output does it give?

I test with the spearfish60 data and the
r.infoprocess.
Attach my code, can you check it ?

  public String run(String cmd) {

    try {
      cmd="/bin/sh -c " + cmd;
      String exe={"/bin/sh -c",cmd};

      Process p1=rt.exec(exe,env);

I suggest that you don't use the shell; it just makes life harder.
Instead, define:

  public String run(String cmd) {
  ...
      Process p1=rt.exec(cmd, env);

and use:

  gm.run({"r.info", "map=roads"});

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

2010/3/5 Glynn Clements <glynn@gclements.plus.com>

maven apache wrote:

Hi, thanks for your detailed reply.
And I have write a java class according to your instruction,however it can
not give the right output.

What output does it give?

I test with the spearfish60 data and the
r.infoprocess.
Attach my code, can you check it ?

public String run(String cmd) {

try {
cmd=“/bin/sh -c " + cmd;
String exe={”/bin/sh -c",cmd};

Process p1=rt.exec(exe,env);

I suggest that you don’t use the shell; it just makes life harder.
Instead, define:

public String run(String cmd) {

Process p1=rt.exec(cmd, env);

and use:

gm.run({“r.info”, “map=roads”});

Hi, I use the following:

public String run(String cmd) {
String exe = { “bash”, “-c”, cmd };
Process p1=rt.exec(exe, env);

Now it works, thank you anyway! I can not make it work without your patient explanation these days! Your are a good guy!

Also, I want to say “Thank you” to everyone who have help me complete my job.! :slight_smile:

Glynn Clements <glynn@gclements.plus.com>

On Fri, Mar 5, 2010 at 7:18 AM, maven apache <apachemaven0@gmail.com> wrote:
...

Now it works, thank you anyway! I can not make it work without your patient
explanation these days! Your are a good guy!

Also, I want to say "Thank you" to everyone who have help me complete my
job.! :slight_smile:

Excellent - please be so kind to summarize "lessions learned" in
the Wiki. I have added a stub page for this:

http://grass.osgeo.org/wiki/GRASS_and_Java

Then others can benefit and we'll get better documentation.

Markus

2010/3/6 Markus Neteler <neteler@osgeo.org>

On Fri, Mar 5, 2010 at 7:18 AM, maven apache <apachemaven0@gmail.com> wrote:

Now it works, thank you anyway! I can not make it work without your patient
explanation these days! Your are a good guy!

Also, I want to say “Thank you” to everyone who have help me complete my
job.! :slight_smile:

Excellent - please be so kind to summarize “lessions learned” in
the Wiki. I have added a stub page for this:

http://grass.osgeo.org/wiki/GRASS_and_Java

Then others can benefit and we’ll get better documentation.

ok, I may add something to the wiki.

Markus

2010/3/24 Markus Neteler <neteler@osgeo.org>

hi

On Sat, Mar 6, 2010 at 2:43 AM, maven apache <apachemaven0@gmail.com> wrote:

2010/3/6 Markus Neteler <neteler@osgeo.org>

On Fri, Mar 5, 2010 at 7:18 AM, maven apache <apachemaven0@gmail.com>
wrote:

Now it works, thank you anyway! I can not make it work without your
patient explanation these days! Your are a good guy!

Also, I want to say “Thank you” to everyone who have help me complete my
job.! :slight_smile:

Excellent - please be so kind to summarize “lessions learned” in
the Wiki. I have added a stub page for this:

http://grass.osgeo.org/wiki/GRASS_and_Java

Then others can benefit and we’ll get better documentation.

ok, I may add something to the wiki.

given all the help you received, please be so kind and add a bit more
to above wiki page.

HI, I have added something yet. It is about the grass installation in Ubuntu and the way to call the grass in the terminal and java app,however what I posted is not formatted yet., I do not know how to highlight the codes .

m

On Wed, Mar 24, 2010 at 3:21 AM, maven apache <apachemaven0@gmail.com> wrote:

2010/3/24 Markus Neteler <neteler@osgeo.org>

hi

On Sat, Mar 6, 2010 at 2:43 AM, maven apache <apachemaven0@gmail.com>
wrote:
> 2010/3/6 Markus Neteler <neteler@osgeo.org>
>> On Fri, Mar 5, 2010 at 7:18 AM, maven apache <apachemaven0@gmail.com>
>> wrote:
>> ...
>> > Now it works, thank you anyway! I can not make it work without your
>> > patient explanation these days! Your are a good guy!
>> >
>> > Also, I want to say "Thank you" to everyone who have help me complete
>> > my
>> > job.! :slight_smile:
>>
>> Excellent - please be so kind to summarize "lessions learned" in
>> the Wiki. I have added a stub page for this:
>>
>> http://grass.osgeo.org/wiki/GRASS_and_Java
>>
>> Then others can benefit and we'll get better documentation.
>
> ok, I may add something to the wiki.

given all the help you received, please be so kind and add a bit more
to above wiki page.

HI, I have added something yet. It is about the grass installation in Ubuntu
and the way to call the grass in the terminal and java app,however what I
posted is not formatted yet., I do not know how to highlight the codes .

done. But not sure if the page is already sufficient to be understood...

Markus

2010/3/24 Markus Neteler <neteler@osgeo.org>

On Wed, Mar 24, 2010 at 3:21 AM, maven apache <apachemaven0@gmail.com> wrote:

2010/3/24 Markus Neteler <neteler@osgeo.org>

hi

On Sat, Mar 6, 2010 at 2:43 AM, maven apache <apachemaven0@gmail.com>
wrote:

2010/3/6 Markus Neteler <neteler@osgeo.org>

On Fri, Mar 5, 2010 at 7:18 AM, maven apache <apachemaven0@gmail.com>
wrote:

Now it works, thank you anyway! I can not make it work without your
patient explanation these days! Your are a good guy!

Also, I want to say “Thank you” to everyone who have help me complete
my
job.! :slight_smile:

Excellent - please be so kind to summarize “lessions learned” in
the Wiki. I have added a stub page for this:

http://grass.osgeo.org/wiki/GRASS_and_Java

Then others can benefit and we’ll get better documentation.

ok, I may add something to the wiki.

given all the help you received, please be so kind and add a bit more
to above wiki page.

HI, I have added something yet. It is about the grass installation in Ubuntu
and the way to call the grass in the terminal and java app,however what I
posted is not formatted yet., I do not know how to highlight the codes .

done. But not sure if the page is already sufficient to be understood…

Thanks for your modifiation, maybe I did not make it clearly, what I want to express is the following:

It seems that calling grass in JAVA by using java.Lang.Runtime.exec() can be implemented easily in Linux platform (tested successfully on Ubuntu 9.10).

First install the grass use the apt-get:

sudo apt-get install grass # it will install the latest stable version

After the installation completed you can:

1 Test it in the terminal without JAVA (make sure that you have a GRASS database installed):

Open a terminal and enter:

grass64

If the grass installed successfully, you will get a welcome screen.

2 Call GRASS command directly in the terminal:

First you should set the environment(In my test, I found just the following four variables are required ):

export GISBASE=/usr/lib/grass64
export GISRC=/home/kk/gisrc
export LD_LIBRARY_PATH=$GISBASE/lib
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts

Then try some GRASS command directly in the terminal, for example:

g.version
g.gisenv

Now you can call GRASS in JAVA if the step 2 passed.

Just use the Runtime.getRuntime.exec(String cmd,String env):

String cmd=“g.version”;
String exe = { “bash”, “-c”, cmd };
String env={“GISBASE=…”,“GISRC=”"…}; // the environment variables which you set in the step 2.
p=Runtime.getRuntime.exec(exe,env);

Markus