Solved

ksh variable expansion when reading from an external file.

Posted on 2004-04-20
3
716 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now