LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting and mythtv (http://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-and-mythtv-881633/)

zunder1990 05-19-2011 01:36 PM

Bash scripting and mythtv
 
I tested this on mythtv 0.23 and Ubuntu 10.10. The script file is called mysql.sh and it is located at /home/user/mysql.sh . The other file is called mysqlcmd and is at /home/user/mysqlcmd.

This is mysql.sh
Code:

#!/bin/bash
#Wrote by Zach Underwood on 5-19-2011
#This script will look in the mythtv database for show that are newly record.
#Then copy them to another place.
#On the way of being copied it will rename it to something human readable.

day=`date -d 'yesterday' +%Y-%m-%d` #This will get yesterday day in the format of 2011-04-21.

sudo rm -f /tmp/recordedprogram #This will remove a temp file

sudo rm -f /tmp/recordedprogramnospace #This will remove a temp file.

mysql -h localhost -u root -ppassword < /home/zach/mysqlcmd
#This will read that mythtv database to see what has be recorded that output it to /tmp/recordedprogram

cat /tmp/recordedprogram | sed 's/ /-/g' >> /tmp/recordedprogramnospace
#This will read the file and replace all spaces with -

file=/tmp/recordedprogramnospace #This will set the temp file with no spaces

while IFS=# read -r chanid starttime title subtitle
#this will read one line at a time a read the different fields into memory and it sets the delimiter to "#"

do

filename=`echo "$chanid"_$starttime |  sed 's/://g' |  sed 's/ //g' | sed 's/-//g'`
#this will read and get the file name in the form of 2211_20110518200000 and this is what mythtv store video named

where=`locate "$filename.mpg"`
#this will read the filename from above and add .mpg to the file name and then will look for the file.
#With mythtv video can be in more than one different folders on the filesystem

fileplace=`echo "$where" | grep -v .png`
#This will read the location of the files from above and remove and that end in .png.

something=`echo $starttime | cut -c 1-10`
# This will read the start time put in the format of 2011-04-21

if [ $day = $something ]; then
#them compares the start time of the video file and yesterday's date and if they match then it will copy to anywhere you want.

sudo cp $fileplace /data/smb/"$title"_"$subtitle".mpg #This is what copies the video and rename it too.

fi

done < "$file"

This is mysqlcmd
Code:

#The bottom part is one line
USE mythconverg;

SELECT chanid, starttime, title, subtitle from recordedprogram into outfile '/tmp/recordedprogram' FIELDS TERMINATED BY '#';


To run then command you will need to pass your password into
Code:

echo "password" | sudo -S /home/user/mysql.sh


By the way I know that the code is not the most clean or that it is not the best way to write code. Well I dont care the code works. I did not write this for a company, I wrote this for home. I also know that mythtv has some scripts that could do this but I could not get them to work or they could not do what I want.

grail 05-19-2011 10:16 PM

Quote:

Well I dont care the code works.
So does this mean you want feedback? Seems an odd post as you don't really have a question??

zunder1990 05-19-2011 10:43 PM

I posted this for any that would like to use. I said that because last time that I posted code someone told that all of my code was wrong and start to tell what I was doing wrong.


All times are GMT -5. The time now is 03:13 AM.