Querying a mysql database from within a shell script?


Results 1 to 2 of 2

Thread: Querying a mysql database from within a shell script?

  1. #1
    Join Date
    Apr 2001
    Location
    UK
    Posts
    11

    Querying a mysql database from within a shell script?

    Is this possible?

    I know your able to use prewritten scripts like so:

    Code:
    mysql db_name < text_file
    but what if the sql statement needs other variables in order to be useful.

    Let me explain.
    I've written a script that searches a directory for *.%17 files. These are text files output from our archaic back office software. This software was written before the days of email addresses so doesn't unerstand them.

    If the script finds a %17 file it runs a line:

    Code:
    luser=`sed '2!d' $file`
    which scans the second line of the file for the username

    I have another file set up that looks like this called emap

    Code:
    AJR      angela@domain.com
    AM       annem@domain.com
    AR       angel@domain.com
    AR2      angel@domain.com
    AW       andrew@domain.com
    BB       blaize@domain.com
    I then run a line that goes:
    Code:
    senderemail=`grep -w ${luser:0:8} /path/to/emap`
    I can then use

    Code:
    ${senderemail:8}
    for my email address which I can then use with mutt. Hey presto, an email spooler.

    It works but is very cumbersome and I could do with more fields with more info for each user to add extra functionality.

    I would to use bash to say something along the lines of:
    email=`select email from database where $luser = AJR`
    or
    phone=`select phoneno from database where $luser = AJR`

    Is this possible, because it would be great.
    Thanks
    Ed
    Last edited by Eddie Fantastic; 09-07-2005 at 11:53 AM.
    I honestly haven't a clue what I'm doing.

  2. #2
    Join Date
    Sep 2003
    Location
    Rochester, MN
    Posts
    3,604
    I use the following for my commercial remover to use with Myth. It reads the cutlist from the database and eventually it gets passed to MPlayer in an edl. There's a lot of options to make sure the output is in a usable format for my purposes, but the man page for mysql should explain all about them.
    Code:
    mysql -D mythconverg -u mysql --password=almost_forgot_to_remove_this;) -r -s --skip-column-names -e "select cutlist from recorded where title like 'sometitle'"
    BTW, I don't use Myth's transcoding because the files it outputs don't work for me with MPlayer, which means I can't burn them to DVD. Not relevant here, but just so people don't think I'm an idiot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •