Sidd_Terp Sidd_Terp - 3 years ago 236
Linux Question

Passing variables from shell script to BTEQ File

I have a shell script(.ksh) inside which I call the BTEQ file to execute SQL query. However, the date of executing the BTEQ file depends on the data availability which another SQL query inside the shell script checks.

What I want: I want to pass 'Date' variable from shell script into the BTEQ file so that the query that executes takes that 'date' into account.

Shell Script and the variable inside the check_data.ksh

Rundate=`date +"%Y-%m-%d"`
cat ~/.tdlogon_simba sample_TD.btq | bteq >> ${log_file} 2>&1

Bteq file code (Sample_TD.btq)

sel * from test_table
where cond = Rundate

In the above code, I want 'Rundate' to come from check_data.ksh

What I don't want: In reality, my query is very big around 3000 line of code so I don't want to execute the query inside the shell script.

I'm really looking for options to pass the variable from shell script to btq file.

Any help would be greatly appreciated.

Answer Source

You can simply update your script to change the keyword Rundate in your .btq file using sed :

sed "s/Rundate/$(date +'%Y-%m-%d')/g" ~/.tdlogon_simba sample_TD.btq | bteq >> ${log_file} 2>&1

The sed command will change all occurrences of the keyword Rundate. You might want to refine this by having a more restrictive search for example:

sed "s/cond = Rundate/cond = $(date +'%Y-%m-%d')/g"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download