SaltyDawg
asked on
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?
#!/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?
why do you need the single quotes around ${UPDATED}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
why do do put ` around the number?
ASKER
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?
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?
ASKER
When I don't use the quotes it fails. Its a mysql database field record. That field is actually a TIMESTAMP...
can you show cases where it fails?
ASKER
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
#!/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
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...
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...
Don't give space when you are declaring variable.
Final code is
Final code is
#!/bin/bash
#
MYUSER="myuser"
UPDATED="20081029144522"
mysql --user=$MYUSER --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';
Close the select statement with " i missed in the code snippet.
don't you single quotes
ASKER
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
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
it should work!
I attached the working copy of script in my previous post, did u try that.
#!/bin/bash
#
MYUSER="myuser"
UPDATED="20081029144522"
mysql --user=$MYUSER --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';"
ASKER
kosarajudeepak:
for some reason that did not work
it still does not like --user=$MYUSER
for some reason that did not work
it still does not like --user=$MYUSER
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 ~]#
Did you remove the spaces from
MYUSER = "myuser"
MYUSER = "myuser"
ASKER
Thanks
following the right way of declaration use double quotes
UPDATED="20081029144522"
gud luck