Write string variables to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write string variables to file
# 1  
Old 01-18-2012
Write string variables to file

I have the following:

Code:
#! /bin/bash
foo="bar"
this="that"

vars="foovar=$foo\n\
thisvar=$this\n"

I want to write the following to a file:

Code:
foovar="bar"
thisvar="that"

Then in another script, I pull this file, and loop through it:

Code:
while read line; do
    eval $line
done < "foofile"

Then I can use $foovar in the new script. I had this working before, but lost the script, and completely forgot how I had set it up Smilie
# 2  
Old 01-18-2012
Have you tried:

Code:
printf "foovar=\"%s\"\n" "$foo" >>foofile

# 3  
Old 01-22-2012
Quote:
Originally Posted by Validatorian
Code:
while read line; do
    eval $line
done < "foofile"


You don't need a loop to read the values from the file; you can just source it:
Code:
. foofile

# 4  
Old 01-24-2012
Quote:
Originally Posted by cfajohnson

You don't need a loop to read the values from the file; you can just source it:
Code:
. foofile

That is wonderful! Exactly what I needed. Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I write variables to same line of a file?

I am trying to keep variables in a file. if I have all variables at the same time, I can write them all like below. echo $var1","$var2","$var3 But, these variables are being calculated at different times then they are lost so I want to keep them in a file seperated by "," . echo... (5 Replies)
Discussion started by: snr_silencer
5 Replies

2. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

3. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

4. Shell Programming and Scripting

Extract string from a file & write to a new file (Perl)

Hi, This is the first time playing around with perl and need some help. Assuming if i have a line of text that looks like this: Date/Time=Nov 18 17:12:11;Device Name=192.168.1.1;Device IP=192.168.1.1;Device Class=IDS;Source IP=155.212.212.111;Source Name=UNKNOWN;Source Port=1679... (3 Replies)
Discussion started by: LuckyGuy
3 Replies

5. Shell Programming and Scripting

How to delete a string pattern in a file and write back to the same file

I have a control file which looks like this LOAD DATA INFILE '/array/data/data_Finished_T5_col_change/home/oracle/emp.dat' PRESERVE BLANKS INTO TABLE SCOTT.EMP FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (................. ..................) How can i edit the... (1 Reply)
Discussion started by: mwrg
1 Replies

6. UNIX for Dummies Questions & Answers

How to write a string with special characters to a file

Hi, I want to erase the contents of a file and write a sql query into the file. "" > tmp.txt "select count\(\*\) \"Names\" from v\$ground where status\=\'Invalid\'\;" > tmp.txt doesnt work, pls help. (2 Replies)
Discussion started by: happyrain
2 Replies

7. UNIX for Dummies Questions & Answers

write string of characters to file

Hi, I have a text file with a very long string of characters, like "ACGCTTGCAA...", and I want to make another file with 36 characters from this string file every 4 lines like: character of position 1 to position 36 of the string file, position 2-37, position 3-38 ... position... (2 Replies)
Discussion started by: fadista
2 Replies

8. Shell Programming and Scripting

line contains a string write to new file otherwise append

hi i have the following function, reading file line by line, if line contains "Change state" i need to cut some fields otherwise i need to append the total line to the new file function parse() { while read LINE do echo $LINE |grep -q "Change state" if then echo $LINE |grep -q... (1 Reply)
Discussion started by: Satyak
1 Replies

9. Shell Programming and Scripting

Can I write a string into file from shell?

Hi , If I used this wc -l *.dat ,it will count number of line in these files like this 1234 test1.dat 2345 test2.dat 3456 test3.dat 7035 total can I add '|'(pipe) in the middle of those number and string like this 1234|test1.dat 2345|test2.dat 3456|test3.dat Thanks in advance. (10 Replies)
Discussion started by: guitaroa47
10 Replies

10. Shell Programming and Scripting

Write system variables into file

Hi everyone, I was told, i my job, to do a script that creates the backup of all the files that are important to us. So i created the script, put it in the crontab and it works great. Now what i want is to write to a file what directories have being copied with date and time. How can i... (3 Replies)
Discussion started by: jorge.ferreira
3 Replies
Login or Register to Ask a Question