Star
Star

Reputation: 2299

Set environment variables in bash file calling a Matlab script

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.

Upvotes: 0

Views: 322

Answers (2)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

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.

Upvotes: 2

Muttley
Muttley

Reputation: 512

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

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

Upvotes: 3

Related Questions