Solved

How to pass shell variable into "mysql -e" querry

Posted on 2008-10-10
4
1,560 Views
Last Modified: 2012-08-13
Hi,

I need to pass the shell variable into mysql -e query in bash shell command line

$ export ID=0
$ mysql -e 'select * from user where id=$ID'
ERROR 1054 (42S22) at line 1: Unknown column '$ID' in 'where clause'
0
Comment
Question by:wesly_chen
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22693113
this is, AFAIK, not possible.
you will have to build a text file with the query, and use that file as input for running the query.
0
 
LVL 38

Author Comment

by:wesly_chen
ID: 22693157
> build a text file with the query, and use that file as input for running the query.
How?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 22693352
that would be like this:
echo "select * from user where id=$ID;" > file.sql

echo "exit;" >> file.sql

mysql < file.sql

rm file.sql

Open in new window

0
 
LVL 38

Author Comment

by:wesly_chen
ID: 22693448
> echo "exit;" >> file.sql
it complains about syntax error. I take out ";" and everything is ok now.

Thanks a lot.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now