bash: How to use Here document on the command line
bash: How to use Here document on the command line
am 02.04.2008 19:26:24 von lihao0129
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 <
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
Re: bash: How to use Here document on the command line
am 02.04.2008 19:38:36 von cfajohnson
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 <
> 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
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
Re: bash: How to use Here document on the command line
am 02.04.2008 19:56:17 von cfajohnson
On 2008-04-02, lihao0129@gmail.com wrote:
> On Apr 2, 1:38 pm, "Chris F.A. Johnson" wrote:
>> On 2008-04-02, lihao0...@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 <
>> > 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%" \
>> ........
>
> 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=<
> 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
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
Re: bash: How to use Here document on the command line
am 02.04.2008 19:57:44 von lihao0129
On Apr 2, 1:38 pm, "Chris F.A. Johnson" wrote:
> On 2008-04-02, lihao0...@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 <
> > 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%" \
> ........
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=<
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
Re: bash: How to use Here document on the command line
am 03.04.2008 02:20:52 von Barry Margolin
In article
<4868a8f7-0d48-474c-aac5-200f70f948eb@m73g2000hsh.googlegroups.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 <
> 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 <
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 ***