Domanda

I have a list of file names with environment variables in them. I'd like to read the file line by line and then set a variable to the read in line however have the envirnment variable translated to the appropriate environment variable. Here is my script so far:

#!/bin/ksh

. /test/currentEnv.sh

while read line
do
        echo $line
done < $1

if my source file is:

foo1$ENVVAR1.csv 
foo2$ENVVAR2.csv
foo3$ENVVAR3.csv

and my Environment variables in currentEnv.sh are

$ENVVAR1=hello;     export ENVVAR1     
$ENVVAR2=world;     export ENVVAR2
$ENVVAR3=test;      export ENVVAR3

I'd like the results of the script to be

foo1hello.csv
foo2world.csv
foo3test.csv

currently it just dumps out the original file:

foo1$ENVVAR1.csv 
foo2$ENVVAR2.csv
foo3$ENVVAR3.csv

Edit

I was able to get the majority of my files resolved using: #!/bin/ksh

. /test/currentEnv.sh

while read line
do
        eval echo $line
done < $1

however some of my variables are in the middle of string like:

foo3$ENVVAR3_bar.csv

this seems to look for an env variable $ENVVAR3_bar and doesn't find it I need this to output:

foo3test_bar.csv
È stato utile?

Soluzione

  1. You declare a variable without the dollar sign:

    $var=value     # no
    var=value      # yes
    
  2. Since underscore is a valid character for a variable name, ksh is trying to expand the variable named ENVVAR3_bar: you need to use braces to separate the variable name from the surrounding text:

    foo3$ENVVAR3_bar.csv      # no
    foo3${ENVVAR3}_bar.csv    # yes
    
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top