How to evaluate text strings provided by sed/grep/whatever?

Posted by T.J. on Stack Overflow See other posts from Stack Overflow or by T.J.
Published on 2010-12-22T23:49:58Z Indexed on 2010/12/22 23:54 UTC
Read the original article Hit count: 192

Filed under:
|
|
|
|

This is for UNIX shell programming. It have to be supported by multiple UNIX platforms including Solaris, Linux, and AIX.

I have this scenario -- I am to read from a text file a string that may or may not contain an environment variable that may or may not be defined. For example:

<foo.bar> This error code was found: $(error_code)

I have the following code:

statement=$(sed -n $1'p' $messagefile)
echo $echo_flag $statement

$1 = line number supplied to this particular function/script.
$messagefile = filename of log file.
$echo_flag = "-e" in Linux, otherwise, empty.
$(error_code) = 42.

Instead of getting this when running:

<foo.bar> This error code was found: 42

I still get this:

<foo.bar> This error code was found: $(error_code)

How exactly do I tell the shell script that the value of statement should be evaluated further beyond what sed have done?

© Stack Overflow or respective owner

Related posts about string

Related posts about unix