[GRASS-user] Using GRASS_BATCH_JOB but with a c++ program

Dear all,
I'm trying to launch a grass work with a script to automate calcul.
I try successfully with calling a shell script with GRASS_BATCH_JOB parameter (which call the test_algo.sh)
with the following shell script

#!/bin/bash
   chmod u+x $HOME/test_algo.sh
   export GRASS_BATCH_JOB=$HOME/test_algo.sh
   grass ~/grassdata/Roujan/simon9/
   unset GRASS_BATCH_JOB

I'm trying now to do the same thing with a c++ program which call the same shell script (test_algo.sh) and having the following lines (here the .cpp file):

#include <stdio.h>
#include <stdlib.h>

const char chmod="chmod u+x /home/rabotin/test_algo.sh";
const char expor="export GRASS_BATCH_JOB=/home/rabotin/test_algo.sh";
const char grass="grass -text ~/grassdata/Roujan/simon9/";
const char unset="unset GRASS_BATCH_JOB";

int main()
{
        printf ("Launching grass test!");
      system(chmod);
      system(expor);
      system(grass);
      system(unset);
      printf ("Launched grass test!");

        return 0;
}

But it doesnt' work well: GRASS is well launched, but no call to test_algo.sh and GRASS doesnt' end successfully

Can anyone have any idea to help me ?

Friendly,

Michael Rabotin

--
*********************************

Michaël Rabotin
Ingénieur d'étude en géomatique

Laboratoire d'étude des Interactions Sol, Agrosystème et Hydrosystème
UMR LISAH SupAgro-INRA-IRD
Bat. 24
2 place Viala
34060 Montpellier cedex 1 FRANCE

Téléphone : 33 (0)4 99 61 23 85
Secrétariat : 33 (0)4 99 61 22 61
Fax : 33 (0)4 67 63 26 14
E-mail : rabotin@supagro.inra.fr

*********************************

rabotin wrote:

Dear all,
I'm trying to launch a grass work with a script to automate calcul.
I try successfully with calling a shell script with GRASS_BATCH_JOB
parameter (which call the test_algo.sh)
with the following shell script

#!/bin/bash
   chmod u+x $HOME/test_algo.sh
   export GRASS_BATCH_JOB=$HOME/test_algo.sh
   grass ~/grassdata/Roujan/simon9/
   unset GRASS_BATCH_JOB

I'm trying now to do the same thing with a c++ program which call the
same shell script (test_algo.sh) and having the following lines (here
the .cpp file):

#include <stdio.h>
#include <stdlib.h>

const char chmod="chmod u+x /home/rabotin/test_algo.sh";
const char expor="export GRASS_BATCH_JOB=/home/rabotin/test_algo.sh";
const char grass="grass -text ~/grassdata/Roujan/simon9/";
const char unset="unset GRASS_BATCH_JOB";

int main()
{
        printf ("Launching grass test!");
      system(chmod);
      system(expor);
      system(grass);
      system(unset);
      printf ("Launched grass test!");

        return 0;
}

But it doesnt' work well: GRASS is well launched, but no call to
test_algo.sh and GRASS doesnt' end successfully

Can anyone have any idea to help me ?

Environment variables are per-process. Each system() call spawns a new
shell process, and the export command will only affect the process in
which it's run, not any other processes. From a C/C++ program, use
putenv() instead, e.g.:

  system(chmod);
  putenv("GRASS_BATCH_JOB=/home/rabotin/test_algo.sh");
  system(grass);

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