Why doesnt "export" works in linux system() cmd to set a linux shell environment variable at runtime? - linux

I was trying to set a linux shell environment variable at run time in software using system() command. In the target, couldn't see it set.
for eg.
snprintf(buf, sizeof(buf), "export A=%s", "luck");
system(buf);
Tried /bin/sh -c "export A=B" in the target run time and had no success.

The system call starts a new shell and runs the command in there.
The environment variable will be set in that shell only.
To set an environment variable in your own process, use putenv.

Related

How to run a shell script as source using python to set environment variables?

I created a shell script which is used to set the environment variables. I am trying to run this script via a python script but every time shows me error as "Command not found". I want to run the shell script as source for setting up the environment variables.
Python : 2.7.5
Script Name : abc.sh
Normal Execution on shell : source abc.sh
Tried using
python : os.system ("source abc.sh")
this shows an error as
"Command not found".
Anybody can help me out how to run this script successfully as source via python ?
The error is because source is a shell "builtin" command -- it is not an external command in your $PATH. But the more fundamental problem is that what you're trying to do won't work due to how environment variables work.
Env vars are private to each process. They are not global and a process cannot modify the env vars of a different process; at least not without the cooperation of the other process. When you start a process it inherits a copy of the env vars of the parent process or the parent provides an explicit set of env vars to the child process it spawns. In either event each process has its own, private, env vars. So even if you did
os.system("sh -c 'source abc.sh'")
it would only modify the env vars of the sh subprocess. It would not modify the environment of the python process.
The simplest solution is to start a shell, do the source abc.sh, then exec your python program. If you absolutely have to set the env vars by running a shell script from within your python program your script will have to write the vars to stdout. Your python program will then have to read that output and parse it to extract the env vars names and value then call os.putenv() to set each var in the python process.
Try your command in command prompt. This because you have not define os in the your command
python : os.system ("source abc.sh"). Your computer is unable access os.system. You first need to install the packages of python which os.system module in it.

Defining shell environment variables in tcsh [duplicate]

This question already has answers here:
Can a shell script set environment variables of the calling shell? [duplicate]
(20 answers)
Closed 3 years ago.
Unable to create environment variables using a tcsh script.
Tried set, but works only inside the script.
setenv doesn't work outside the script.
export says "command not found" in the terminal I'm trying to run.
#!/usr/intel/bin/tcsh
#set WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
setenv WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
echo $WV
env $WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
I expect the output to be /p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv, when i echo the environment variable WV on the terminal, but i am getting the error of undefined variable.
Environment variables are set in the current process and inherited by child processes. You can't set environment variables in a parent process.
You have to use the source command to execute the script. That makes the current shell process execute the script itself, rather than running it in a child process.
source env_vars.tcsh
set is for setting shell variables, not environment variables. export is a bash command (and also other shells based on Bourne Shell syntax), not a tcsh command.
env requires the arguments before the program name to be variable settings in the form name=value, e.g.
env VAR1=val1 VAR2=val2 /p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv
It runs the program with those variables added to the environment.

Setting console env using a shell script

I have a shell script setmyenv.sh as below
#!/bin/sh
export PATH=./abc/tools:$PATH
env | grep PATH
When I run it sh setmyenv.sh, I could see that the PATH env is set accordingly.
PATH=./abc/tools:<whatever my existing PATH setting>
However, after my command finish, if I manually type env | grep PATH on the console, I got
PATH=<whatever my existing PATH setting>
I lost the setting that I set using setmyenv.sh
It looks like the environement is only set in the lifetime of my script run.
How could I have the environment set sticky even after the script ended. i.e. the purpose of the script is to set the environment.?
P/S: I don't want to set it in my .bash_profile nor etc\profile, given I only want to set it when needed, by calling setmyenv.sh, but not every time I open my console. i.e. not per the answer of Using .sh script to set an environment variable or How to set global environment variables using shell script .sh
When you run
sh setmyenv.sh
it runs in a separate sh process and the changes to PATH are lost when the process finishes.
You need to source your script:
source setmyenv.sh
or
. setmyenv.sh
so that it runs in your current shell and all variable assignments are preserved. Remember not to have any exit in setmyenv.sh script. If you do, sourcing the script will terminate your shell.
See also:
Difference between sourcing a script vs executing it
What's a subshell

Shell script running from php code is using /sbin/nologin how to set this to /bin/bash

I am running a shell script by which we are scheduling a task using at command. But it schedules the at task but its not running the same beacuse its using shell /sbin/nologin when we are calling it from php code. It works fine if we run it from terminal.
You should check the "$PATH" env variable. When you are logged in from terminal the shell has initialized it's search path via .bashrc etc. "cron" or "at" jobs don't do that.
So try to log the environment variables to a file in your 'at' jobs and check if it is set up right.

How to launch gnome-terminal from command line and duplicate environment variables?

Is there a way to launch a gnome-terminal from the command line (i.e., using the /usr/bin/gnome-terminal command) and have the new terminal inherit the environment variables and other set variables of terminal from which the command was run? The scenario is thus:
Open a terminal
Set some variables
Set some environment variables
Launch an executable that needs the variables and the environment variables of the current terminal in a new terminal
Thoughts?
This turned out to be a gnome-terminal issue. When launching the gnome-terminal, specifying the --disable-factory option provides the following directive:
"Do not register with the activation name server, and do not reuse an already running GNOME terminal process" (gnome-terminal man page)
Specifying this option was required in order to inherit the environment of the previous shell.
You need to use export against the variables in the other shell if you want child processes to inherit them.

Resources