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?
LVL 1
SaltyDawgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak KosarajuDevOps EngineerCommented:
UPDATED=`20081029144522`
following the right way of declaration use double quotes
UPDATED="20081029144522"
gud luck
omarfaridCommented:
why do you need the single quotes around ${UPDATED}
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

omarfaridCommented:
why do do put ` around the number?
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?
SaltyDawgAuthor Commented:
When I don't use the quotes it fails. Its a mysql database field record. That field is actually a TIMESTAMP...
omarfaridCommented:
can you show cases where it fails?
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
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...


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

Deepak KosarajuDevOps EngineerCommented:
Close the select statement with " i missed in the code snippet.
omarfaridCommented:
don't you single quotes
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
omarfaridCommented:
it should work!
Deepak KosarajuDevOps EngineerCommented:
I attached the working copy of script in my previous post, did u try that.
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

SaltyDawgAuthor Commented:
kosarajudeepak:

for some reason that did not work

it still does not like --user=$MYUSER
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

ozoCommented:
Did you remove the spaces from
MYUSER = "myuser"
SaltyDawgAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.