码迷,mamicode.com
首页 > 系统相关 > 详细

Shell 编程基础之 Select 练习

时间:2014-10-28 19:51:19      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   sp   div   on   art   

一、语法

select 变量 in con1 con2 con3    # 自动列出 con1,con2,con3 的选择菜单
do
    #执行内容
break    # select本身就是一个循环,break是当选择后,就跳出循环
done

二、练习

  1. select + case 模拟 Linux 启动脚本
    function programstatus(){
      if [ "$1" -eq 0 ]; then
        echo "* program is running"
      else
        echo "* program has stopped"
      fi
    }
    
    status=0 #0: start; 1:stop
    select p in "start" "stop" "status" "restart" "*" ""
    do
    case "$p" in
    "start")
      status=0
      programstatus $status
      ;;
    "stop")
      status=1
      programstatus $status
      ;;
    "status")
      programstatus $status
      ;;
    "restart")
      if [ "$status" -eq 0 ]; then
        status=1
        echo "* program has stopped"
      fi
      status=0
      echo "* program is running"
      ;;
    "")
      break
      ;;
    *)
      echo "Plz input [start|stop|status|restart]"
       ;;
    esac
    done
    user@ae01:~$ ./test.sh
    1) start
    2) stop
    3) status
    4) restart
    5)
    #? 1
    * program is running
    #? 2
    * program has stopped
    #? 3
    * program has stopped
    #? 4
    * program is running
    #? 5
    user@ae01:~$

     

Shell 编程基础之 Select 练习

标签:style   blog   io   color   ar   sp   div   on   art   

原文地址:http://www.cnblogs.com/tannerBG/p/4057422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!