1

I am creating a shell script that retrieves values from the database and spools into a text file. Those values will have variables ($CURR_DATE $SITE etc...) in the database. So when I want to execute the program with those variables I run into an issue where it is using the literal string and not the value from the variable.

for example.

while read line;
      do
         Unix_Array[$counter]=$line;
         let counter=counter+1;
      done < parameterfile.txt
      echo "Finished putting into array"
      while [[ $c -lt ${#Unix_Array[@]} ]]
         do
            PARAMS="${PARAMS:-}${PARAMS:+ }${Unix_Array[$c]}"
            ((c=$c+1))
         done
echo "Finished creating parameter string"


EXECUTE="$PROGRAM $USERID $PARAMS"
echo $PARAMS
$EXECUTE

I think it is executing like

Program user/id@DB $CURR_DATE $SITE

instead of the actual variables that were declared and already set.

How can i build the execution statement so that it will use the variables declared and not the literal variable.

1 Answer 1

2

Once you've collected the array, use it directly:

typeset -a params
while IFS= read -r line; do
    params[n++]=$line; 
done < parameterfile.txt

"$program" "$userid" "${params[@]}"

As to the lines containing variables, I'd hesitantly recommend using eval. What does the parameter file look like? Who has permission to write to it?

Get out of the habit of using ALL_CAPS_VARS: one day you'll use PATH or LANG and wonder why things "don't work".

3
  • the parameter file is like param1 $param2 param3 $param4 1 US so its 1 column from the database each parameter is separated by new line so it looks like just 1 column of parameters.
    – Alkey29
    Mar 26, 2014 at 20:45
  • I added eval before the $EXECUTE and it executed correctly. What is the issue using eval?
    – Alkey29
    Mar 26, 2014 at 21:36
  • @Alkey29 eval is unsafe if you feed it input that you have no control over (e.g. """data""" from a file that users have write access to or parameters from the command line), you don't want to execute arbitrary code and in most cases it can be avoided. Mar 28, 2014 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.