Unable to change environment variables in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to change environment variables in bash script
# 1  
Old 10-26-2010
Unable to change environment variables in bash script

Hello!

For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which will be typed by me on the keyboard).

So if my password is 'Linda' the script has to change for instance the environment variable http_proxy from

myuser:PASSWORD

to

myuser:Linda

Could you please help me to fix the script below? There are two lines that do not work (indicated in the script). I have already tried many combinations of eval, \$$, etc...

Thanks a lot!
Marko

Code:
#!/bin/bash
read -s -p "Password: " passw; echo
perl_cmd="s/PASSWORD/$passw/g"
echo "perl_cmd = $perl_cmd"
#for var in `set | grep PASSWORD`; do  # fails if there are spaces in the value
for var in `set | perl -lne 'print $1 if /(.*?)=.*PASSWORD/'`; do  # works fine
         echo "var = $var"
         val=`echo $var | perl -pe '$perl_cmd'`  # does not work
         echo "val = $val"
         export $var=$val   # does not work
done

# 2  
Old 10-26-2010
Should

Code:
export $var=$val   # does not work

not be

Code:
export var=$val   # does not work

Correct me if I am wrong. I am learning as well!
# 3  
Old 10-26-2010
Hi dahlia,

The problem of what you propose is that I don't want to set a variable called "var" but instead a variable whose name is the value of "var".

Thanks!
Marko
# 4  
Old 10-26-2010
It is too difficult to guess what you are trying to achive from script which "does not work".
Please give a detailed example.
# 5  
Old 10-26-2010
Sorry, I realise now that my post was not clear enough.

All I want is be able to use the string "PASSWORD" instead of my actual password in the export commands inside my .bashrc and to change these variables replacing "PASSWORD" by the actual password by using a script. In that way my password is not written in the disk.

I just found a way to do it. Here is it:
Code:
read -s -p "Password: " passw; echo
for var in `set | perl -lne 'print $1 if /(.*?)=.*PASSWORD/'`; do
    eval val=\${$var//PASSWORD/$passw}
    #echo "val = $val"
    eval export $var=\'$val\'
done

The for line is kind of ugly, but I couldn't find a more elegant way of looping over all environment variables. Anyway, it works and I am happy with it.

The final difficulty I had with it is that if I put "#!/bin/bash" in the beginning and call the script as a sub shell I don't get the exports... The solution was to add

alias set-password='source $HOME/bin/set-password.sh'

to my .bashrc, to avoid having to type the source command.

Thanks a lot everybody!
Marko
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Challenge with sh script using environment variables to check for file.

Hi All Thanks for reviewing my question. I have a sh script where I used an environmental variable for the directory for the file I need to check to ensure before executing a process. I have confirmed the permissions and I can find the file if I use a hard coding of the directory. This is a... (5 Replies)
Discussion started by: rstojkovic68
5 Replies

2. Shell Programming and Scripting

Run script through cron with user environment variables

Hi everyone, I wrote a script that is supposed to be run by cron on a daily basis. It works just fine if I run it manually, but due to a lack of environment variables (which are available during my user session but not when cron runs the script) it keeps failing to run successfully. Here's the... (2 Replies)
Discussion started by: gacanepa
2 Replies

3. Shell Programming and Scripting

Sourcing .cshrc (C shell) environment variables to bash

I have tried with the following: csh -c 'source ~/.cshrc; exec bash' # works perfectly (cat ~/.cshrc; echo exec bash) | csh # not working And, using sed, I successfully retrieved the environment variables from ~/.cshrc sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc but now... (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. Shell Programming and Scripting

How to change Linux Terminal environment variable in a perl or bash script?

Hi, I meet an problem that it cannot change Terminal environment variable in a perl or bash script. This change can only exist and become effective in script lifetime. But I want to make this change take effect in current opened Terminal. In our view, the thought seems to be impossible, As... (9 Replies)
Discussion started by: weichanghe2000
9 Replies

5. Shell Programming and Scripting

How to use ifdef for bash variables in csh environment?

Hi Guys, I have a a bash script and i am exporting a variable in it. I am calling a csh script from this bash script. The variable "ABC" will be visible in csh script. ks.bash export ABC = abc ./kp.csh ab.csh echo $ABC setenv ABC =cde (i want to assign this value to ABC only if... (4 Replies)
Discussion started by: vdhingra123
4 Replies

6. Shell Programming and Scripting

Using Datastage environment variables in Unix script

Hi All, I am using ETL tool Datastage and is installed on Linux environment. Few environment variables are set in datastage. Now my requirement is to use those environment variables in a unix script. Is there any option I can do it? Sugeestions from people working on datastage and linux... (1 Reply)
Discussion started by: bghosh
1 Replies

7. Shell Programming and Scripting

environment variables in a sed script file

Hello Everyone I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this: { a\ STRINGTABLE DISCARDABLE\ BEGIN\ 5, 150 {a\ #define RC_SHELL, "%ID_SHELL%"\ #define RC_NAME, "%ID_NAME%"\ END } ... (1 Reply)
Discussion started by: edgarvm
1 Replies

8. Emergency UNIX and Linux Support

Problem setting environment variables from script

Hi all! I know that environment variables can be set on the .bashrc file, but I need to set them from a sh script. I saw a lot of websites that teach this but it doesn't work for me. #!/bin/sh DEKTOP=$DESKTOP=:/home/rrodrigues/Desktop export DESKTOP if I do echo $DESKTOP returns me... (10 Replies)
Discussion started by: ruben.rodrigues
10 Replies

9. Homework & Coursework Questions

BASH Environment Variables as arguments?

1. The problem statement, all variables and given/known data: Write a shell program called myenv which takes one argument. The argument should be the name of an environment variable, such as PATH HOME etc. myenv should print out the value of the variable given as the argument. If no argument is... (1 Reply)
Discussion started by: Helix
1 Replies

10. UNIX for Dummies Questions & Answers

change makefile environment variables

this is my first post so Hello, here is my question @top level Makefile should not set values for environment variables FC, CC, FFLAGS (etc) but use the ones that mpi_make sets. So as you can see i have to run an mpi program, in fortran and i am supposed to do the above.the program was... (3 Replies)
Discussion started by: Kwstas
3 Replies
Login or Register to Ask a Question