Solved

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

Posted on 2008-10-10
4
1,575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

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 143

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

710 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