Solved

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

Posted on 2008-10-10
4
1,568 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

740 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