Reading from a file and storing it in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading from a file and storing it in a variable
# 1  
Old 09-23-2010
Reading from a file and storing it in a variable

Hi folks,
I'm using bash and would like to do the following. I would like to read some values from the file and store it in the variable and use it.

My file is 1.txt and its contents are

VERSION=5.6
UPDATE=4




I would like to read "5.6" and "4" and store it in a variable in shell script. How do I do that?

Thanks,
# 2  
Old 09-23-2010
You can use: eval $(<file)
# 3  
Old 09-23-2010
Quote:
Originally Posted by danmero
You can use: eval $(<file)
I don't understand this though. How do I search for patterns in a text file and read the whole line in shell script?
# 4  
Old 09-23-2010
Code:
# cat file
VERSION=5.6
UPDATE=4

# cat script.sh
#!/bin/bash -x
eval $(<file)
echo ${VERSION} ${UPDATE}

# /temp/script.sh
+ eval VERSION=5.6 UPDATE=4
++ VERSION=5.6
++ UPDATE=4
+ echo 5.6 4
5.6 4

# 5  
Old 09-23-2010
Can any one help, why my code is not right?

Code:
IFS="="
while read key value
do
  $key=$value
done < 1.txt


Last edited by rdcwayx; 09-23-2010 at 11:12 PM..
# 6  
Old 09-23-2010
Quote:
Originally Posted by rdcwayx
Can any one help, why it is not right?

Code:
IFS="="
while read key value
do
  $key=$value
done < 1.txt

That's correct, however my previous solution will do exactly the same
Code:
eval $(<file)

# 7  
Old 09-23-2010
Quote:
Originally Posted by rdcwayx
Code:
$key=$value

variables are declared like this
Code:
 key=$value

however, since they are taken from file, you can use declare (instead of eval , which is dangerous in some cases)
Code:
 while IFS== read -r key value do declare $key=$value done


Last edited by kurumi; 09-23-2010 at 11:06 PM..
This User Gave Thanks to kurumi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to compare each file in two directores by storing in variable

In the below bash I am trying to read each file from a specific directory into a variable REF or VAL. Then use those variables in an awk to compare each matching file from REF and VAL. The filenames in the REF are different then in the VAL, but have a common id up until the _ I know the awk portion... (15 Replies)
Discussion started by: cmccabe
15 Replies

2. UNIX for Beginners Questions & Answers

Storing file contents to a variable

Hi All, I was trying a shell script. I was unable to store file contents to a variable in the script. I have tried the below but unable to do it. Input = `cat /path/op.diary` Input = $(<op.diary) I am using ksh shell. I want to store the 'op.diary' file contents to the variable 'Input'... (12 Replies)
Discussion started by: am24
12 Replies

3. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

4. Shell Programming and Scripting

Storing multiple file paths in a variable

I am working on a script for Mac OS X that, among many other things, gets a list of all the installed Applications. I am pulling the list from the system_profiler command and formatting it using grep and awk. The problem is that I want to be able to use each result individually later in the script.... (3 Replies)
Discussion started by: cranfordio
3 Replies

5. Shell Programming and Scripting

storing a value from another file as a variable[solved]

Hi all, im having snags creating a variable which uses commands like cut and grep. In the instance below im simply trying to take a value from another file and assign it to a variable. When i do this it only prints the $a rather than the actual value. I know its simple but does anyone have any... (1 Reply)
Discussion started by: somersetdan
1 Replies

6. Shell Programming and Scripting

Storing lines of a file in a variable

i want to store the output of 'tail -5000 file' to a variable. If i want to access the contents of that variable, it becomes kinda difficult because when the data is stored in the variable, everything is mushed together. you dont know where a line begins or ends. so my question is, how can i... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Programming

Reading Scientific notation from file and storing in array

Hi, I am trying to read a set of numbers that are in scientific notation into a file so I can do some math on them, but when I display the array contents the numbers aren't the same as the numbers in the file. Could someone explain why? Thanks. int main() { double fArray; ... (3 Replies)
Discussion started by: Filter500
3 Replies

8. Shell Programming and Scripting

Storing the contents of a file in a variable

There is a file named file.txt whose contents are: +-----------------------------------+-----------+ | Variable_name | Value | +-----------------------------------+-----------+ | Aborted_clients | 0 | | Aborted_connects | 25683... (6 Replies)
Discussion started by: proactiveaditya
6 Replies

9. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

10. UNIX for Dummies Questions & Answers

Ksh Storing Multiple Files and reading each line in each file.

How would I go about storing multiple file paths in a directory that begin like: 20080402* and run a loop that reads each line of each file thats in a given directory. So far this is what I have: #!/bin/ksh echo "ENTER Reprint Date (YYYYMMDD): " read ReprintDate echo ""... (1 Reply)
Discussion started by: developncode
1 Replies
Login or Register to Ask a Question