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

David Brieck Jr. dbrieck at gmail.com
Mon Feb 16 01:34:34 UTC 2009


On Sun, Feb 15, 2009 at 6:51 PM, Drew Tomlinson <drew at mykitchentable.net> 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?
>
> I'm pretty new to this stuff so if the answer is obvious, nudges to the
> relevant links would be appreciated.
>
> Thanks,
>
> Drew

I'm a real big fan of PHP shell scripts. Just put this in a file and
execute it like a shell script:

#!/usr/bin/php -q
<?php

// connect
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

//query
$result = mysql_query("SELECT cutlist FROM recorded WHERE basename =
'".$FILENAME."'");

// will be either 0 or 1
$cutlist = mysql_num_rows($result);

// be nice and close the connection
mysql_close($link);
?>

Providing you have the correct packages installed you'll be able to
use all of PHP's built in database functionality. It should be much
quicker and simpler than figuring out how to do all that in a bash
script.

I haven't written a UserJob though, so I'm not sure where you're
getting $FILENAME from so if you use what I wrote you'll need to
figure that one out.


More information about the mythtv-users mailing list