Problem while passing variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem while passing variable
# 1  
Old 11-22-2010
Problem while passing variable

Within a shell im starting a terdata BTEQ session

Code:
#!/bin/ksh
set -x
 
TEMP_DATABASE=`head -6 /home/karthik/para.txt|tail -1`
TEMP_TABLE=`head -7 /home/karthik/para.txt|tail -1`
 
bteq<<EOF
.LOGON TEST/karthik,********;
.EXPORT FILE=test1.txt;
.set heading ''
.set titledashes off; 
.set width 1300;
SELECT COUNT(*) FROM ${TEMP_DATABASE}.${TEMP_TABLE};
.EXPORT RESET;
.QUIT;
.exit;
EOF

When I running this, its gives result as expected. That is my shell variable passing into BTEQ session.

I’m storing my actual into a file SQL.txt

My file contains following SQL

Code:
SELECT COUNT(*) FROM ${TEMP_DATABASE}.${TEMP_TABLE};

Again im rewriting my BTEQ script as follows


Code:
#!/bin/ksh
set -x
 
TEMP_DATABASE=`head -6 /home/karthik/para.txt|tail -1`
TEMP_TABLE=`head -7 /home/karthik/para.txt|tail -1`
 
bteq<<EOF
.LOGON TEST/karthik,********;
.EXPORT FILE=test1.txt;
.set heading ''
.set titledashes off; 
.set width 1300;
.run file /home/karthik/SQL.txt;
.EXPORT RESET;
.QUIT;
.exit;
EOF

Here my shell variable is not passing into my SQL stored in the file. Hence my script is not running

please help me out

Last edited by pludi; 11-22-2010 at 08:01 AM.. Reason: code tags, please...
# 2  
Old 11-22-2010
Shell parameter substitution does not take place in data files.
You will need to process the line in Shell if you want Shell to perform the substitution.
For example:

Code:
#!/bin/ksh
set -x
 
TEMP_DATABASE=`head -6 /home/karthik/para.txt|tail -1`
TEMP_TABLE=`head -7 /home/karthik/para.txt|tail -1`
echo "SELECT COUNT(*) FROM ${TEMP_DATABASE}.${TEMP_TABLE};" >/home/karthik/SQL.txt

There are other alternatives, such as:
1) Read the environment variables with the SQL progam. I don't know how to do this in your SQL but most database languages offer this.
2) Use sed or ed to change the SQL.txt file.
3) Use a SQL program to change the SQL.txt file.
# 3  
Old 11-22-2010
actually i dont want to use my SQL with in the shell....coz my SQL may contains more than 500 lines....so i want to store it in a file and need to run...moreover i dont want to edit the shell(editing SQL in shell) each time when Requirement changes....rather i can change the SQL stored in a file

FYI: im using teradata DB
# 4  
Old 11-22-2010
The reason is that the parsing process in the shell runs in steps: in one steps all the parameters are expanded, in one step all the files are being read, etc. This is why you can have a "here-document" containing variables (which will be expanded to there values), but not a file containing variables.

You have several options, but probably the easiest one to maintain is: you could parse (this is: replace the variables with their content) your file ( /home/karthik/SQL.txt, if I'm correct) yourself prior to reading it. If you have a file with variable definitions (i called it "some_declaration_file") of the form

Code:
var1=value1
var2=value2
var3=value3
...

You could do the following:

Code:
#!/bin/ksh
set -x

TEMP_DATABASE=`head -6 /home/karthik/para.txt|tail -1`
TEMP_TABLE=`head -7 /home/karthik/para.txt|tail -1`

cp /home/karthik/SQL.txt /tmp/parsefile
cat some_declaration_file |\
while read line ; do
     varname=${line%%=*}
     varcontent=${line##*=}

     sed 's/\${'"$varname"'}/'"$varcontent"'/g'  /tmp/parsefile > /tmp/parsefile.tmp
     mv /tmp/parsefile.tmp /tmp/parsefile
done

bteq<<EOF
.LOGON TEST/karthik,********;
.EXPORT FILE=test1.txt;
.set heading ''
.set titledashes off; 
.set width 1300;
.run file /tmp/parsefile;
.EXPORT RESET;
.QUIT;
.exit;
EOF

Actually the "sed"-statement changes "${variable}" to "content_of_variable" if in the declaration file there is a line

Code:
variable=content_of_variable

Instead of reading this declaration file completely line by line (as i have done with the while-loop) you could also limit your replacement to several lines of this declaration file.

I hope this helps.

bakunin

Last edited by bakunin; 11-22-2010 at 09:07 AM..
# 5  
Old 11-22-2010
Code:
 
#!/bin/ksh
set -x
TEMP_DATABASE=`head -6 /home/karthik/para.txt|tail -1`
TEMP_TABLE=`head -7 /home/karthik/para.txt|tail -1`
cp /home/karthik/SQL.txt /home/karthik/parsefile
cat some_declaration_file |\
while read line ; do
varname=${line%%=*}
varcontent=${line##*=}
sed 's/\${'"$varname"'}/'"$varcontent"'/g' /tmp/parsefile > /tmp/parsefile.tmp
mv /tmp/parsefile.tmp /tmp/parsefile
done
bteq<<EOF
.LOGON TEST/karthik,******;
.EXPORT FILE=test1.txt;
.set heading ''
.set titledashes off; 
.set width 1300;
.run file /tmp/parsefile;
.EXPORT RESET;
.QUIT;
.exit;
EOF

sorry for bugging you again

this is not working sir....

even i could not understand your points too as im very new to unix ..

please correct me ..

---------- Post updated at 09:05 AM ---------- Previous update was at 09:01 AM ----------

let me tell what i understood...

1. need to copy the content of SQL.txt into another file ...
2. there i need to replace the parameter content with original value
3. need to use it that for futher SQL processing

---------- Post updated at 12:11 PM ---------- Previous update was at 09:05 AM ----------

thanks sir ....its working fine ....i tried something with ur code(combination urs + my understanding) ..its working fine
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing. one two three four five ten six seven eight Expected output if I have passed one and ten two three four five (8 Replies)
Discussion started by: mychbears
8 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

4. Shell Programming and Scripting

Passing variable with *

Hi Folks, I would like to pass a variable with a wild card in an argument. My script works if I don't use a wildcard but fails when I use *. I want to use the script like: scriptname -F <filename*> @ i = 0 while ($i <= ${#argv}) switch ($argv) case -F: set j = `echo $i +1... (2 Replies)
Discussion started by: dixits
2 Replies

5. Programming

problem passing unix variable to c

hi, i am trying to call a unix script in a c program, this unix script will set a flag, the value of which i would like to return to the calling C program. this is the code that i have written.. the get env functions works if i use other env variables set at session level in unix like... (1 Reply)
Discussion started by: sais
1 Replies

6. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

7. Shell Programming and Scripting

passing a variable inside another variable.

Any help would be great. I know this is a dumb way of doing this, but I would like to know if there is a solution doing it this way. I'm very new at this and I'd like to learn more. Thanks! :D:D count=0 while ; do echo "enter your name" read name_$count let count=count+1 done ... (2 Replies)
Discussion started by: reconflux
2 Replies

8. Shell Programming and Scripting

Problem passing a specific variable to sed

Hi, I'm a bit of sed n00b here. My issue is as follows: I'm trying to pass a variable to sed so that all instances of this variable (in a text file) will be replaced with nothing. However, the value of this variable will always be a folder location e.g. "C:\Program Files\Folder1" I... (5 Replies)
Discussion started by: Mr_Plow
5 Replies

9. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

10. UNIX for Dummies Questions & Answers

Variable passing

Hi, If a script A(Parent) is running and script B(child) is run from script A, will the variables in script A be past to script B? Will the variables exist only for the duration of running the script? Thank you (2 Replies)
Discussion started by: whugo
2 Replies
Login or Register to Ask a Question