Save an environment variable without using /bin/bash - c

I have a program that creates a environment variable called $EGG with this code
memcpy(buff,"EGG=",4);
putenv(buff);
system("/bin/bash");
And the value of buff is used to create an environment variable, and I use it through $EGG variable, but for use it I see that I must use the call system("/bin/bash");. Otherwise, if I don't use /bin/bash call I don't find my $EGG variable.
Is there a way to save my environment variable without calling /bin/bash?

Short answer: You cannot modify an existing environment the way you try.
Background: The program you use to create the environment variable EGG in gets its own environment on start-up (typically as copy of the environment of the process that starts the program). Inside the program's very own environment EGG is created.
When a program ends its environment is also gone and with it would had been created in there.
To modify an environment programmatically do not write a (C) program but use a script.
Using bash this could look like this:
#!/bin/bash
export EGG=4
save this as for example set_egg_to_4.sh and adjust the files mode do be able to execute the it:
$ chmod +x set_egg_to_4.sh
Then run the script:
$ ./set_egg_to_4.sh
and test for EGG, by doing
$ echo $EGG
4

To "permanently" set an environment variable, add it to your .bash_profile file.
export EGG=4
This file is sourced each time you start a login session, so that EGG is added to your environment each time. Any that inherit (directly or indirectly) from this shell will also have EGG in its environment.
There may be other files on your system that are sourced immediately on startup, so that a variable set in such a file is available to all processes (regardless of the user). (One I'm think of is /etc/environment.)

Related

How do I set an environment variable on macOs Mojave?

I'm installing 'THERMUS: A Thermal Model Package for ROOT' on macOs Mojave. One of the steps is (after unzipping) the following: "Set an environment variable `THERMUS' to point at the top-level directory containing the THERMUS code". I really don't know what to do.
I've seen the other post on stackoverflow: 'no rule to make target' with no reason, but, as I am a beginner, I really don't understand how I should set it. Please help me
To set an environment variable on Mac OSX, first open a terminal window.
If you are setting the environment variable to run jobs from the command line, use the following command:
export variable=value
where variable is the name of the environment variable (such as programmer) and value is the value you want to assign to the variable, (such as /opt/programmer/suites2013). You can find out which environment variables have been set with the env command.
If you are setting the environment variable globally to use with applications, use the commands given below. The environment variables set by these commands are inherited by any shell or application.
OS X 10.10
# To set an environment variable, enter the following command:
launchctl setenv variable "value"
# To find out if an environment variable is set, use the following command:
launchctl getenv variable
# To clear an environment variable, use the following command:
launchctl unsetenv variable

Permanent variables for batch files

My program is a GUI based install and run tool for a certain program, when I say GUI, I mean batch files. When it installs a program, it needs to always know where it is installed, currently, it just records it into a variable, but that only works if they don't shut the program and return at a later date, so I wanted to know a way of storing a permanent variable.
I was thinking maybe something like this:
set instloc='C:\Users\JoeBloggs\Documents\Environment\My Programs\This program.exe'
echo %instloc% > instloc.txt
attrib +h instloc.txt
But I was wondering if there was a dedicated command for it
Set modifies the current shell's environment values, and the change is temporary but available immediately. The change will not affect other shells that are running, and as soon as you close the shell, the new value is lost.
setx modifies the value permanently, which will affect in all future shells, but does not modify the environment of the shells already running. You have to exit the shell and reopen it before the change will be available, but the value will remain modified until you change it again.
In your case, instead of using set use setx:
setx instloc "C:\Users\JoeBloggs\Documents\Environment\My Programs\This program.exe"

Tracing Batch Variable

I have this batch which executes on the server computer. There is a scheduled job which runs the batch. The Batch detects a particular file and then it executes an sqlcmd like below:
if not exist %TRIG_FILE% goto No_Triggers
sqlcmd
-S %WSL_SERVER%
-d %WSL_DATABASE%
-E
-Q "DECLARE #RES integer;DECLARE #RET varchar(1);DECLARE #MSG varchar(65);EXEC Ws_Job_Release 1,'Release Job Unlock Batch','All',0,0,'Unlock_Batch',#RET OUTPUT,#MSG OUTPUT,#RES OUTPUT"
My question is - how did the batch know what the %WSL_SERVER% variable is, because when I look at the script, there is nowhere in there which sets the %WSL_SERVER% variable.
This is the first time I'm reading a .bat script, I know a fair bit of programming, but I can't see how that variable was passed into this script so that it knows which server. There's no other batch calling this, it's from the batch run by the scheduler.
thanks
gemmo
Most likely the WSL_SERVER and WSL_DATABASE are global environment variables initialised every time with your Windows session. That means they exist (are defined) in every CMD session and thus in every batch script. You can open a new Command Prompt window and issue this command
SET WSL
which will (try to) display all environment variables, whether global or local, whose names start with WSL. My guess is the output will show you at least the two WSL variables used in your script.
There is a number of global variables pre-defined and maintained by the OS. Yours, however, are probably user-defined (just my guess based on the fact that my system does not have them). User-defined variables can be created by third-party software or your own (maybe someone else's) batch scripts, as well as with a standalone invocation of the SETX command:
SETX VarName "Value"
You can use that command to change the value of any of your variables globally. Note that you can also change that value temporarily only, for the duration of the script, using the SET command as usual, if global change is undesirable:
SET "VarName=Value"

How to set Home and Path directories in C?

I am programming a small unix shell written in c. I want it to do only some basic commands for now. e.g. ls, pwd, ch
My problem is how do I set the Home directory and Path directory? I want to read the configuration from a text file so it can be easily changed whenever.
I am going to be using execv() to call unix functions such as ls. For example PATH
should determine the directories my shell should use to search for executable programs
when the user types a command
Thanks
They are all simply environment variables that you manipulate e. g. through setenv(3) (run man 3 setenv for details). The variables are HOME and PATH. See also man 7 environ.
Note that setting/changing an environment variable only influences the current process and all processes forked from it after the setting/changing (unlike in Windows, AFAIK).
Check out the function setenv. See man 3 setenv for information about it.
Unix already offers you an environmental variable that contains all the paths where system executables are stored. Retrieve the variable in your code with getenv("PATH"); Each path is separated with a ':' so all you need to do is tokenize and begin searching those paths for the executable your command wants to run. In this function you should also be able to search any path of your choosing for an executable.
You can decide what directory you want to start in ("home directory" as you say) by manipulating the current working directory before the shell prompts with chdir(). You can also use that unix function for implementing a cd command that can be used throughout the run time of the shell.

User defined command

I am having a situation for which I am looking for some suggestion.
Suppose I write a program which prints the directory names of the directories.
Is it possible to convert this program into a command (just on my system).
Not be alias but via C only.
As long as the file is executable (has the exec x access for the user starting it) and can be seen from the command interpreter (usually bash or sh), you can consider it to be a command.
There will be no difference in running your own file from your path than the ls command for instance.
Also, the C (or C++ ...) language is not a requirement. There are plenty of commands in, for instance, /usr/bin that are a script, meaning they're sh or bash (or even perl)...
access Ensure the file has the x access right (e.g. chmod u+x file)
path Ensure the file is in your PATH, or add an entry in your path (for instance) with PATH=$PATH:mypath
test Test it well before to put it in a path from which other users may have access
Put it in the path. On Linux, for example, you should put it in /usr/local/bin.
First, compile the program and create an executable using gcc program.c -o myexecfile. Then, an executable file named myexecfile is created in the same directory. You can run it by using ./myexecfile.
If you are on Unix(Linux etc.) and want to use it like ls or any other standard command, you need to place it in a directory that is specified in $PATH variable. For example, /usr/local/bin.

Resources