Passing a variable from shell script to mysql query?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a variable from shell script to mysql query?
# 1  
Old 06-10-2009
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...

Code:
. $X='grep foo blah.log'
then 'mysql command SELECT foo FROM bar WHERE ' . $X

or something like that.

This is part of my script:

Code:
cid=$(awk '{ print $2 }' /home/test.txt | sed -n 2p)
echo "cid is" $cid
cat /home/my2.sql | /home/build/env/util/connectdb.sh biller > /home/test2.txt
cat /home/test2.txt

In the my2.sql file:

Code:
select * from Notifications join Events using (EventId) where CustomerId = '&cid';

Any suggestions? Possible? If so what am I doing wrong?

Thanks

David
# 2  
Old 06-10-2009
what shell are you using??
Code:
cid=`awk 'NR==2{print $2}' /home/test.txt `
echo "cid is" $cid

rest that cat and piped to dbconnect i didn't understand

and if cid is variable use $cid not &cid
Code:
select * from Notifications join Events using (EventId) where CustomerId = '$cid';

# 3  
Old 06-10-2009
I'm using Bash.

For the code: cat /home/my2.sql | /home/build/env/util/connectdb.sh biller > /home/test2.txt

I'm passing the query after connecting to the database and then passing the result to the test2.txt file. Not sure if this is the correct way.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable definition inside mysql query

Hello, I am running ubuntu16.04. By means of @Rudic's help, I have below command. What I need to do is to replace video_id by value of video_id in which WHERE clause is matched: {print "INSERT INTO video_series_files (id, video_id, file_type, protocol, \ url, languages, quality, accessed... (12 Replies)
Discussion started by: baris35
12 Replies

2. Shell Programming and Scripting

Shell script automation using cron which query's MySQL Tables

What I have: I have a input.sh (script which basically connect to mysql-db and query's multiple tables to write back the output to output1.out file in a directory) note: I need to pass an integer (unique_id = anything b/w 1- 1000) next to the script everytime I run the script which generates... (3 Replies)
Discussion started by: kkpand
3 Replies

3. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: gpk_newbie
7 Replies

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

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