Read variables from a file and then export it.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read variables from a file and then export it.
# 1  
Old 07-22-2013
Read variables from a file and then export it.

Hi
I want to read variables from one file and then set it as environment variable;
The text file is test.txt which contains
SPEED:1000
IP:172.26.126.11
My code is:
Code:
while read line; do
var1=`echo $line | awk 'BEGIN {FS=":"} { print $1 }'`
echo $var1
var2=`echo $line | awk 'BEGIN {FS=":"} { print $2 }'`
echo $var2
export $var1=$var2
echo $var1
done < test.txt

But it does not set SPEED=1000 or IP:172.26.126.11
It gives me output :
SPEED
1000
SPEED
IP
172.26.126.11
IP
Please help me to export those variables.
Thanks.
# 2  
Old 07-22-2013
Code:
eval export $var1=$var2

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 07-22-2013
You could try something like:-
Code:
`cat test.txt | tr ":" "="`

This will change the colon character to be an equal sign and then execure the resulting statement. This must come with a warning that you must be absolutely certain of the integrity of the process creating the file. Consider that the following input file would cause damaging results.
Code:
SPEED:1000
IP:172.26.126.11
rm -r /

The process is using cat, I know but I can seem to get tr to take an input file. It would be far better if the source file could be written with the equal already in it and then you would simply:-
Code:
. test.txt




Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 07-22-2013
Thanks alot Smilie
Code:
eval export $var1=$var2

It worked !!!
# 5  
Old 07-22-2013
Again, I would caution you that you must be certain that no rogue entries appear that could destroy your system. I wouldn't say that eval is evil in itself, but it does introduce risks.



Robin
# 6  
Old 07-22-2013
Quote:
Originally Posted by SSM
Code:
while read line; do
var1=`echo $line | awk 'BEGIN {FS=":"} { print $1 }'`
echo $var1
var2=`echo $line | awk 'BEGIN {FS=":"} { print $2 }'`

You can avoid the inefficient and unnecessary command substitution pipelines by using IFS:
Code:
while IFS=: read name value; do
    eval export $name=$value

Quote:
Originally Posted by rbatte1
You could try something like:-
Code:
`cat test.txt | tr ":" "="`

This will change the colon character to be an equal sign and then execure the resulting statement. This must come with a warning that you must be absolutely certain of the integrity of the process creating the file. Consider that the following input file would cause damaging results.
Code:
SPEED:1000
IP:172.26.126.11
rm -r /

The process is using cat, I know but I can seem to get tr to take an input file. It would be far better if the source file could be written with the equal already in it and then you would simply:-
Code:
. test.txt

Your main point stands, but your specific example is harmless. While eval'ing untrusted input is a massive risk, rm -r / leading to export rm -r /= won't cause any damage. I would expect nothing more than a harmless syntax error message. Worst case, perhaps an empty variable named rm is exported into the environment. However, foo:bar; rm -r / leading to export foo=bar; rm -r / will cause grief.

With regard to tr, it doesn't take file arguments. It's a simple filter that reads from stdin and writes to stdout. However, you can read a file without using cat; just use redirection.
Code:
tr ... < file

Sourcing a file is simpler and cleaner than a while-read-eval loop, but it's no safer. If the input is untrusted, eval and sourcing both execute it.

If the shell supports process substitution, the file's contents can be transformed in transit:
Code:
. <(sed 's/:/=/; s/^/export /')

Regards,
Alister

Last edited by alister; 07-22-2013 at 08:50 AM..
These 2 Users Gave Thanks to alister For This Post:
# 7  
Old 07-22-2013
Yes. It is really helpful.
Code:
while IFS=: read name value; do    
 eval export $name=$value

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

2. Shell Programming and Scripting

Read the text file and connect to DB then export the DB

hi, i'm new to shell scripting. I have a text file(sample.txt). It contains the different username & password for different users of oracle db. .txt content user1|password1 user2|password2 user3|password3 user4|password4 First read the text file and take the username and password then... (5 Replies)
Discussion started by: priya001
5 Replies

3. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

4. Shell Programming and Scripting

Read env variables from argument file

Hi, I have a generic shell script (runBatchJob.sh) to read files (batchJob) with commands in them and execute the commands by reading the batchJob file as below ./runBatchJob.sh batchJob batchJob file $BATCHDIR/execute_procedure.sh $DATADIR/collectData.sh $OTHER_ENV_VAR/doSomething.sh ... (10 Replies)
Discussion started by: bOngY
10 Replies

5. Shell Programming and Scripting

How to read a file and assign variables to data?

Hi, I need a simple csh script to read a file containing data like this Buy Transactions : 175 Sell Transactions : 212 Server: sepo2 i want to read both field and its value and assign variables to each.. (2 Replies)
Discussion started by: pravsripad
2 Replies

6. Shell Programming and Scripting

Read variables and their values from file

Hi, I want to read the variables and the values from the txt file and compare these values with the ones computed by script. for ex: say var.txt contains the variable names and their values: one 1 two 2 three 3 The value of variables "one" "two" and "three" will be computed in the script... (3 Replies)
Discussion started by: bhushana
3 Replies

7. Shell Programming and Scripting

Piping through commands read as variables from file

This is going to be part of a longer script with more features, but I have boiled it down to the one thing that is presently stumping me. The goal is a script which checks for updates to web pages that can be run as a cron job. The script reads (from a tab-delim file) a URL, an MD5 digest, and an... (1 Reply)
Discussion started by: fitzwilliam
1 Replies

8. Shell Programming and Scripting

Read variables contain spaces from text file

Dears, I developed a shell script to read varibales from text file as the following: cat /dev/null > /rename-OUT.txt while read line do set -- `echo $line` snmpset -c dslmibs $1 sysName.0 octetstring $2 after=$(snmpget -c dslmibs $1 sysName.0 | cut -d: -f3) echo "$1,$2,$after" >>... (1 Reply)
Discussion started by: ahmed.zaher
1 Replies

9. Solaris

not able to export the Variables

I am working with Sun Solaris 9 and I want to export the environment variable from my application(xxxx.ksh) but I am not able to see it when I am using SET command I am writing some variables which I have to set COMMON_USER_HOME=${HOME} export COMMON_USER_HOME echo... (6 Replies)
Discussion started by: smartgupta
6 Replies

10. Shell Programming and Scripting

export variables

I have a master shell, which calls another shell to export some env variables. But when I just run the child shell from the command line, and see if the variables are exported by doing, echo $EXPORTED_VAR1 I am not seeing the value. But I am sure, I am using the child shell from a master... (4 Replies)
Discussion started by: srishan
4 Replies
Login or Register to Ask a Question