[mythtv-users] Ways to Query Database From sh Script?

Michael Heironimus mkh01 at earthlink.net
Mon Feb 16 01:59:36 UTC 2009


On Sun, Feb 15, 2009 at 03:51:47PM -0800, Drew Tomlinson wrote:
> I'm working on my first UserJob script.  In this script, I want to test
> if a recording has a cutlist before processing.  I've Googled and the
> way found to query the database from /bin/sh goes like this:
> 
> CUTLIST=`cat << EOF | mysql -u mythtv -pmythtv mythconverg
>         SELECT
>                 cutlist
>         FROM
>                 recorded
>         WHERE
>                 basename = '$FILENAME';
> EOF`
> 
> Using the above, $CUTLIST will be set to either "cutlist 1" or "cutlist
> 0" depending upon whether a cutlist exists. (I suppose piping to awk
> would remove "cutlist").  Is this the only way to query from /bin/sh? 
> Is there a more practical way?

While this is the proper general approach for running DB queries from a
shell script, this specific example is unnecessarily ugly and
convoluted.

1. You don't need cat. It does nothing except feed a here document in to
mysql, so you might as well just use "mysql ... <<EOF" instead.

2. You don't need a here document anyway because that query is a
single-line statement so you could just echo it in to mysql.

3. But you don't need to echo it either because the man page for "mysql"
should mention the "-e" option that lets you pass a statement to be
executed on the command line.

4. While you are reading that man page to gain enlightenment you should
take note of options like "-N" and "-s" that reduce the output so you
won't get the column header in your variable.

-- 
Michael Heironimus


More information about the mythtv-users mailing list