【shell】Linux shell 之 case 详解

前端之家收集整理的这篇文章主要介绍了【shell】Linux shell 之 case 详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

总的来说,case是一个判断语句 ,比if更加容易理解一点。

case 语句格式

  1. case in 变量
  2. 1)
  3. 内容 ;;
  4. 2)
  5. 内容 ;;
  6. esac

注意:每个内容后面都需要添加 ;; ,可以跨行也可以同行写。

实例:根据用户输入的选择执行语句。

  1. #!/bin/bash -
  2. # 打印选择菜单
  3. cat <<EOF
  4. Option:
  5. 1) restart networking service.
  6. 2) start networking service.
  7. 3) stop networking service.
  8. *) exit.
  9. EOF
  10. read -p "Please enter a option:" number
  11.  
  12. # 使用case语句对参数进行判断
  13. case $number in
  14. 1)
  15. echo "The Networking service is restart,wait......" ;;
  16. 2)
  17. echo "The Networking service is start,wait......" ;;
  18. 3)
  19. echo "The Networking service is stop,wait......" ;;
  20. *)
  21. echo "exit." ;;
  22. esac

[root@XiaoPeng scripts]# bash case.sh
Option:
1) restart networking service.
2) start networking service.
3) stop networking service.
*) exit.
Please enter a option:1
The Networking service is restart,wait......

版权所有:arppinging

猜你在找的Bash相关文章