------myplayer添加播放列表的源码
#!/bin/sh
#MList
#############################function
sets#################################
#to add a new item to the list,if the list is not exit,then
creat it
add()
{
Hold="" #for hold
something
SName="" #name for the song
Lines="0"
NextLine=""
Hold=`find $MusicPath -name ListLib`
if [ -z $Hold ];then #if the hold is empty
touch $MusicPath/ListLib
fi
while :
do
echo "Enter the num of the song you want to add"
echo "[q to quit][li to see the list]:"
read SNum
if [ -n $SNum ];then
if [ $SNum = "q" ];then
return 0
fi
if [ $SNum = "li" ];then
ls $MusicPath|sed =
echo "****************************************************"
echo "Enter the number of the song"
read SNum
fi
#get the lines number of the list
SNum=`expr $SNum /* 2`
SName=`ls $MusicPath|sed =|sed -n /`echo $SNum/`p`
Lines=`wc -l $MusicPath/ListLib|awk '{print $1}'`
NextLine=`expr $Lines + 1`
echo "$NextLine
$MusicPath/$SName">>$MusicPath/ListLib
else
echo "You entered anything!Enter again!"
fi
done
return 0
}
#delete some items in the list
delete()
{
Hold="" #for hold
something
SName="" #name for the song
Lines="0"
NextLine=""
MaxLines=""
Hold=`find
$MusicPath -name ListLib`
if [ -z $Hold ];then #if the hold is empty
echo "Still No list ,enter add to creat one!"
return 0
fi
while :
do
Lines=`wc -l $MusicPath/ListLib|awk '{print $1}'`
if [ $Lines -eq "0" ]; then
echo "List empty!"
return 0
fi
echo "Enter the name of the song you want to delet[q to
quit]:"
echo -e "Or enter /"l/" to check the list"
read SName
if [ -n $SName ];then
if [ $SName = "q" ];then
break
fi
if [ $SName = "l" -o $SName = "L" ];then
cat
$MusicPath/ListLib|more
#open the list
echo "you can enter the number of the song:"
read Lines
else
#get the lines which will be delet
Lines=`sed -n /`echo $SName/`p $MusicPath/ListLib|awk '{print
$1}'`
fi
#delet the item choosed
sed `echo $Lines`d $MusicPath/ListLib>c;mv c
$MusicPath/ListLib
#every lines bellow the $Lines minutes 1
MaxLines=`wc -l $MusicPath/ListLib|awk '{print $1}'`
while [ $Lines -le $MaxLines ]
do
NextLine=$Lines
Lines=`expr $Lines + 1`
sed s/$Lines/$NextLine/ $MusicPath/ListLib>out
mv out
$MusicPath/ListLib
done
else
echo "You entered anything!Enter again!"
fi
done
return
0
}
#play in loop
loopPlay()
{
echo "loop running....[ctrl]+z to stop!"
while :
do
mplayer `cat $MusicPath/ListLib|awk '{print $2}'`
done
return 0
}
#play in random
randomPlay()
{
count="0"
i="0"
Hold=`wc -l
$MusicPath/ListLib|awk '{print $1}'`
#when the list is less than 5
items
Random=`awk 'BEGIN{srand();print
a=int(10*rand())}'`
if [ $Hold -le "10" ];then
while :
do
if [ $Random -le $Hold ];then
Play=`sed -n /`echo $Random/`p $MusicPath/ListLib|awk '{print
$2}'`
echo
"&&&&&&&&&&&$Play
^^^^^^^^^^^^^^^^^^^^"
mplayer $MusicPath/$Play
fi
Random=`awk 'BEGIN{print a=int(10*rand())}'`
done
#when the list is more than 5
items
else
while : #play untill you
entered the [esc] button
do
Random=`expr $Random + $count`
i=`expr $Hold - $count`
if [ $i -ge "5" ];then
count=`expr $count + 5`
else
count="0"
fi
#when the random is more than total items,do nothing
#if less than items,play the music
if [ $Random -le $Hold ];then
Play=`sed -n /`echo $Random/`p $MusicPath/ListLib|awk '{print
$2}'`
echo
"&&&&&&&&&&&$Play
^^^^^^^^^^^^^^^^^^^^"
mplayer $Play
fi
Random=`awk 'BEGIN{print a=int(10*rand())}'`
done
fi
return 0
}
#select a song to play
selectPlay()
{
Num=""
Hold=""
Play=""
echo
"###############################################################"
echo "The items are as following:"
cat `echo $MusicPath`/ListLib|more
echo
"###############################################################"
echo "Enter the items' number to select"
read Num
Hold=`wc -l $MusicPath/ListLib|awk '{print $1}'`
if [ $Hold -ge $Num ];then
Play=`sed -n /`echo $Num/`p $MusicPath/ListLib|awk '{print
$2}'`
mplayer $Play
else
echo "No such files!"
fi
}
#############################################################################
MusicPath=/home/flora/Music
#where the music is stored
export MusicPath
PWDPath=`pwd` #show the current path
Hold="" #for hold
something
choice="" #hold for user's
choose
#see in the .bashrc if it has the right path
#if no then add the new path
Hold=`grep "PATH" "$HOME/.bashrc"|grep $PWDPath`
echo "#######$Hold#######"
if [ -z $Hold ];then #if the
hold is empty
#add a new path to the $PATH
echo -e
"PATH=$PATH:$PWDPath /n export
PATH">>$HOME/.bashrc
source
$HOME/.bashrc #make the
chtoange valued
fi
echo "MList---ad|de|lo|ra|se|list|lab|q"
echo "ad --add new item to the play list"
echo "de --delete one or more items in the play list"
echo "lo --play the music in the list in loop style"
echo "ra --play the music in the list in random style"
echo "se --play the music by select one item"
echo "list --to see in the music list"
echo "lib --to see in the music library"
echo "q --quit"
read choice
while [ $choice!="q" ]
do
case $choice in
ad|Ad)
add
;;
de|De)
delete
;;
lo|Lo)
loopPlay
;;
ra|Ra)
randomPlay
;;
se|Se)
selectPlay
;;
list|List)
cat $MusicPath/ListLib|more
;;
lab|Lab)
ls $MusicPath|more
;;
q|Q)
exit 0
;;
*)
echo "Illegel input enter again!"
;;
esac
echo "MList---ad|de|lo|ra|se|list|lab|q"
read choice
done
shell脚本学习笔记(二)