Escaping a variable in ksh

I am trying to echo a variable exactly to a script-

echo "${var1} ${var2} >> output.output

Gives me a blank file.

I would like output.output to basically say:

${var1} ${var2}

I think I need to use a special escape character for variables. Am I right in assuming that, and is it the best approach?

couple of problems:

  1. you're missing a closing double-quote for your echo to work
  2. if you want a literal string ${var1} ${var2} , use SINGLE-quotes for the echo (instead of DOUBLE-QUOTES)
echo "\${var1} \${var2}"

Ooops just saw your post vgersh99...

1 Like

This worked great. One concern.

I am trying to echo a query that gets passed to my command:

Heres what I am getting

-i "where doc_name like \'%\'

Heres what I need

-i "where doc_name like '${variable}%'

Please be VERY careful with your quotes (single / double)! You seem to be missing one again in above!

Do I get you right that you want '${variable}' literally written to your file, but then expanded when executing that file? Might run into trouble with all those single quotes in place. Please post a sample of the desired output file (and its possible execution log).

Nope.

Here's what I am running-

while IFS=, read agname tablename
do
echo "while read IFS=, read docname agname" >> $BASEDIR/scripts/${agname}_retrieval.ksh
echo "do" >> $BASEDIR/scripts/${agname}_retrieval.ksh
echo "get -G \${agname} -i \"where doc_name like \'${docname}%\'" -o \${agname}" >> $BASEDIR/scripts/${agname}_retrieval.ksh
echo "done" < $BASEDIR/data/${agname}/${agname}_docnames.del" >> $BASEDIR/scripts/${agname}_retrieval.ksh
done < $BASEDIR/output/$STEP1_OUTPUT

The final script is generating but..

while read IFS=, read docname agname
do
get -acgNv -G ${agname} -i "where doc_name like \'%\' -o ${agname} >> /tmp/final/scripts/AG_retrieval.ksh
echo done < /tmp/final/data/AG/AG_docnames.del

Not sure why it's putting extra echos in the output

Desired-

while read IFS=, read docname agname
do
get -acgNv -G ${agname} -i "where doc_name like '${docname}%' -o ${agname}
 done < /tmp/final/data/AG/AG_docnames.del

See post #5, line 1.

EDIT: How far would this get you (zeroth approximation):

while IFS=, read agname tablename
  do    exec  > $BASEDIR/${agname}_retrieval.ksh
        echo "while read IFS=, read docname agname"
        echo "do"
        echo "get -G \${agname} -i \"where doc_name like '\${docname}%'\" -o \${agname}"
        echo "done < $BASEDIR/data/${agname}/${agname}_docnames.del"
        exec 1>&2
   done < $BASEDIR/output/$STEP1_OUTPUT

EDIT: or (first approximation)

while IFS=, read agname tablename
  do    cat <<-EOTXT  > $BASEDIR/${agname}_retrieval.ksh
        while read IFS=, read docname agname
        do
        get -G \${agname} -i "where doc_name like '\${docname}%'" -o \${agname}
        done < $BASEDIR/data/${agname}/${agname}_docnames.del
        EOTXT
      done < $BASEDIR/output/$STEP1_OUTPUT

Your first approximation worked!

Thank you!

Staring at (and comparing) the resultant scripts again, the $tablename is not used at all, and the actual $agname value is used for the input file name only. Why all that effort and complexity if you could have one single constant(!) script and call it with a parameter indicating the input file?
Like

$ /tmp/final/scripts/ALL_retrieval.ksh  /tmp/final/data/AG1/AG1_docnames.del
$ /tmp/final/scripts/ALL_retrieval.ksh  /tmp/final/data/AG2/AG2_docnames.del
$ /tmp/final/scripts/ALL_retrieval.ksh  /tmp/final/data/AG3/AG3_docnames.del

, and in /tmp/final/scripts/ALL_retrieval.ksh you read your data from $1 .