• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1441
  • Last Modified:

Using Variable in Linux Bash for MySQL Query

I am having trouble with using a variable in a Linux Bash:

#!/bin/bash
#
UPDATED=`20081029144522`
mysql --user=myuser --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';"



The command work and runs the query, without the variables but when I add the variable I get:
testbash.sh: line 1: 20081029144522: command not found

I can echo the variable and it display on screen but when I place it inside the command it doea not work.

What am I doing wrong?
0
SaltyDawg
Asked:
SaltyDawg
  • 8
  • 6
  • 5
  • +1
1 Solution
 
Deepak KosarajuDevOps EngineerCommented:
UPDATED=`20081029144522`
following the right way of declaration use double quotes
UPDATED="20081029144522"
gud luck
0
 
omarfaridCommented:
why do you need the single quotes around ${UPDATED}
0
 
Deepak KosarajuDevOps EngineerCommented:
UPDATED=`20081029144522`
bash treats any declaration between ` ` as shell command so it says
testbash.sh: line 1: 20081029144522: command not found
use " " for any variable declaration in bash.
final Code is
#!/bin/bash
#
UPDATED="20081029144522"
mysql --user=myuser --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
omarfaridCommented:
why do do put ` around the number?
0
 
SaltyDawgAuthor Commented:
kosarajudeepak:

I used the double quotes there and it did work, but I have other places with variables and it did not work. Are variables declared differently depending on their use or placement?
0
 
SaltyDawgAuthor Commented:
When I don't use the quotes it fails. Its a mysql database field record. That field is actually a TIMESTAMP...
0
 
omarfaridCommented:
can you show cases where it fails?
0
 
SaltyDawgAuthor Commented:
what if i wanted to do this:

#!/bin/bash
#
MYUSER = "myuser"
UPDATED="20081029144522"
mysql --user=${MYUSER} --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';


that's not working for me, it fails

Does it work differently within a command line statement? Because each instance in a statement like this it fails
0
 
Deepak KosarajuDevOps EngineerCommented:
Variable declaration on its use depends on way you are trying to inherit them.
For example: If I would like to declare a variable where its value is equal to the output of a command then I define as
PROCESSID=`ps -ef | grep crond | grep -v grep |  awk {'print $(2)'}`
kill -9 $PROCESSID

Other way is contant variable declaration like a text variable
PROCESSNAME="crond"
ps -ef | grep $PROCESSNAME | grep -v grep |  awk {'print $(2)'}

I think its clear now...


0
 
Deepak KosarajuDevOps EngineerCommented:
Don't give space when you are declaring variable.
Final code is
#!/bin/bash
#
MYUSER="myuser"
UPDATED="20081029144522"
mysql --user=$MYUSER --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';

Open in new window

0
 
Deepak KosarajuDevOps EngineerCommented:
Close the select statement with " i missed in the code snippet.
0
 
omarfaridCommented:
don't you single quotes
0
 
SaltyDawgAuthor Commented:
The select statements works when I use

UPDATED=20081029144522
mysql --user=myuser --password=ebtron EbtronSystems -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='$UPDATED';"

I explicitly wrote in the user=myuser and inserted a variable into WHERE Updated='$UPDATED';" and it works. But I also need to be able to put a variable into user=$MYUSER
0
 
omarfaridCommented:
it should work!
0
 
Deepak KosarajuDevOps EngineerCommented:
I attached the working copy of script in my previous post, did u try that.
0
 
Deepak KosarajuDevOps EngineerCommented:

#!/bin/bash
#
MYUSER="myuser"
UPDATED="20081029144522"
mysql --user=$MYUSER --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';"

Open in new window

0
 
SaltyDawgAuthor Commented:
kosarajudeepak:

for some reason that did not work

it still does not like --user=$MYUSER
0
 
Deepak KosarajuDevOps EngineerCommented:
Thats strange here is the demo I tried.
[root@pro02 ~]# cat mysql.sh 
MYUSER="root"
mysql --user=$MYUSER -p -e "show databases;"
[root@pro02 ~]# sh mysql.sh 
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| nagios             | 
| test               | 
+--------------------+
[root@pro02 ~]# 

Open in new window

0
 
ozoCommented:
Did you remove the spaces from
MYUSER = "myuser"
0
 
SaltyDawgAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 8
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now