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
    评论
1. 什么是shell脚本Shell脚本是一种编程语言,它是在Unix和Linux操作系统中使用的一种脚本语言。它可以帮助用户自动化任务,并且可以运行一系列命令。 Shell脚本通常以.sh扩展名结尾。 2. 为什么要学习shell脚本学习shell脚本可以帮助你自动化任务,提高工作效率。Shell脚本也可以帮助你编写小工具,方便你自己或其他人使用。Shell脚本还可以帮助你更好地理解Linux和Unix操作系统。 3. 如何编写一个简单的shell脚本? 首先,在命令行中输入命令nano test.sh,创建一个名为test.sh的文件。然后,在文件中输入以下内容: #!/bin/bash echo "Hello World" 接着,按下Ctrl + X,然后按下Y,最后按下Enter,保存并退出文件。接下来,您需要在命令行中输入以下命令: chmod +x test.sh ./test.sh 这将使test.sh文件可执行,并运行脚本。在命令行中,您应该看到输出“Hello World”。 4. shell脚本中的注释是什么? 注释是用于向脚本中添加说明和文档的文本。在Shell脚本中,注释以“#”开头。注释不会被脚本解释器执行,但可以帮助其他人更好地理解脚本。 5. 如何在shell脚本中使用变量? 变量是一个用于存储值的占位符。在Shell脚本中,您可以使用以下语法来定义变量: my_variable="Hello World" 您可以使用echo命令来输出变量的值: echo $my_variable 6. 如何在shell脚本中使用条件语句? 在Shell脚本中,您可以使用条件语句来执行基于条件的操作。以下是一个示例条件语句: if [ $my_variable = "Hello World" ] then echo "The variable contains Hello World" else echo "The variable does not contain Hello World" fi 7. 如何在shell脚本中使用循环? 在Shell脚本中,您可以使用for循环或while循环来执行重复的操作。以下是一个示例for循环: for i in 1 2 3 4 5 do echo $i done 以上代码将输出数字1到5。 8. 如何在shell脚本中使用函数? 在Shell脚本中,您可以使用函数来组织和重复使用代码。以下是一个示例函数: function say_hello { echo "Hello World" } 您可以通过以下方式调用函数: say_hello 9. 如何从shell脚本中读取用户输入? 在Shell脚本中,您可以使用read命令来从用户那里读取输入。以下是一个示例: echo "What is your name?" read name echo "Hello $name" 以上代码将提示用户输入他们的名字,并输出“Hello”后跟用户的名字。 10. 如何在shell脚本中使用命令行参数? 在Shell脚本中,您可以使用$1、$2、$3等变量来访问命令行参数。例如,以下是一个示例脚本,它接受两个命令行参数并将它们相加: #!/bin/bash sum=$(($1 + $2)) echo "The sum of $1 and $2 is $sum" 您可以使用以下命令来运行脚本并传递两个参数: ./test.sh 2 3 以上代码将输出“The sum of 2 and 3 is 5”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值