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.
I'm in the process of creating a request system for my shoutcast server (using sc_serv and sc_trans_linux). I have created the frontend using php and mysql to grab all of the data I need. On the backend I need to create a script to move around the playlist, and load the request file when someone creates it.
My plan is:
User requests the song and is placed into a request file.
I would like to have a file monitor the file and detect if anything is added (tail -f perhaps?)
Then I would need to see if the request file is already being played on the server.
If it is, just add the song to the end of the file and reload the playlist.
If not, load the request file.
After a song is queued, delete the file from the playlist.
If playlist is empty reload the default playlist.
My problem is, I'm not sure how to delete a line from a playlist, add to the end of a playlist and detect the presence of a file. If someone could give me a few hints, that would be great.
This script should give you some pointers (never used shoutcast so bear with me ):
What I have done is to have two files; request.txt & playlist.txt. Whenever a song is added to request.txt it is added to the end of playlist.txt. If the song is already in playlist.txt then it is deleted from playlist.txt before adding it to the end. I hacked this script up pretty quickly, so there is no sanity checks etc.
Code:
#!/bin/bash
PL_FILE='playlist.txt'
REQ_FILE='request.txt'
#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
touch $PL_FILE
fi
tail -f $REQ_FILE|while read request;do
#Use awk to echo only the lines which *aren't* the requested file
cat $PL_FILE|awk '! /^'$request'$/ {print $NF}'>$PL_FILE
#Put the requested file on the end of the playlist
echo $request>>$PL_FILE
done
(Piping into loops and using read is really useful)
Hope that gives you some help, if you need any more explaination/help post back && I'll do my best.
Thanks infinity. That looks like I can use this for part of my script. Anyone have a tip for me about removing a line from the playlist after seeing it played in a log file? Or checking to see if the file is now empty after deleting the song?
On a side note:
If anyone thinks my logic is incorrect on how I am creating this script, let me know.
Originally posted by windisch Thanks infinity. That looks like I can use this for part of my script. Anyone have a tip for me about removing a line from the playlist after seeing it played in a log file? Or checking to see if the file is now empty after deleting the song?
On a side note:
If anyone thinks my logic is incorrect on how I am creating this script, let me know.
You could just pipe the whole list through grep -v to remove the line and redirect it to a temp file. Then copy the temp file over the orignial file and append (>>) the extracted line to the end.
Originally posted by carl.waldbieser
You could just pipe the whole list through grep -v to remove the line and redirect it to a temp file. Then copy the temp file over the orignial file and append (>> ) the extracted line to the end.
This will put all lines that aren't exactly $request back into $PL_FILE. Then you can do the append (echo whatever >> $PL_FILE).
Side note: Is it safe to redirect straight back into a file that you are piping in from? I've never had any problems doing it, but i've never tried it extensively
Quote:
Originally posted by windisch
Anyone have a tip for me about removing a line from the playlist after seeing it played in a log file? Or checking to see if the file is now empty after deleting the song?
Checking for an empty file is easy, i like to use 'wc'
Code:
if [ `wc -c $file|sed 's/ .*//'` -eq 0 ];then
file_is_empty
else
file_is_not_empty
fi
As for the log file removing thing, you can use tail -f to monitor the log file and then either grep -v or my awk meathod to remove it from the playlist.
I was planning on monitoring the log file for the song being played with tail -f. The log file looks like this:
<09/13/05@08:11:12> [MAIN] Title Updated
<09/13/05@08:14:24> [DECODE] Opened 09 - Leaving An Old Friend.mp3
<09/13/05@08:14:46> [MAIN] Title Updated
How do I get the info after the word Opened into the $request file so I can use your code? I'm new to parsing.
Ah yes, oops. I forgot that you have to escape the [ and ] characters in regular expressions:
Code:
cmd=$(echo $line|sed 's/.*[\(.*\)].*/\1/')
Should be:
Code:
cmd=$(echo $line|sed 's/.*\[\(.*\)\].*/\1/')
and
Code:
filename=$(echo $line|sed 's/.*] Opened \(.*\)/\1/')
should be
Code:
filename=$(echo $line|sed 's/.*\] Opened \(.*\)/\1/')
Note the backslashes to escape the [ & ].
The second error is due to the fact that cmd was being set to an empty string, as the sed script failed. So bash saw
Code:
if [ = "DECODE" ]
and coughed. Now cmd should be being set correctly, so that shouldn't be a problem.
It's grabbing the first word. But the - might be throwing it off. Can I set it up to detect everything after Opened and end it with .mp3? There is no specific pattern to the file names outside of having .mp3 at the end.
<09/13/05@14:37:46> [MAIN] Title Updated
<09/13/05@14:41:40> [DECODE] Opened 11 - Troian Beauty.mp3
<09/13/05@14:42:03> [MAIN] Title Updated
<09/13/05@14:45:02> [DECODE] Opened 13 - The Brink Of Time.mp3
<09/13/05@14:45:24> [MAIN] Title Updated
<09/13/05@14:47:31> [DECODE] Opened 16 - One-Winged Angel.mp3
<09/13/05@14:47:53> [MAIN] Title Updated
<09/13/05@14:54:29> [MAIN] SIGWINCH; Next Song
<09/13/05@14:54:30> [DECODE] Opened Electronica - Der Ententanz (Dance Little Bird).mp3
<09/13/05@14:54:52> [MAIN] Title Updated
<09/14/05@07:09:57> [MAIN] Title Updated
<09/14/05@07:13:27> [DECODE] Opened David Bowie - Let's Dance.mp3
<09/14/05@07:13:49> [MAIN] Title Updated
<09/14/05@07:17:24> [DECODE] Opened 23 - Mysterious Shop.mp3
<09/14/05@07:17:46> [MAIN] Title Updated
<09/14/05@07:18:23> [DECODE] Opened Sugar Ray - Lemonade and Brownies - 12 - Caboose.mp3
<09/14/05@07:18:46> [MAIN] Title Updated
<09/14/05@07:21:34> [DECODE] Opened Kill Bill - 14 - The Lonely Shepherd - Zamfir.mp3
<09/14/05@07:21:56> [MAIN] Title Updated
What do I need to do to get the rest of the file name to be copied? Do I need to be worried about it grabbing "Updated"?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.