Solved

ksh variable expansion when reading from an external file.

Posted on 2004-04-20
3
724 Views
Last Modified: 2013-12-27
Hi,

   I think this is an easy question!  I've got a text file where I store information that I want to read into a ksh script.  For example, the text file has a line:
abcdef|-p ${NAME}

   In my ksh script, I have defined the NAME variable (NAME="temp") but when I parse the line from the text file as follows (line has been read into variable REC):
TEXT=$(echo $REC|awk '-F|' '{print $2}')
the resulting TEXT variable contains:
-p ${NAME}
instead of:
-p temp

  Is there a way to get this variable expansion to work properly?

Thanks

0
Comment
Question by:KAG99
  • 2
3 Comments
 
LVL 7

Expert Comment

by:fim32
ID: 10870235
hmm, i don't think ksh works that way...  unlike perl, there are no advanced q// options...
perhaps someone can correct me if i'm wrong?

the easiest way to do this, is to set your vars in your text file to some distinctive character string, like %%NAME%% (i've seen this one used)

and then after you've awked, sed the replacement:
TEXT=$(echo $REC|awk '-F|' '{print $2}'|sed -e "s/%%NAME%%/$NAME/")
0
 
LVL 3

Accepted Solution

by:
dtkerns earned 50 total points
ID: 10870973
you need to use eval to get ksh to do double substitution ... and an echo since eval needs to do something with the expresion

TEXT=`eval echo $(echo $REC|awk '-F|' '{print $2}')`
0
 
LVL 7

Expert Comment

by:fim32
ID: 10873119
ah, that pesky eval :P
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question