BASH question
Paul King
sciguy-Ja3L+HSX0kI at public.gmane.org
Sat Dec 15 11:47:31 UTC 2007
Why BASH? If you want scripting, this looks like a prime candidate for a PERL
project.
If you insist on BASH, then you will need to teach yourself SED and AWK, which
are two good stream-level parsers.
> Hey all,
>
> I've been banging my head against something all day that *should* be
> easy/obvious, but obviously not to me...
>
> I've got a task (insane, yes, but...) to write a sample script in BASH to
> call do a mysql database call and parse the results. I've build the call
> and can get the results showing on the screen, but I've been stumped dumb
> on how to feed the results, one line at a time, into an array.
>
> Any tips/suggestions/helpful whacks up-side the head are much appreciated!
>
> Madi
>
> PS - Here is what I've got (stripped down) so far...
>
> #!/bin/bash
>
> # Variables...
> MYSQLBIN='/usr/bin/mysql';
> DBNAME='testdb';
> DBUSER='user';
> DBPASSWD='secret';
> DBPORT=''
> DBHOST='localhost'
>
> # This creates the connection string that will be use in a similar way
> # that the 'dbh' database handle was used in the Perl and Ruby sample
> # scripts.
> # '-b' disables the bell (this is a script, so useless)
> # '--disable-pager' makes sure the output doesn't get interrupted.
> # '-Bs' is 'silent' output (s) without tables headers (B), columns are
> # tab-deliminated.
> # You may want to look at '-E' for "vertical" output in some cases.
> # You may want to look at '-X' for XML-formatted output; looks nice!
> DBH="mysql -Bs -b --disable-pager -D ${DBNAME} -u ${DBUSER} -p${DBPASSWD}";
> if [ "${DBPORT}" != "" ]; then
> {
> # Append the port command if defined.
> DBH="${DBH} -P ${DBPORT}"
> }
> fi
> if [ "${DBHOST}" != "" ]; then
> {
> # Append the host commend if defined.
> DBH="${DBH} -h ${DBHOST}";
> }
> fi
> echo "SQL connection command: [${DBH}]";
>
> # Our sample query
> QUERY='SELECT foo, bar, baz FROM testtable LIMIT 5;';
> echo "Query: [${QUERY}]"
>
> # Execute the query.
> echo "Calling: [${DBH} -e\"${QUERY}\"]";
> echo "/- Begin DB results output -=-=-=-=-"
> ${DBH} -e"${QUERY}";
> echo "\\- End DB results output -=-=-=-=-=-"
> exit;
>
> --
> The Toronto Linux Users Group. Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
>
> __________ NOD32 2724 (20071214) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>
--
The Toronto Linux Users Group. Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
More information about the Legacy
mailing list