moc
April 20th, 2008, 06:27 PM
Hi,
I've created a small script for playing out my mp3 files randomly.
It generates a random number and then plays out the associated mp3 in a background process using play. By polling the play process, it detects when the play is ended and the next number is played.
#!/bin/bash
#
# Play music from misc dir
misc_path="/home/moc/Music/Misc"
cd $misc_path
num_music=`find . | grep mp3 | wc -l`
echo "number of MP3's : $num_music"
while true
do
number=$RANDOM
let "number %= $num_music"
let "number += 1"
current_num=`find . | grep mp3 | head -$number | sed '$!d'`
play "$current_num" &>/dev/null &
echo "playing $current_num"
while [ `ps -ef | grep "$current_num" | wc -l` -gt 0 ];
do
sleep 1
done
echo "ending $current_num"
done
exit 0
My problem is the `ps -ef | grep "$current_num" | wc -l`
As i understand grep, it will print out the the matching process and the grep itself. e.g if the process is found, the above will return 2.. if not 1.
example (playing is ruinning):
$ps -ef | grep "./mymusic.mp3"
moc 2886 2876 12 00:12 pts/0 00:00:06 play ./mymusic.mp3
moc 3388 2984 0 00:13 pts/1 00:00:00 grep ./mymusic.mp3
However.. when running my script, it seems that sometimes, the last output line is omitted, so that grep doesn't output itself ??
That's why I check for greater than zero rather than one in my script.
Else my script will sometimes start the next number before the current is ended. On the other hand... with 0, I get an unpredictable delay between numbers.
can anyone explain this ?? Is a shell bug??
Besides this... my script seems to work. Feel free to use it with your modifications
regards
/moc
I've created a small script for playing out my mp3 files randomly.
It generates a random number and then plays out the associated mp3 in a background process using play. By polling the play process, it detects when the play is ended and the next number is played.
#!/bin/bash
#
# Play music from misc dir
misc_path="/home/moc/Music/Misc"
cd $misc_path
num_music=`find . | grep mp3 | wc -l`
echo "number of MP3's : $num_music"
while true
do
number=$RANDOM
let "number %= $num_music"
let "number += 1"
current_num=`find . | grep mp3 | head -$number | sed '$!d'`
play "$current_num" &>/dev/null &
echo "playing $current_num"
while [ `ps -ef | grep "$current_num" | wc -l` -gt 0 ];
do
sleep 1
done
echo "ending $current_num"
done
exit 0
My problem is the `ps -ef | grep "$current_num" | wc -l`
As i understand grep, it will print out the the matching process and the grep itself. e.g if the process is found, the above will return 2.. if not 1.
example (playing is ruinning):
$ps -ef | grep "./mymusic.mp3"
moc 2886 2876 12 00:12 pts/0 00:00:06 play ./mymusic.mp3
moc 3388 2984 0 00:13 pts/1 00:00:00 grep ./mymusic.mp3
However.. when running my script, it seems that sometimes, the last output line is omitted, so that grep doesn't output itself ??
That's why I check for greater than zero rather than one in my script.
Else my script will sometimes start the next number before the current is ended. On the other hand... with 0, I get an unpredictable delay between numbers.
can anyone explain this ?? Is a shell bug??
Besides this... my script seems to work. Feel free to use it with your modifications
regards
/moc