全部博文(930)
分类:
2008-08-31 19:46:40
代码: |
#!/bin/bash
myplayer (){ #后台调用播放音乐函数 if which mplayer >/dev/null;then exec mplayer "$1" >&/dev/null #用exec调用替换进程 fi } rmNumfile(){ if [ -f .num ];then rm .num fi } rmMenufile(){ if [ -f .menu ];then rm .menu fi } #define some signals sigNext=25 sigPre=26 sigExit=27 sigNum=28 # #you can add some more #以下为操作处理函数 next(){ # kill -$sigNext $1 # echo "kill -$sigNext $1" echo "下一个..." kill -$sigNext $1 } pre(){ # echo "kill -$sigPre $1" echo "上一个..." kill -$sigPre $1 } myexit(){ echo "接收到退出信号,请稍等..." kill -$sigExit $1 sleep 2 echo "主进程退出!" } csnum(){ echo "选中第$(cat .num) 首。" # echo "`expr $1 - 1`" > .num kill -$sigNum $2 } getkey(){ local state substate oldstate state=1 oldstate=$state rmNumfile rmMenufile # echo mypid=$$ #主进程ID getkeypid=$1 trap "rmNumfile; rmMenufile; kill -$sigexit $getkeypid;" 2 3 while true do read -s -n 1 ans # echo $ans case "$ans" in [nN]|[jJ]) next $getkeypid ;; [mM]|[kK]) pre $getkeypid ;; q|Q) myexit $getkeypid exit 0 ;; [0-9]) if [ "$state" == "1" ];then echo -n "$ans" >> .num csnum $ans $getkeypid state="$oldstate" elif [ "$state" == "2" ];then echo -n "$ans" >>.num state=`expr "$state" - 1` elif [ "$state" -eq "3" ];then echo -n "$ans" >.num state=`expr "$state" - 1` else echo "an error accur" fi ;; a|A) if [ "$oldstate" -lt "3" ];then oldstate=`expr "$oldstate" + 1` echo "now level $oldstate" else echo " now level $oldstate" fi state=$oldstate rmNumfile ;; s|S) if [ "$oldstate" -gt "1" ];then oldstate=`expr "$oldstate" - 1` echo "now level $oldstate" else echo "now level $oldstate" fi state=$oldstate rmNumfile ;; d|D) echo "now level $oldstate" ;; l|L) clear cat .menu ;; c|C) clear ;; *) echo no such op;; esac done } NothingPlay(){ echo There is nothing mp3,please input Q to exit! } getpid(){ #暂时没用到的函数 ppid=$(ps aux|grep "myplayer" |grep -v "grep") if [ "$ppid" == "" ];then return -1; fi pid=$(echo $ppid |awk '{print $2}') return $pid } killpid(){ kill -9 "$1" >&/dev/null return "$?" } myUsage(){ cat << EOF Usage: $APP_NAME [MusicDIRCTORY] Start playing Music.(version $APP_VERSION) @GPL -h, --help display this help and exit some operations for example: a) level up s) level down :) d) show current level l) list menu n) next m) pre q) exit for more message,please send email to EOF } #信号处理部分 #do_sigNum(){ # if [ "$glNum" != "" ] # then # n=`expr $glNum + 0 ` #转换为数字 # else # n=1 #从头开始 # fi # killpid $mplayerpid #} runAskey(){ #后台守护进程 #定义本地变量 local line n playing; # 在此进行信号挂载 trap 'killpid $mplayerpid;rmNumfile;rmMenufile;exit 0;' $sigExit trap 'killpid $mplayerpid ;' $sigNext trap ' n=`expr "$n" - 2`;killpid $mplayerpid ;' $sigPre # trap ' n=`awk \' NR==1 {print $1;}\' .num`;killpid $mplayerpid;' $sigNum trap ' n=$((($(cat .num) - 1))) ;rmNumfile;killpid $mplayerpid;' $sigNum # echo $1 if [ ! -d "$1" ] then myUsage echo "please input Q to exit" exit 0 fi line=`ls $1|grep '.mp3' |wc -l` if [ "$line" == "0" ] then NothingPlay exit 0 fi n=1 ls $1|grep .mp3|awk '{print NR,$0}' | tee .menu while [ "$n" -le "$line" ] do playing=$(ls $1|grep .mp3|sed -n ''"$n"'p') echo "正在播放:$playing 下一首:$(ls $1|grep .mp3|sed -n ''"$((($n+1)))"'p')" if [ -f "$1"/"$playing" ];then #调整目录结构 myplayer "$1"/"$playing"& else if [ -f "$1""$playing" ];then myplayer "$1""$playing" & fi fi mplayerpid="$!" # echo "mplayerpid=$mplayerpid" 保存音乐进程ID wait $mplayerpid #等待音乐结束 sleep 2 n=`expr "$n" + 1` #播放下一首 echo "请等待..." done echo "播放完毕,按 ‘q‘以退出!" while : do sleep 10 done } APP_NAME="${0##*[\\/]}" APP_VERSION="0.0" directory="$1" if [ "$2" == "--show" ];then runAskey $directory else $0 "$1" --show & #启动后台守护进程 #echo "pid=$!" if [ "$1" != "-h" ];then if [ "$1" != "--help" ];then getkey $! #启动前台接收操作 fi fi fi |