Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script for setting environment variables 1

Status
Not open for further replies.

MickeyZub

Programmer
Mar 14, 2006
3
IE
Hi all,

I'm looking for help in writing a script that will extract environment variables names and values from a text file and set them in the current C-Shell using 'setenv' command. The text file has the following format:

# Comments start with # sign
VARIABLE_NAME_1 variable_value_1
VARIABLE_NAME_2 variable_value_2

I tried the following:
#!/bin/csh
awk '/^[^#]/ {print "setenv "$1" "$2}' env_variables.txt | csh

which didn't produce any errors, but also didn't set the variables.

Any help would be much appreciated.
Mickey
 
Does csh have an eval function or a source command ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
So, the simplest way:
awk '/^[^#]/ {print "setenv "$1" "$2}' env_variables.txt > /tmp/$$
source /tmp/$$
rm -f /tmp/$$

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
can be done with source and alias construction

if you have this script in your $HOME dir:
set_env_vars.csh:
#!/bin/csh
awk '/^[^#]/ {print "setenv "$1" "$2}' env_variables.txt >$HOME/set_env_vars.source

and if this alias is set e.g. in $HOME/.cshrc
alias set_env_vars="$HOME/set_env_vars.csh; source $HOME/set_env_vars.source"

then issueing the command

set_env_vars

would first run the csh script to process the text_file into the 'source' file; and then source the processed file to set the env vars in your current cshell.

Note, I haven't tried it out. I don't have a cshell here, and don't use it much anyway. I prefer Korn shell.


HTH,

p5wizard
 
Thanks a lot for suggestions. I used the one from PHV and after I source my simple script the environment variables are all set to correct values.
Thanks again,
Mickey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top