Translating variables in text file to their values using bourne shell

Translating variables in text file to their values using bourne shell

Post by Dan-Ake Andersson ETX/XD/E » Tue, 03 Jul 2001 18:35:53



Hi,

I have a bourne shell script in which I
have to read a text file that contains
variabel names that is available to the
script (The variables are as "$variablename"),
the variable names should be replaced with their
value in the output file.
I have a solution that im not pleased with:

echo "more <<EOF
`cat $inputfile`
EOF" | sh > $outputfile

Is it possible to do this in a way that the
variables that I want to replace does not have
to be exported?

Regards,
        Dan-?ke Andersson

 
 
 

Translating variables in text file to their values using bourne shell

Post by Ronald Fische » Tue, 03 Jul 2001 22:45:24



Quote:> I have a bourne shell script in which I
> have to read a text file that contains
> variabel names that is available to the
> script (The variables are as "$variablename"),
> the variable names should be replaced with their
> value in the output file.

And I assume they are set as shell variables in the calling script?

Quote:> Is it possible to do this in a way that the
> variables that I want to replace does not have
> to be exported?

Assuming that the text file comes from stdin, the snippet

while read LINE
do
   eval echo "$LINE"
done

copies it to stdout, with all $XXX replaced by their values (and all
other * side effects coming from 'eval').

Ronald
--
To reduce spam in my inbox, the address given in the Reply-To: header is
not guaranteed to live longer than 1 month after the article was
posted. My permanent address is (after deleting the XXX):


 
 
 

Translating variables in text file to their values using bourne shell

Post by Bill Marcu » Wed, 04 Jul 2001 04:25:32



Quote:>Hi,

>I have a bourne shell script in which I
>have to read a text file that contains
>variabel names that is available to the
>script (The variables are as "$variablename"),
>the variable names should be replaced with their
>value in the output file.
>I have a solution that im not pleased with:

>echo "more <<EOF
>`cat $inputfile`
>EOF" | sh > $outputfile

>Is it possible to do this in a way that the
>variables that I want to replace does not have
>to be exported?

while read line
do
 eval echo $line
done < inputfile > outputfile
 
 
 

Translating variables in text file to their values using bourne shell

Post by Dan-Ake Andersson ETX/XD/E » Fri, 06 Jul 2001 18:05:31


This seemed like the perfect solution:


> while read LINE
> do
>    eval echo "$LINE"
> done

> while read line
> do
>  eval echo $line
> done < inputfile > outputfile

but I then realized that it did not work :(
The problem is that some of the lines in my
text file contains for instance semicolons
that eval removes.

A sample text file looks like this:


SYATI;

I want all variables to be translated, but
the rest of the text should be left as is.
My initial solution did this, but has the
drawback of that all variables had to be
exported.

echo "more <<EOF
`cat $inputfile`
EOF" | sh > $outputfile

Regards,
        Dan-?ke Andersson

 
 
 

Translating variables in text file to their values using bourne shell

Post by Reinhard Skarba » Fri, 06 Jul 2001 20:18:39




...
...

Hi !

A little example. It's not bourne shell but maybe it's similar :

in.txt is your text-file
and fill-in.ksh is a tiny ksh-script :

$ fill-in.ksh in.txt
Changed file : in.txt-new
$

$ cat in.txt
Mr. $LOGNAME is
working on $PWD
$

$ cat in.txt-new
Mr. skarbal is
working on /home/users/skarbal/uebung/report/3huf !
$

$ cat fill-in.ksh
echo "cat <<EOF" >${1}-tmp
cat $1 >>${1}-tmp
echo "EOF" >>${1}-tmp
chmod 755 ${1}-tmp
. ${1}-tmp >${1}-new
rm -f ${1}-tmp
echo "Changed file : ${1}-new"
$

Regards
  Reinhard

 
 
 

Translating variables in text file to their values using bourne shell

Post by Chris F.A. Johnso » Sat, 07 Jul 2001 03:05:44



> This seemed like the perfect solution:

> > while read LINE
> > do
> >    eval echo "$LINE"
> > done

> > while read line
> > do
> >  eval echo $line
> > done < inputfile > outputfile

> but I then realized that it did not work :(
> The problem is that some of the lines in my
> text file contains for instance semicolons
> that eval removes.

> A sample text file looks like this:

> emu:set-iog-dir $DIR;


> SYATI;

> I want all variables to be translated, but
> the rest of the text should be left as is.
> My initial solution did this, but has the
> drawback of that all variables had to be
> exported.

> echo "more <<EOF
> `cat $inputfile`
> EOF" | sh > $outputfile

How about:

while read line
do
  eval echo \"$line\"
done < inputfile > outputfile

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. Better Way to Get Value of Value of Variable in Bourne shell?

Hi

I need to create a Bourne shell function that can change/manipulate the values
of the given variable names, e.g.

  $ P1=John
  $ P2=Jane
  $ P3=Paul

  $ echo "1=$P1, 2=$P2, 3=$P3."
  1=John, 2=Jane, 3=Paul.

  $ add_prefix Hi_ P1 P2 P3

  $ echo "1=$P1, 2=$P2, 3=$P3."
  1=Hi_John, 2=Hi_Jane, 3=Hi_Paul.

After several trial-and-error, I finally got the add_prefix() working, but I
think my solution is too complicated and inefficient, specifically the
"eval $var='$prefix`eval echo \\$$var`'" that takes 2 evals and 1 command
substitution, as shown below.

  add_prefix () {
    prefix=$1
    shift
    for var in $*; do
      eval $var='$prefix`eval echo \\$$var`'
    done
  }

Can any Bourne shell programming guru show me a more concise, efficient and
elegant way in writing the add_prefix() function without using any external
command?

Is there any shorter syntax like ${$var} in Bourne shell that can replace
`eval echo \\$$var` ?  Something concise and simpler will be helpful because
the actual function I need to write is much more complicated than add_prefix()
function shown above.

Thanks.  I appreciate your help.

Regards
Hon-Chi

2. apps - finding & installing

3. getting environment variables from c shell using Bourne shell

4. IDE wrong lock

5. How do you set a variable's initial value in Bourne or tcsh shells?

6. can't connect with dip

7. Assigning an exit value to a Bourne shell variable?

8. NEC Superscript 870 Printer

9. bourne shell variable within a variable

10. modifying a bourne shell variable using sed: ain't workin'

11. Using Variables For Pattern In Bourne Shell case Statement

12. Looping through a text file in bourne shell?

13. c shell script; Howto use environement variables to build environment variables and take their value