Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

bash: How to use Here document on the command line
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

Report this thread to moderator Post Follow-up to this message
Old Post
lihao0129@gmail.com
04-03-08 12:41 AM


Re: bash: How to use Here document on the command line
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chris F.A. Johnson
04-03-08 12:41 AM


Re: bash: How to use Here document on the command line
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
lihao0129@gmail.com
04-03-08 12:41 AM


Re: bash: How to use Here document on the command line
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chris F.A. Johnson
04-03-08 12:41 AM


Re: bash: How to use Here document on the command line
In 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 ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
04-03-08 12:41 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Shell Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 01:47 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.