ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Oh I know what I have done wrong. Test gets funny if you don't put spaces before and after the ='s.
Try:
Code:
if [ "$cmd" = "MAIN" -a "$msg" = "Title Updated" ];then
if [ ! -z "$filename" ];then
echo "$cmd $msg"
echo "Requested Song $filename Has Been Played. Removing From Playlist."
cat $PL_FILE2|awk '! /'"$filename"'$/ {print $L}'>$PL_FILE2
kill -s USR1 $t
filename=""
fi
fi
The statement is working, but not correctly. It doesn't detect MAIN and Title Updated.
I echoed the script and this is what it outputs:
MAIN /1
Song Requested. Added To Current Playlist
MAIN /1
MAIN /1
MAIN /1
MAIN /1
DECODE: Disc2_05_-1979.mp3
DECODE /1
Song Requested. Added To Current Playlist
MAIN /1
MAIN /1
MAIN /1
MAIN /1
So it is not grabbing the Title Updated part, and I'm not sure why. Thanks again. Take you time and get back with me when you have time. I'm sure this is taking longer than you expected.
That did it! There's only 1 small thing I can think of. If a song is requested, during the process of decoding, I get the seg fault. How can I get this code below to wait until the Main Title Updated shows in the log?
Code:
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
touch $PL_FILE2
chmod 666 $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
There's only 1 small thing I can think of. If a song is requested, during the process of decoding, I get the seg fault. How can I get this code below to wait until the Main Title Updated shows in the log?
There are a couple of ways to implement it, it depends on one thing: Will there ever be more than one request after a DECODE but before the Main Title Updated?
I would say, more than likely. It takes between 30-60 seconds for the decoding of a song to finish. At the moment a friend and I listen to it, often at the same time. So there could be multiple requests entered at any time.
OK.. what we will have to do is to indroduce a request queue. So when a request occurs it checks if there is decoding in progress, and if so queue the request up. Then once the decoding finishes, if there is a request list, add all the requests to the playlist. I implemented this and ended up with something like this:
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'
QUE_FILE='request_queue.txt'
curfile="NON" #Just incase it doesn't get set on the first pass
tail -f --lines=1 $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 [ ! -f $PL_FILE ];then #Will Ignore Removing Songs If Default List Is Loaded
echo "Ignoring Log"
else
if [ $curfile = "LOG" ];then
cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
msg=$(echo $line|sed 's/.*\] \(.*\)/\1/')
if [ $cmd = "DECODE" ];then
filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
echo 'DECODE: '$filename
fi
if [ "$cmd" = "MAIN" -a "$msg" = "Title Updated" ];then
if [ ! -z "$filename" ];then
echo "Requested Song $filename Has Been Played. Removing From Playlist."
cat $PL_FILE2|awk '! /'"$filename"'$/ {print $L}'>$PL_FILE2
filename=""
#If there is a request queue, act on the queue
if [ -f $QUE_FILE ];then
if [ ! -f $PL_FILE ];then
echo "CREATE: $PL_FILE2"
mv $PL_FILE2 $PL_FILE
touch $PL_FILE2
kill -s USR2 $t #Should this be below the loop?
fi
cat $QUE_FILE|while read line;do
echo $line >> $PL_FILE2
done
kill -s USR1 $t
rm -f $QUE_FILE
fi
fi
fi
fi
fi
if [ $curfile = "REQ" ];then
echo "REQUEST: $line"
if [ ! -z "$filename"];then
echo 'Decoding In progress, adding request to queue.'
echo $line>>$QUE_FILE
else
if [ ! -f $PL_FILE ];then
#If it doesn't exist then create an empty file using touch and add song to list
echo "CREATE: $PL_FILE2"
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
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 don't know if your script is exactly the same as the one I modified here, so I will point out all the changes I have made individually:
Code:
QUE_FILE='request_queue.txt'
Just add a variable to hold the location of the queue file
Code:
filename=""
#If there is a request queue, act on the queue
if [ -f $QUE_FILE ];then
if [ ! -f $PL_FILE ];then
echo "CREATE: $PL_FILE2"
mv $PL_FILE2 $PL_FILE
touch $PL_FILE2
kill -s USR2 $t #Should this be below the loop?
fi
cat $QUE_FILE|while read line;do
echo $line >> $PL_FILE2
done
kill -s USR1 $t
rm -f $QUE_FILE
fi
Implement the checking for the queue. A little code duplication here, from the request check below.
Code:
if [ ! -z "$filename"];then
echo 'Decoding In progress, adding request to queue.'
echo $line>>$QUE_FILE
else
if [ ! -f $PL_FILE ];then
#If it doesn't exist then create an empty file using touch and add song to list
echo "CREATE: $PL_FILE2"
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
fi
If there is decoding in progress then queue the request, otherwise procced as normal.
Once again, great job. The songs waited in the queue, until the song was done decoding. I will have to modify it to wait for the same thing when playing the default list also, but I'll try that first before bothering you again.
It looks like you added the code in correctly, but it is not detecting if it decoding correctly. I added an echo to see what the contents of $filename is, and its empty.
Code:
if [ $curfile = "REQ" ];then
echo "REQUEST: $line"
if [ ! -z "$filename" ];then
echo 'Decoding In progress, adding request to queue.'
echo $line>>$QUE_FILE
else
if [ ! -f $PL_FILE ];then
#If it doesn't exist then create an empty file using touch and add song to list
Also I Modified This
Code:
if [ "$cmd" = "MAIN" -a "$msg" = "Title Updated" ];then
if [ ! -z "$filename" ];then
echo "Requested Song $filename Has Been Played. Removing From Playlist."
cat $PL_FILE2|awk '! /'"$filename"'$/ {print $L}'>$PL_FILE2
filename=""
#If there is a request queue, act on the queue
To This:
Code:
if [ "$cmd" = "MAIN" -a "$msg" = "Title Updated" ];then
if [ ! -z "$filename" ];then
echo "Requested Song $filename Has Been Played. Removing From Playlist."
cat $PL_FILE2|awk '! /'"$filename"'$/ {print $L}'>$PL_FILE2
if [ `wc -c $PL_FILE2|sed 's/ .*//'` -eq 0 ];then
echo "Empty"
else
kill -s USR1 $t
fi
filename=""
#If there is a request queue, act on the queue
So that it would reload the playlist after remove, except for if it was empty, because that causes an error. If its empty, it will let your last section of code take effect.
I figured out why. Because of the this if statement, its not setting $filename, because it is currently ignoring the log file when the default playlist is playing.
Code:
if [ ! -z "`echo $line|sed 's/.*==>.*<==.*//'`" ];then
if [ ! -f $PL_FILE ];then #Will Ignore Removing Songs If Default List Is Loaded
echo "Ignoring Log"
else
if [ $curfile = "LOG" ];then
cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
msg=$(echo $line|sed 's/.*\] \(.*\)/\1/')
if [ $cmd = "DECODE" ];then
filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
echo 'DECODE: '$filename
So I modified it to this:
Code:
if [ ! -z "`echo $line|sed 's/.*==>.*<==.*//'`" ];then
if [ ! -f $PL_FILE ];then #Will Ignore Removing Songs If Default List Is Loaded
echo "Ignoring Log"
if [ $curfile = "LOG" ];then
cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
msg=$(echo $line|sed 's/.*\] \(.*\)/\1/')
if [ $cmd = "DECODE" ];then
filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
echo 'DECODE: '$filename
else
if [ $curfile = "LOG" ];then
cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
msg=$(echo $line|sed 's/.*\] \(.*\)/\1/')
if [ $cmd = "DECODE" ];then
filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
echo 'DECODE: '$filename
I came across another issue. I noticed that if a filename has parentheses, it will not correctly remove them from the playlist. Is there a way around this with awk? Or do I need to remove the parentheses from the filenames?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.