LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-03-2008, 02:56 PM   #1
ThaSaltyDawg
LQ Newbie
 
Registered: Nov 2008
Location: Myrtle Beach, SC
Posts: 7

Rep: Reputation: 0
Question Using Variable in Linux Bash for MySQL Query


Hello All
I am an experienced programmer but very unexperienced using Linux. But I'm working on something requiring me to use Linux. I have this project that has to be done like last week. So I'd appreciate any help.

I am writing a bash to do the following:
# -- APPEND NEW ORDERSUMMARY UPDATE INFO
# -- CHECK IF ANY RECORDS WERE ADDED OR UPDATED SINCE LAST RAN
# -- CREATE MYSQLDUMP-REPLACE FILE
# -- SECURE COPY FILE TO WEB
# -- ARCHIVE MYSQLDUMP-REPLACE FILE
# -- APPEND TO LOG FILE

Now I managed to get the bash "working" (when not using variables) but when I insert variables if does not work. "Right now" (until I run into something else) this variable issue is the only thing holding me up.

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 works 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 command line but when I place it inside the command it does not work.

Are there different ways to declare and insert variables?

What am I doing wrong?

And Thanks for the help
 
Old 11-03-2008, 03:11 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
#!/bin/bash
#
UPDATED=`20081029144522`
mysql --user=myuser --password=mypass MyDB -e "SELECT Updated FROM OrderSummary_LastUpdate WHERE Updated='${UPDATED}';"
Well... you're using back ticks to assign the value to a variable. This is permitted, but it stats for command substitution, which means "assign the output of the command inside back ticks to the variable". This is the reason of the command not found error: the shell tries to execute the command 20081029144522, which is obviously not found.

You can do the assignment using one of the following:
Code:
UPDATED=20081029144522        # no quotes
UPDATED="20081029144522"      # double quotes
UPDATED='20081029144522'      # single quotes
For the meaning of the different types of quotes, have a look at chapter 5 of the Advanced Bash Scripting Guide. For the command substitution issues, chapter 11

Last edited by colucix; 11-03-2008 at 03:12 PM.
 
Old 11-03-2008, 07:34 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,425

Rep: Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159
Since you are an "advanced programmer," let me also suggest to you that perhaps you should re-evaluate the question of whether it is worth your time to try to do this with "Bash scripting."

You have several full-featured programming languages at your disposal... probably "Perl, PHP, Python, and Ruby ... (Scheme, anyone?)... " maybe more. It does not make the slightest bit of difference to Unix/Linux which one you choose.

The #!program_name (so-called "shebang") line at the top of any file will specify the programming-language that any particular command is written in. Bash will automatically invoke the appropriate programming-language environment. The end-user will neither know nor care.

The scripting facilities in Bash are intended to be very rudimentary ... so that you don't have to invoke another language (if you don't wish to...) just to do "simple things." But at the same time, they have no reason to be anything more than that... because so gosh-darned many choices are available. You are free, then, to make a voluntary choice, entirely without penalty. And in this case, one of the various true programming-languages I just mentioned might be much more to your liking.

It all comes down to... "what is, for you, the best tool for the job." In your case, given your self-professed experience, I doubt that "Bash is 'it.'" I come from a more-or-less similar background, and it certainly is not for me... I frankly know very little about "Bash scripting" because I have had little reason to do much with it. Other "better for me" alternatives are so-readily available... The beauty of Unix/Linux is that "you actually have real choices." The amount of work-time that it would realistically take "an experienced programmer" to whack-out a suitable solution using any of the aforementioned languages would be negligible. I picked-up Python in an afternoon (good enough to get the job done...), PHP in a fortnight, Perl in less than three days. Ruby... well... (Naah, seriously, I just haven't gotten around to "that one, yet.")

Last edited by sundialsvcs; 11-03-2008 at 07:42 PM.
 
Old 11-04-2008, 03:55 AM   #4
ThaSaltyDawg
LQ Newbie
 
Registered: Nov 2008
Location: Myrtle Beach, SC
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sundialsvcs View Post
Since you are an "advanced programmer," let me also suggest to you that perhaps you should re-evaluate the question of whether it is worth your time to try to do this with "Bash scripting."

You have several full-featured programming languages at your disposal... probably "Perl, PHP, Python, and Ruby ... (Scheme, anyone?)... " maybe more. It does not make the slightest bit of difference to Unix/Linux which one you choose.

The #!program_name (so-called "shebang") line at the top of any file will specify the programming-language that any particular command is written in. Bash will automatically invoke the appropriate programming-language environment. The end-user will neither know nor care.

The scripting facilities in Bash are intended to be very rudimentary ... so that you don't have to invoke another language (if you don't wish to...) just to do "simple things." But at the same time, they have no reason to be anything more than that... because so gosh-darned many choices are available. You are free, then, to make a voluntary choice, entirely without penalty. And in this case, one of the various true programming-languages I just mentioned might be much more to your liking.

It all comes down to... "what is, for you, the best tool for the job." In your case, given your self-professed experience, I doubt that "Bash is 'it.'" I come from a more-or-less similar background, and it certainly is not for me... I frankly know very little about "Bash scripting" because I have had little reason to do much with it. Other "better for me" alternatives are so-readily available... The beauty of Unix/Linux is that "you actually have real choices." The amount of work-time that it would realistically take "an experienced programmer" to whack-out a suitable solution using any of the aforementioned languages would be negligible. I picked-up Python in an afternoon (good enough to get the job done...), PHP in a fortnight, Perl in less than three days. Ruby... well... (Naah, seriously, I just haven't gotten around to "that one, yet.")
WOW I had no idea I could use other languages. PHP or PERL sounds right up my alley. I definitely know more about those than I do about bash.

Do you have any help or tutorial links?
 
Old 11-04-2008, 07:12 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Perl:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials - see the database programming section
 
Old 11-04-2008, 07:35 PM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
If you use a bash variable in a script use double quotes instead of single quotes. Otherwise it is taken literally instead of evaluated. If your tables have a modification timestamp field, shouldn't you select records selected after your last backup. You are selecting records with an exact timestamp down to the second.
 
Old 11-05-2008, 01:03 AM   #7
ThaSaltyDawg
LQ Newbie
 
Registered: Nov 2008
Location: Myrtle Beach, SC
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
If you use a bash variable in a script use double quotes instead of single quotes. Otherwise it is taken literally instead of evaluated. If your tables have a modification timestamp field, shouldn't you select records selected after your last backup. You are selecting records with an exact timestamp down to the second.
Yea I noticed that and fixed it already

thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 08:30 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM
bash variable loses contents in mysql statement thedude2010 Programming 3 06-02-2006 05:15 AM
php mysql variable arguments in query. ldp Linux - Software 0 10-26-2004 08:56 AM
How to pass mysql query to a variable? chynna_v Programming 4 09-03-2004 06:09 AM


All times are GMT -5. The time now is 05:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration