Passing value of variable to a query within shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing value of variable to a query within shell script
# 1  
Old 10-22-2013
Passing value of variable to a query within shell script

I have a script in which i connect to database to run a query and get the result of the query to a temp file. This works fine , now what i want is there is flat file which contains the value to be used in the query. I want to read this file line by line and then run the query for each value in that file.

Example

Lets say Flat file(abc.txt) contains employee number
713
845
81

I need to run this query within a script

select * from emptab where emp_no="value from the file"

The present script runs fine if i pass value like this

Code:
eusql<< EOF
select * from emptab where emp_no in ('713','845','81')
go > tmp.txt
EOF

But i cant edit the code always so i would like to have this edited within a flat file and use the same in the script.

I tried with this but it didn't work

Code:
While read val
do
eusql<< EOF
select * from emptab where emp_no="$val"
go > tmp.txt
EOF
done < abc.txt

Thanks in advance.
# 2  
Old 10-22-2013
What is $val ? Bits of it may need escaping.

Why is while capitalized?

Was your code intended at all when you tried it?
# 3  
Old 10-22-2013
Code:
for emp_no in $(<file)
do
  echo "select * from emptab where emp_no=${emp_no}"
done > query.sql

Emanuel
# 4  
Old 10-22-2013
You don't need a for-loop for that, better to use a while.

Code:
while read emp_no
do
        echo "select * from emptab where emp_no=${emp_no}"
done < inputfile > query.sql

# 5  
Old 10-22-2013
Quote:
Originally Posted by Corona688
You don't need a for-loop for that, better to use a while.
...
Is there a good reason for that?

Emanuele
# 6  
Old 10-22-2013
Yes. 'for' splits across whitespace, read splits across newlines, a subtle but important difference.

Also, 'while' is more efficient because it reads a line at a time instead of cramming the entire arbitrary sized file into memory at once.
# 7  
Old 10-22-2013
Ok, thank you.

Emanuele
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass Variable from shell script to select query for SqlPlus?

echo "set echo off"; echo "set feedback off"; echo "set linesize 4000"; echo " set pagesize 0"; echo " set sqlprompt ''"; echo " set trimspool on"; Select statement is mentioned below echo "select res.ti_book_no from disney_ticket_history res where res.ti_status =${STATUS} and... (7 Replies)
Discussion started by: aroragaurav.84
7 Replies

2. Shell Programming and Scripting

Passing shell variable to awk script

I want to pass a shell variable to awk script : # cat file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO 92000 4087.5625 4 96 #... (8 Replies)
Discussion started by: Reboot
8 Replies

3. Shell Programming and Scripting

Issue with passing variable to Grep in a shell script

Hi, I'm trying to check if methods specified in a class have been added to the corrosponding interface. My code below is giving me the following errors: grep: function: No such file or directory grep: import($zipfile): No such file or directory grep: function: No such file or... (1 Reply)
Discussion started by: racshot65
1 Replies

4. Shell Programming and Scripting

Using a shell script variable in a mysql query using 'LIKE'

Hello. I am writing a simple script that reads a text file and removes records from a mysql database. The items in the text file are of the format: firstname.middle.lastXXX, where XXX is a 3 digit number. The table has an email field that will match the firstname.middle.last. So, I thought I... (1 Reply)
Discussion started by: bricoleur
1 Replies

5. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

6. UNIX for Advanced & Expert Users

Passing Hash variable in to sql query in perl

Hi Everyone, Can anyone help me how do i call hash variable in to sql query in perl. Please see the script below i have defined two Hash %lc and %tab as below $lc{'REFF'}='V_RES_CLASS'; $lc{'CALE'}='V_CAP_CLASS'; $lc{'XRPD'}='V_XFMR_CLASS'; $tab{'V_RES_CLASS'}='V_MFR_SERS';... (6 Replies)
Discussion started by: jam_prasanna
6 Replies

7. Shell Programming and Scripting

Passing a variable from shell script to mysql query?

I heard this was possible but from my research I haven't been able to figure it out yet. Seems it should be simple enough. Basically from a high level view I'm trying to accomplish... . $X='grep foo blah.log' then 'mysql command SELECT foo FROM bar WHERE ' . $X or something like that. ... (2 Replies)
Discussion started by: kero
2 Replies

8. Shell Programming and Scripting

error in passing a variable to sqlplus from a shell script

hi, I am using a shell script from where i will be conecting to sqlplus.. i am having a problem in passing a variable to sqlplus query.. i will be assigning the variable in the unix environment..whenever i am trying to pass a variable having the contents greater than 2500 characters, i am... (3 Replies)
Discussion started by: kripssmart
3 Replies

9. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies

10. Shell Programming and Scripting

passing awk variable to the shell script

hi; i have a file containing lines like: 1|1069108123|96393669788|00963215755711|2|0|941||;serv:Pps6aSyria;first:0;bear i want to extract the second, third and fourth record of each line and store it in a file ";" seperated this is what i wrote while read line do ... (3 Replies)
Discussion started by: bcheaib
3 Replies
Login or Register to Ask a Question