-----闹钟的源码
(shell 脚本对格式的要求很严格,比如说if [ -z $Hold ] 要求每个字符之间都有空格,不然就会报错。)
#!/bin/bash
#use at to set a Alarm
MusicPath="/home/flora/Yalta.mp3" #where the music is placed
Time="" #the time entered to start the job
Run=1 #flag number to indicate the smooth running,1=="not complete the job"
Error="" #check the log to see if there is something wrong!
RunNum="" #stands for the ID of the running mplayer
TaskNum="" #stands for the job's num in the "at"
Read="" #hold the read-in command
PWDPath=`pwd` #show the current path
Hold="" #for hold something
#see in the .bashrc if it has the right path
#if no then add the new path
Hold=`grep "PATH" "$HOME/.bashrc"|grep $PWDPath`
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
fi
source $HOME/.bashrc #make the change valued
echo "start|stop|set|check"
echo "start --start a Alarm"
echo "stop --stop the clock music"
echo "set --change the clock time"
echo "check --check the setted job"
echo "quit --quit the program"
read Read #read from the user
while [ -n $Read ] #when Read is not empty
do
case $Read in
start|Start)
while [ $Run -eq "1" ]
do
echo "Please enter the time>>"
echo "(Time format--13:13 now+1minutes)"
read Time
#use mplayer to play the clock music,and save the system log to
#alarm.txt
touch alarm.txt
echo mplayer $MusicPath|at $Time >alarm.txt 2>&1
Error=`grep "error" alarm.txt`
rm alarm.txt #delet the hold file
#check the time format
if [ -z $Error ];then #if there is no error then exit
Run=0
exit 0
else
#when Error is not empty it means you have something wrong to do the job
echo "The time you entered is not correct!Enter again!"
Run=1
fi
done
;;
stop|Stop)
#RunNum indicates the ID of the mplayer
RunNum=`ps -ef|grep "mplayer"|sed -n 1p|awk '{print $2}'`
if [ -n $RumNum ];then
kill $RunNum #stop the mplayer
exit 0
else
echo "The clock is not started!"
echo -e "Enter /"start/" to set a clock!"
fi
;;
set|Set)
#check if the clock is set,Task
echo "There are such task will be excute:"
at -l
echo "Enter the job ID to change the Time"
read TaskNum
if [ -n $TaskNum ];then
atrm $TaskNum #delete the task
Read="start" #set a new clock
else
echo -e "No task still or dont't want to change!"
exit 1
fi
;;
check|Check)
#check if the clock is set,Task
echo "There such task bellow to be excute:"
at -l
exit 0
;;
quit|Quit)
exit 0
;;
*)
echo "start|stop|set|check"
echo "start --start a Alarm"
echo "stop --stop the clock music"
echo "set --change the clock time"
echo "check --check the setted job"
echo "quit --quit the program"
;;
esac
done
shell脚本学习笔记(一)