A better solution to eval?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A better solution to eval?
# 1  
Old 05-24-2013
A better solution to eval?

Hi,

I am trying to replace some variables in an sql file, but I seem to be having some trouble during the loop.

Code:
sql_file.sql

SELECT *
FROM  DATABASE.TABLE
WHERE EVENT_DT < ${TD_EFFECTIVE_DATE}

My script looks like this

Code:
#!/bin/ksh

SQL_FILE=$1


#testing

TD_EFFECTIVE_DATE="20130524"

test_var="WHERE EVENT_DT <= ${TD_EFFECTIVE_DATE}"

eval echo "\${test_var}"

#doesn't quite work?

cat ${SQL_FILE} | while read SQL_LINE
do
        if [[ -n `echo ${SQL_LINE} | grep '${'` ]]
then
                eval echo "\${SQL_LINE}"
        fi
done

Code:
Output:

WHERE EVENT_DT <= 20130524
WHERE EVENT_DT <= ${TD_EFFECTIVE_DATE}

Also, is this the best way to replace these variables? I will have multiple difference variables in a single file.

Thanks for your help in advance.
# 2  
Old 05-24-2013
I would suggest, have some unusual indicator in sql file and replace that with the actual value with-in the script.
Something like..


Code:
SELECT *
FROM  DATABASE.TABLE
WHERE EVENT_DT <  #~#

And later replace #~# with date in script.
# 3  
Old 05-24-2013
You could also change your SQL file into this:

Code:
cat <<EOF

SELECT *
FROM  DATABASE.TABLE
WHERE EVENT_DT < ${TD_EFFECTIVE_DATE}

EOF

... and then you could use your SQL file in shell like . /path/to/file.sql and it would print the substituted text.
# 4  
Old 05-24-2013
The eval on the whole line also interprets <= as shell command...
Is it possible to have the "skeleton" directly in the script?
E.g. as a here document?
The variable substitutions will happen automatically.
Code:
#!/bin/ksh

TD_EFFECTIVE_DATE="20130524"

while read SQL_LINE
do
       echo "$SQL_LINE"
done << _EOT
SELECT *
FROM  DATABASE.TABLE
WHERE EVENT_DT < ${TD_EFFECTIVE_DATE}
_EOT

Of course you can also do
Code:
sqlcommand << _EOT
...
_EOT

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Eval

thank you (35 Replies)
Discussion started by: ratnalein88
35 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

5. Shell Programming and Scripting

eval

hi all, Am trying to add some code to a ksh script and i dont understand how an eval function is used : _var=$1 _conceal=$2 eval _val=\$${_var} can someone shed some light on what the eval function in the above context means/does ?? thanks. (4 Replies)
Discussion started by: cesarNZ
4 Replies

6. Shell Programming and Scripting

EVal

Hi All, I'm running some encrypted data through a script I wrote. In order to do this, I'm using eval to resolve some of my variables. At the moment, when I use eval to resolve, it strips out some of my encrypted values, and totally drops some others. For example if I have the value ab1"3 it drops... (1 Reply)
Discussion started by: Khoomfire
1 Replies
Login or Register to Ask a Question