Code Comments
Programming Forum and web based access to our favorite programming groups.Below is a bash command I used to extract some data from the MySQL DB: ======================================== ======== today=$( date -d '-2days' +%Y-%m-%d ) mysql -ulihao -p -e 'SELECT ...... FROM ..... WHERE `mydate` LIKE "$today%" ........' ======================================== ======== Basically, the SQL command is very long and need to access some shell variables. I know I can do it like: mysql -ulihao -p -e ' SELECT ...... FROM ..... WHERE `mydate` LIKE "$today%" ....... ' Can I use HERE documents? how-to??? I tried the following command line: mysql -ulihao -p -e <<END SELECT ...... FROM ..... WHERE `mydate` LIKE "$today%" ....... END and this does NOT work... Also how can I handle backticks properly in contained SQL command which will execute command under shell.. Thanks, lihao
Post Follow-up to this messageOn 2008-04-02, lihao0129@gmail.com wrote: > Below is a bash command I used to extract some data from the MySQL > DB: > ======================================== ======== > today=$( date -d '-2days' +%Y-%m-%d ) > mysql -ulihao -p -e 'SELECT ...... FROM ..... WHERE `mydate` LIKE > "$today%" ........' > ======================================== ======== > > Basically, the SQL command is very long and need to access some shell > variables. I know I can do it like: > > mysql -ulihao -p -e ' > SELECT ...... > FROM ..... > WHERE `mydate` LIKE "$today%" > ........ > ' > > Can I use HERE documents? how-to??? I tried the following command > line: > > mysql -ulihao -p -e <<END > SELECT ...... > FROM ..... > WHERE `mydate` LIKE "$today%" > ........ > END > > and this does NOT work... Also how can I handle backticks properly in > contained SQL command which will execute command under shell.. A here-document delivers its contents to the standard input of the command; you need it on the command line, e.g.: mysql -ulihao -p -e \ SELECT ...... \ FROM ..... \ WHERE `mydate` LIKE "$today%" \ ....... -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence
Post Follow-up to this messageOn Apr 2, 1:38 pm, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote: > On 2008-04-02, lihao0...@gmail.com wrote: > > > > > > > A here-document delivers its contents to the standard input of the > command; you need it on the command line, e.g.: > > mysql -ulihao -p -e \ > SELECT ...... \ > FROM ..... \ > WHERE `mydate` LIKE "$today%" \ > ........ oh, so I can not use HERE document here, right? Is it possible to assign an input from HERE document to a bash variable, like: var=<<END a long paragraph ....... ... END I know this is not working, just want to know if and how(if possible) I can do it.. thanks again... Regards, lihao
Post Follow-up to this messageOn 2008-04-02, lihao0129@gmail.com wrote: > On Apr 2, 1:38 pm, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote: > > oh, so I can not use HERE document here, right? Right. > Is it possible to > assign an input from HERE document to a bash variable, like: > > var=<<END > a long paragraph ....... > .... > END > > I know this is not working, just want to know if and how(if possible) > I can do it. I repeat: A here-document delivers its contents to the standard input of the command; you need it on the command line, e.g.: var=" a long paragraph ... " -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence
Post Follow-up to this messageIn article <4868a8f7-0d48-474c-aac5-200f70f948eb@m73g2000hsh.googlegroups.com>, "lihao0129@gmail.com" <lihao0129@gmail.com> wrote: > Below is a bash command I used to extract some data from the MySQL > DB: > ======================================== ======== > today=$( date -d '-2days' +%Y-%m-%d ) > mysql -ulihao -p -e 'SELECT ...... FROM ..... WHERE `mydate` LIKE > "$today%" ........' > ======================================== ======== > > Basically, the SQL command is very long and need to access some shell > variables. I know I can do it like: > > mysql -ulihao -p -e ' > SELECT ...... > FROM ..... > WHERE `mydate` LIKE "$today%" > ........ > ' > > Can I use HERE documents? how-to??? I tried the following command > line: > > mysql -ulihao -p -e <<END > SELECT ...... > FROM ..... > WHERE `mydate` LIKE "$today%" > ........ > END > > and this does NOT work... Also how can I handle backticks properly in > contained SQL command which will execute command under shell.. > > Thanks, > lihao As the other poster said, here-documents are passed as standard input, they don't get turned into command arguments. So you need a command that will read the input: mysql -ulihao -p -e "`cat <<END SELECT ... FROM ... WHERE ... .. END `" But, as the other poster said, you don't need the here-document, you can simply enter the parameter as a multi-line quoted argument. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE don't copy me on replies, I'll read them in the group ***
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.