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:
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.
You could try something like:-
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.
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:-
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.
You can avoid the inefficient and unnecessary command substitution pipelines by using IFS:
Quote:
Originally Posted by rbatte1
You could try something like:-
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.
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:-
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.
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:
Regards,
Alister
Last edited by alister; 07-22-2013 at 08:50 AM..
These 2 Users Gave Thanks to alister For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)