LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2005, 08:00 AM   #16
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30

For the part of the script that monitors the request file and then moves it to the playlist, should I use this part of the script you used for the log monitoring?
What I need to do here is just grab the entire line that is written to this request file. It's in a format like this:

/video3/MythMusic/Artist/Album/Song Title.mp3

There could be letters, symbols or numbers in the line.

tail -f $LOG_FILE|while read line;do
cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
 
Old 09-14-2005, 11:00 AM   #17
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
I've done some work to get the logical portion of the script done. Here is what I have

Code:
#!/bin/bash

process=sc_trans_linux  ###Process name entered into variable###
t=`pidof $process`      ###Grabs PID and enters it into variable###
echo $t
PL_FILE='MythMusic.temp'
PL_FILE2='MythMusic.alw'
REQ_FILE='/var/www/html/shoutcast/request.alw'
LOG_FILE='/home/draco/shoutcast/transcode/sc_trans.log'

tail -f $REQ_FILE|while read line;do
#Check if PL_FILE *doesn't* exists (without the bang (!) it would check if it did exist)
if [ ! -f $PL_FILE ];then
        #If it doesn't exist then create an empty file using touch and add song to list
        mv $PL_FILE2 $PL_FILE
        touch $PL_FILE2
        echo $line >> $PL_FILE2
        kill -s USR2 $t
        kill -s USR1 $t
else
        echo $line >> $PL_FILE2  #Adds To Currently Plaing Request List
        kill -s USR1 $t
fi

tail -f $LOG_FILE|while read line;do
        cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
        filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
     if [ $cmd = "DECODE" ];then
        awk '! /^'"$filename"'$/ {print $NF}'
     fi
done

if [ `wc -c $PL_FILE2|sed 's/ .*//'` -eq 0 ];then
     #file_is_empty
        mv $PL_FILE $PL_FILE2
        kill -s USR2 $t
        kill -s USR1 $t

else
     #file_is_not_empty
fi

done #Ends Main While Loop
I get this error when I try to run it:
./request_test: line 41: syntax error near unexpected token `fi'
./request_test: line 41: `fi'
 
Old 09-14-2005, 11:12 AM   #18
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Nevermind, I fixed the last if then statement.

After running I get this output:

Updated
Live).mp3
Updated
Harlem.mp3
Updated
Playlist
playlist
(MythMusic.alw)
entries...
entries...
playlist
(Arranged).mp3

It only appears to be grabbing the last word in the line of the log.

Last edited by windisch; 09-14-2005 at 11:46 AM.
 
Old 09-15-2005, 09:01 AM   #19
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Can anyone help me get the script to pull up the entire filename and not the last word?
 
Old 09-15-2005, 09:06 AM   #20
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
Just a quick reply, i'm rather busy right now, but will get back and have a look later. The problem with it only getting the last word in the awk script; the $NF should be $L.
 
Old 09-15-2005, 09:15 AM   #21
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
I figured you were busy. I'm not trying to push, I was looking back on my last post, and didn't think I was clear. Thanks agian for the help.
 
Old 09-15-2005, 09:49 AM   #22
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
OK, I can see the entire line now in the display. There must be something wrong with my logic in this script.
It's not detecting if the request file is empty.
It's not removing the song from the list after being played.
It is not detecting request songs requested after the first request.

Is there something wrong with my loops? I would like to create this, so it can constantly run and monitor these files, so I can automate this process.

Code:
#!/bin/bash

process=sc_trans_linux  ###Process name entered into variable###
t=`pidof $process`      ###Grabs PID and enters it into variable###
echo $t
PL_FILE='MythMusic.temp'
PL_FILE2='MythMusic.alw'
REQ_FILE='/var/www/html/shoutcast/request.alw'
LOG_FILE='/home/draco/shoutcast/transcode/sc_trans.log'

tail -f $REQ_FILE|while read line;do
#Check if PL_FILE *doesn't* exists (without the bang (!) it would check if it did exist)
if [ ! -f $PL_FILE ];then
        #If it doesn't exist then create an empty file using touch and add song to list
        echo "Song Requested.  New Request Playlist To Be Created"
        mv $PL_FILE2 $PL_FILE
        touch $PL_FILE2
        echo $line >> $PL_FILE2
        kill -s USR2 $t
        kill -s USR1 $t
else
        echo "Song Requested.  Added To Current Playlist"
        echo $line >> $PL_FILE2  #Adds To Currently Plaing Request List
        kill -s USR1 $t
fi

tail -f $LOG_FILE|while read line;do
        cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
        filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
     if [ $cmd = "DECODE" ];then
        awk '! /^'"$filename"'$/ {print $L}'
        fi
done

if [ `wc -c $PL_FILE2|sed 's/ .*//'` -eq 0 ];then
     #file_is_empty
        echo "Request Playlist Is Empty, Reverting To Default Playlist"
        mv $PL_FILE $PL_FILE2
        kill -s USR2 $t
        kill -s USR1 $t

#else
     #file_is_not_empty
fi

done #Ends Main While Loop
 
Old 09-15-2005, 04:55 PM   #23
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
I've had a quick look, and the main problem is with the nested do loop:
Code:
tail -f $LOG_FILE|while read line;do
        cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
        filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
     if [ $cmd = "DECODE" ];then
        awk '! /^'"$filename"'$/ {print $L}'
        fi
done
this loop will not return until tail -f finishes.. which will be never. So the script stick in this loop. The problem is that you want to be monitoring two files at once. The best solution is probably to use tail -f with two arguments. The problem with this is that you have to keep track of which file tail has detected changes in. I have written this little script:
Code:
#!/bin/bash
process=sc_trans_linux  ###Process name entered into variable###
t=`pidof $process`      ###Grabs PID and enters it into variable###
echo $t
PL_FILE='MythMusic.temp'
PL_FILE2='MythMusic.alw'
REQ_FILE='/var/www/html/shoutcast/request.alw'
LOG_FILE='/home/draco/shoutcast/transcode/sc_trans.log'
curfile="NON" #Just incase it doesn't get set on the first pass
tail -f $LOG_FILE $REQ_FILE|while read line;do
        if [ "$line" = "==> $REQ_FILE <==" ];then
                curfile="REQ"
        fi
        if [ "$line" = "==> $LOG_FILE <==" ];then
                curfile="LOG"
        fi
        if [ ! -z `echo $line|sed 's/.*==>.*<==.*//'` ];then
                if [ $curfile = "LOG" ];then
                        cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
                        filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
                        if [ $cmd = "DECODE" ];then
                                cat $PL_FILE|awk '! /^'"$filename"'$/ {print $L}'>$PL_FILE
                        fi
                fi
                if [ $curfile = "REQ" ];then
                        if [ ! -f $PL_FILE ];then
                                #If it doesn't exist then create an empty file using touch and add song to list
                                echo "Song Requested.  New Request Playlist To Be Created"
                                mv $PL_FILE2 $PL_FILE
                                #You don't actually need touch here, as the above command will create the file
                                #touch $PL_FILE2
                                echo $line >> $PL_FILE2
                                kill -s USR2 $t
                                kill -s USR1 $t
                        else
                                echo "Song Requested.  Added To Current Playlist"
                                echo $line >> $PL_FILE2  #Adds To Currently Plaing Request List
                                kill -s USR1 $t
                        fi
                fi
        fi
        if [ `wc -c $PL_FILE2|sed 's/ .*//'` -eq 0 ];then
                echo "Request Playlist Is Empty, Reverting To Default Playlist"
                mv $PL_FILE $PL_FILE2
                kill -s USR2 $t
                kill -s USR1 $t
        fi
done
I've copied and pasted most of your code into there, with a few minor alterations. We can work on that bit once we figure out if this works lol.

Good luck!
 
Old 09-19-2005, 07:24 AM   #24
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
I finally got a chance to try out the code and I get this error:

./request: line 17: [: too many arguments
 
Old 09-19-2005, 08:39 AM   #25
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
I think that might be solved by putting quotes around the expression in the if:
Code:
if [ ! -z "`echo $line|sed 's/.*==>.*<==.*//'`" ];then
rather than
Code:
if [ ! -z `echo $line|sed 's/.*==>.*<==.*//'` ];then
Hope that helps
 
Old 09-19-2005, 10:16 AM   #26
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
We're getting close here. Its correctly detecting files and songs. One problem with the code is this part:

Code:
if [ ! -z "`echo $line|sed 's/.*==>.*<==.*//'`" ];then
                if [ $curfile = "LOG" ];then
                        cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
                        filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
                        if [ $cmd = "DECODE" ];then
                                cat $PL_FILE|awk '! /^'"$filename"'$/ {print $L}'>$PL_FILE
                        fi
This code is monitoring the log file and creates the temp file by default, which then throws the next if statement into adding onto my default playlist, and not creating a new playlist. I modified the variable so it doesn't create the temp file automatically. I'm now testing the rest of the script. I'll post my results shortly. Thanks again for your help!
 
Old 09-19-2005, 10:53 AM   #27
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Ok, it is detecting if the song has been played now. But it is not removing the filename from the playlist. It is only grabbing the filename and not the path, so it will not remove it with your awk command.

Example:
02 Heavy.mp3
/video3/MythMusic/Collective Soul/Dosage/02 Heavy.mp3

Can we change the command so that it wil remove it if it finds the end of the filepath to be a match?
 
Old 09-19-2005, 11:29 AM   #28
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
easy
Code:
cat $PL_FILE|awk '! /'"$filename"'$/ {print $L}'>$PL_FILE
rather than
Code:
cat $PL_FILE|awk '! /^'"$filename"'$/ {print $L}'>$PL_FILE
Note the removal of the ^. Now it will strip out any lines that end in $filename.
 
Old 09-19-2005, 12:40 PM   #29
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
That appears to be working, very well. I am running into a problem with it (of course). When I enable the script, it starts running through the default playlist (all of my songs) and starts removing them from the playlist. The first time I ran it, it removed all of the songs from the list. I don't want it to remove any songs, until a song is requested and the request playlist is being played. There should be a way to only use the awk script, after a song is requested, right?
 
Old 09-19-2005, 12:46 PM   #30
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Another thing is when I start the script, it starts removing the songs previously played (because of tail's default of 10 lines i think). If when set the lines to around 3, that should take care of that, right?

Last edited by windisch; 09-19-2005 at 12:48 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
creating shell script programming using KNOPPIX.. help cinderella Linux - Newbie 4 12-20-2004 08:12 PM
Creating a shell script to run Java program paultaylor Programming 7 11-12-2004 04:11 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 11:23 PM
Help creating a directory back up shell script WarriorWarren Linux - General 6 04-06-2003 10:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration