shell脚本学习笔记(一)闹钟的源码

shell脚本学习笔记(一)

        -----闹钟的源码
(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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值