Set environment variables in bash file calling a Matlab script









up vote
0
down vote

favorite
1












I have the following bash file launching some Matlab m-files (main.m and f.m which are scripts) 4 times (4 tasks).



#$ -S /bin/bash
#$ -l h_vmem=4G
#$ -l tmem=4G
#$ -cwd
#$ -j y

#Run 4 tasks where each task has a different $SGE_TASK_ID ranging from 1 to 4
#$ -t 1-4

#$ -N example
date
hostname

#Output the Task ID
echo "Task ID is $SGE_TASK_ID"

/share/apps/[...]/matlab -nodisplay -nodesktop -nojvm -nosplash -r "main; ID = $SGE_TASK_ID; f; exit"


The f.m script uses the Gurobi toolbox and I have been told that in order for the file to execute properly I have to set the environment variable



GRB=/apps/[...].lic


where [...] contains the path.



I am a very beginner on how to write bash files and I apologise if my question is silly: where/how/what should I write on the batch file above to use the Gurobi toolbox?



I have googled on how to set environment variables but I got confused between setting, exporting, env. There are many similar questions on in this forum but, since they apply to apparently differently structured batch files, I couldn't understand whether their answers can be tailored also to my case.










share|improve this question



















  • 1




    Replaced the obviously wrong tag [batch-file] with [bash]
    – LotPings
    yesterday















up vote
0
down vote

favorite
1












I have the following bash file launching some Matlab m-files (main.m and f.m which are scripts) 4 times (4 tasks).



#$ -S /bin/bash
#$ -l h_vmem=4G
#$ -l tmem=4G
#$ -cwd
#$ -j y

#Run 4 tasks where each task has a different $SGE_TASK_ID ranging from 1 to 4
#$ -t 1-4

#$ -N example
date
hostname

#Output the Task ID
echo "Task ID is $SGE_TASK_ID"

/share/apps/[...]/matlab -nodisplay -nodesktop -nojvm -nosplash -r "main; ID = $SGE_TASK_ID; f; exit"


The f.m script uses the Gurobi toolbox and I have been told that in order for the file to execute properly I have to set the environment variable



GRB=/apps/[...].lic


where [...] contains the path.



I am a very beginner on how to write bash files and I apologise if my question is silly: where/how/what should I write on the batch file above to use the Gurobi toolbox?



I have googled on how to set environment variables but I got confused between setting, exporting, env. There are many similar questions on in this forum but, since they apply to apparently differently structured batch files, I couldn't understand whether their answers can be tailored also to my case.










share|improve this question



















  • 1




    Replaced the obviously wrong tag [batch-file] with [bash]
    – LotPings
    yesterday













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have the following bash file launching some Matlab m-files (main.m and f.m which are scripts) 4 times (4 tasks).



#$ -S /bin/bash
#$ -l h_vmem=4G
#$ -l tmem=4G
#$ -cwd
#$ -j y

#Run 4 tasks where each task has a different $SGE_TASK_ID ranging from 1 to 4
#$ -t 1-4

#$ -N example
date
hostname

#Output the Task ID
echo "Task ID is $SGE_TASK_ID"

/share/apps/[...]/matlab -nodisplay -nodesktop -nojvm -nosplash -r "main; ID = $SGE_TASK_ID; f; exit"


The f.m script uses the Gurobi toolbox and I have been told that in order for the file to execute properly I have to set the environment variable



GRB=/apps/[...].lic


where [...] contains the path.



I am a very beginner on how to write bash files and I apologise if my question is silly: where/how/what should I write on the batch file above to use the Gurobi toolbox?



I have googled on how to set environment variables but I got confused between setting, exporting, env. There are many similar questions on in this forum but, since they apply to apparently differently structured batch files, I couldn't understand whether their answers can be tailored also to my case.










share|improve this question















I have the following bash file launching some Matlab m-files (main.m and f.m which are scripts) 4 times (4 tasks).



#$ -S /bin/bash
#$ -l h_vmem=4G
#$ -l tmem=4G
#$ -cwd
#$ -j y

#Run 4 tasks where each task has a different $SGE_TASK_ID ranging from 1 to 4
#$ -t 1-4

#$ -N example
date
hostname

#Output the Task ID
echo "Task ID is $SGE_TASK_ID"

/share/apps/[...]/matlab -nodisplay -nodesktop -nojvm -nosplash -r "main; ID = $SGE_TASK_ID; f; exit"


The f.m script uses the Gurobi toolbox and I have been told that in order for the file to execute properly I have to set the environment variable



GRB=/apps/[...].lic


where [...] contains the path.



I am a very beginner on how to write bash files and I apologise if my question is silly: where/how/what should I write on the batch file above to use the Gurobi toolbox?



I have googled on how to set environment variables but I got confused between setting, exporting, env. There are many similar questions on in this forum but, since they apply to apparently differently structured batch files, I couldn't understand whether their answers can be tailored also to my case.







bash matlab environment-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









user3285148

631525




631525







  • 1




    Replaced the obviously wrong tag [batch-file] with [bash]
    – LotPings
    yesterday













  • 1




    Replaced the obviously wrong tag [batch-file] with [bash]
    – LotPings
    yesterday








1




1




Replaced the obviously wrong tag [batch-file] with [bash]
– LotPings
yesterday





Replaced the obviously wrong tag [batch-file] with [bash]
– LotPings
yesterday













2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Within your bash file, just add the following line before launching the matlab m-files:



export GRB="/apps/[...].lic"





share|improve this answer



























    up vote
    2
    down vote













    Environment variables are owned by a process, a running process can't change environment of another running process, when creating a new process exported variables of parent are set in child process by default, the environment variables changed in child process can't affect parent process.



    GRB=/apps/[...].lic will set variable GRB to a value in bash process it can be seen using echo "$GRB" for example but this variable is not exported, means that when calling matlab, for matlab process environment variable GRB will not be set. Using export GRB before calling matlab will make the variable exported to matlab process.



    There's also a syntax to set environment variable for a new process without affecting current bash process: GRB=/apps/[...].lic /share/apps/[...]/matlab ....



    For further details man bash /export /^ENVIRONMENT



    Also compare output of following commands, set (a builtin, a bash "function" no new process created), env (/usr/bin/env a command, a new process is created and only sees exported variables)



    $ set
    $ env


    the first shows variables, whereas the second environnment which is a subset of first.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223693%2fset-environment-variables-in-bash-file-calling-a-matlab-script%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      Within your bash file, just add the following line before launching the matlab m-files:



      export GRB="/apps/[...].lic"





      share|improve this answer
























        up vote
        2
        down vote



        accepted










        Within your bash file, just add the following line before launching the matlab m-files:



        export GRB="/apps/[...].lic"





        share|improve this answer






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Within your bash file, just add the following line before launching the matlab m-files:



          export GRB="/apps/[...].lic"





          share|improve this answer












          Within your bash file, just add the following line before launching the matlab m-files:



          export GRB="/apps/[...].lic"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Muttley

          2748




          2748






















              up vote
              2
              down vote













              Environment variables are owned by a process, a running process can't change environment of another running process, when creating a new process exported variables of parent are set in child process by default, the environment variables changed in child process can't affect parent process.



              GRB=/apps/[...].lic will set variable GRB to a value in bash process it can be seen using echo "$GRB" for example but this variable is not exported, means that when calling matlab, for matlab process environment variable GRB will not be set. Using export GRB before calling matlab will make the variable exported to matlab process.



              There's also a syntax to set environment variable for a new process without affecting current bash process: GRB=/apps/[...].lic /share/apps/[...]/matlab ....



              For further details man bash /export /^ENVIRONMENT



              Also compare output of following commands, set (a builtin, a bash "function" no new process created), env (/usr/bin/env a command, a new process is created and only sees exported variables)



              $ set
              $ env


              the first shows variables, whereas the second environnment which is a subset of first.






              share|improve this answer


























                up vote
                2
                down vote













                Environment variables are owned by a process, a running process can't change environment of another running process, when creating a new process exported variables of parent are set in child process by default, the environment variables changed in child process can't affect parent process.



                GRB=/apps/[...].lic will set variable GRB to a value in bash process it can be seen using echo "$GRB" for example but this variable is not exported, means that when calling matlab, for matlab process environment variable GRB will not be set. Using export GRB before calling matlab will make the variable exported to matlab process.



                There's also a syntax to set environment variable for a new process without affecting current bash process: GRB=/apps/[...].lic /share/apps/[...]/matlab ....



                For further details man bash /export /^ENVIRONMENT



                Also compare output of following commands, set (a builtin, a bash "function" no new process created), env (/usr/bin/env a command, a new process is created and only sees exported variables)



                $ set
                $ env


                the first shows variables, whereas the second environnment which is a subset of first.






                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Environment variables are owned by a process, a running process can't change environment of another running process, when creating a new process exported variables of parent are set in child process by default, the environment variables changed in child process can't affect parent process.



                  GRB=/apps/[...].lic will set variable GRB to a value in bash process it can be seen using echo "$GRB" for example but this variable is not exported, means that when calling matlab, for matlab process environment variable GRB will not be set. Using export GRB before calling matlab will make the variable exported to matlab process.



                  There's also a syntax to set environment variable for a new process without affecting current bash process: GRB=/apps/[...].lic /share/apps/[...]/matlab ....



                  For further details man bash /export /^ENVIRONMENT



                  Also compare output of following commands, set (a builtin, a bash "function" no new process created), env (/usr/bin/env a command, a new process is created and only sees exported variables)



                  $ set
                  $ env


                  the first shows variables, whereas the second environnment which is a subset of first.






                  share|improve this answer














                  Environment variables are owned by a process, a running process can't change environment of another running process, when creating a new process exported variables of parent are set in child process by default, the environment variables changed in child process can't affect parent process.



                  GRB=/apps/[...].lic will set variable GRB to a value in bash process it can be seen using echo "$GRB" for example but this variable is not exported, means that when calling matlab, for matlab process environment variable GRB will not be set. Using export GRB before calling matlab will make the variable exported to matlab process.



                  There's also a syntax to set environment variable for a new process without affecting current bash process: GRB=/apps/[...].lic /share/apps/[...]/matlab ....



                  For further details man bash /export /^ENVIRONMENT



                  Also compare output of following commands, set (a builtin, a bash "function" no new process created), env (/usr/bin/env a command, a new process is created and only sees exported variables)



                  $ set
                  $ env


                  the first shows variables, whereas the second environnment which is a subset of first.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  Nahuel Fouilleul

                  13.7k1525




                  13.7k1525



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223693%2fset-environment-variables-in-bash-file-calling-a-matlab-script%23new-answer', 'question_page');

                      );

                      Post as a guest














































































                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20