linux - qgetenv returns NULL -
i need value of environment variable in linux(ubuntu). purpose using "qgetenv" function environment variable name. can confirm value of environment variable set because when echo $variable_name, printing value of variable. snippet of code using:
qbytearray root = qgetenv("paparazzi_home"); if (root.data()==null) { cerr << "paparazzi_home not defined" << endl; exit(0); } string pprzroot = string(root.data());
and echo $paparazzi_home prints following string:
"/home/manish/paprazzi-git/paparazzi/".
invoking program using sudo "path application dir"/"application name".
answer: "sudo" removes of user's environment variables. therefore, if 1 wants preserve environment variables along sudo, sudo -e must used. see "man sudo" more detail.
having
$ echo $paparazzi_home
print expected value doesn't verify $paparazzi_home
environment variable. unexported shell variable.
if set typing @ shell prompt:
$ paparazzi_home=/home/manish/paprazzi-git/paparazzi/
then it's not environment variable. try this:
$ export paparazzi_home=/home/manish/paprazzi-git/paparazzi/
and run program again. or, if you've assigned value it:
$ export paparazzi_home
some older shells, original sh
, don't support setting , exporting variable in 1 command. if you're using such shell, you'll need use 2 commands set environment variable:
$ paparazzi_home=/home/manish/paprazzi-git/paparazzi/ $ export paparazzi_home
if you're using csh or tcsh, syntax different. set (non-environment) shell variable:
% set paparazzi_home = /home/manish/paprazzi-git/paparazzi/
to set environment variable:
% setenv paparazzi_home /home/manish/paprazzi-git/paparazzi/
in csh , tcsh, shell variables , environment variables not closely tied in sh, ksh, bash, zsh, et al; there's no direct way (like export
command) change existing shell variable environment variable. , can have shell variable , environment variable same name, in case $paparazzi_home
expand value of shell variable. if you're using bash, can safely ignore paragraph.
another possibility program being launched in such way doesn't inherit shell's environment. if launch program ide or other gui, won't inherit environment variables set in shell -- unless launched ide or gui shell, , after setting environment variable. since you're on ubuntu, try launching application typing full path @ shell prompt.
oh, , don't need trailing /
.
** update :**
after lengthy discussion in chat, turns out op invoking program using sudo
, because needs root privileges.
sudo
, default, removes environment variables.
you can ask preserve environment using sudo -e
. man sudo
details.
it's not clear sudo -e
best solution; may have security implications.
if program needs 1 environment variable, this:
$ sudo env paparazzi_home=/home/manish/paprazzi-git/paparazzi /path/to/the/program
Comments
Post a Comment