LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-17-2012, 10:10 AM   #1
WindozBytes
Member
 
Registered: Aug 2001
Location: CT, USA
Distribution: Mandriva 2008
Posts: 105

Rep: Reputation: 15
ksh read variable from text file and populate with content of shell variable


The value of VAR is set to abc in script 1 and exported to script 2.
Script 2 reads key-value pairs from a text file, one line at a time.
One of the values in the text file is $VAR. I've tried using sed to get the value of the $VAR shell variable (abc in this case) to be substituted for the text literal $VAR that is read from the file, but that isn't working for me. Is there a way to do this?

#!/usr/bin/ksh

VAR="abc"

#Concatenate key-value pairs from text file prefixing each key with the -set command.
if [ -e parm.txt ] ; then
i=0
while read value ; do
i=$((i+1))
PARM=" -set ""$value""$PARM"
done < parm.txt
fi

#The values in parm.txt:
# key1 value1
# key2 value2
# key3 $VAR
# ...
# key(n) value(n)
 
Old 09-17-2012, 11:02 AM   #2
tc_
LQ Newbie
 
Registered: Sep 2010
Location: Germany
Distribution: Slackware
Posts: 27

Rep: Reputation: 30
I think, sed does the job fairly well.

Consider the example
Code:
#!/bin/sh
VAR="abc"
echo "key1 hallo\nkey2 \$VAR\nkey3 world" | sed -e "s/\$VAR/${VAR}/"
The \$VAR gets replaced by abc. Or, using files, as you did:
Code:
}
VAR="abc"
cat test | sed -e "s/\$VAR/${VAR}/"
outputs

Code:
key1 blah
key2 abc
key2 blub
when run on the file

Code:
key1 blah
key2 $VAR
key2 blub
One has to use \$ in the sed expression, for otherwise, sed tries to match end-of-line.

Last edited by tc_; 09-17-2012 at 11:07 AM. Reason: removed stupid error
 
Old 09-17-2012, 11:26 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 2,866

Rep: Reputation: 698Reputation: 698Reputation: 698Reputation: 698Reputation: 698Reputation: 698
It doesn't look like you're opening the file.

In KornShell, you open a file for reading or writing like this:
Code:
exec 3< filename       # open filename for reading
exec 4> filename       # open filename for writing
You want to use unit numbers greater than 2 (because 0, 1, and 2 are standard input, output and error channels automatically opened by the shell).

So, to read from a file
Code:
exec 3< filename
while read -u 3 variable
do
     whatever
done
#       close the input file
exec 3<&-
There are built-in substring functions:
Code:
${parameter#pattern}     # remove small left pattern
${parameter##pattern}    # remove large left pattern
${parameter%pattern}     # remove small right pattern
${parameter%%pattern}    # remove large right pattern
Here are some examples:
Code:
# remove small left pattern
cd ${HOME}/src/cmd
print ${PWD#$HOME}
src/cmd

# remove large left pattern
x=/one/two/three
print ${x##*/}
three

# remove small right pattern
x=file.c
print ${x%.c}.o
file.o

# remove large right pattern
x=foo/fun/bar
print ${x%%/*}
foo
Probably a little easier than fiddling with sed.

It might be helpful to try reading your file with the above and just simply print what you get; for example, to read multiple values from a file (separated by white space):
Code:
exec 3< parm.txt
while read -u 3 a b c
do
	print $a $b $c
done
exec 3<&-
That would print the "# keyn valuen".

Also not that if you're executing two shell programs where the first one places a value in the environment you need to execute the first with the "dot space;" e.g.,
Code:
. prog1
<wait a while>
prog2
If you don't, the value place in the environment by prog1 is lost.

Hope this helps some.

Last edited by tronayne; 09-17-2012 at 11:28 AM.
 
Old 09-17-2012, 12:48 PM   #4
WindozBytes
Member
 
Registered: Aug 2001
Location: CT, USA
Distribution: Mandriva 2008
Posts: 105

Original Poster
Rep: Reputation: 15
Thanks for the response tc_. Your sed solution was remarkably similar to mine, but I had neglected to escape the $ at the start of my variable name.
 
Old 09-17-2012, 01:48 PM   #5
tc_
LQ Newbie
 
Registered: Sep 2010
Location: Germany
Distribution: Slackware
Posts: 27

Rep: Reputation: 30
Quote:
Originally Posted by WindozBytes View Post
Thanks for the response tc_. Your sed solution was remarkably similar to mine, but I had neglected to escape the $ at the start of my variable name.
Well, my solution was just fixing yours. Anyway, you're welcome.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 01:38 PM
[bash] Read file's content into variable problem Coolmax Programming 6 08-31-2009 04:12 PM
ksh: while read doesn't get to read more than one variable eantoranz Linux - Software 1 07-24-2009 03:33 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
how to send shell variable data to a text file ginda Programming 7 06-23-2006 05:49 AM


All times are GMT -5. The time now is 11:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration