Linux系统中的shell编程——case语句和函数

Linux系统中的shell编程——case语句和函数

一.case语句

1.工作原理: case语句为多重匹配语句,如果匹配成功,执行相匹配的命令

语法结构:

语法结构
case var in
pattern 1)
command 1
;;
pattern 2)
command 2
*)
;;
command 3
;;
esac

2.应用实例

(1)服务状态管理 

         需求: 当给程序传入start、stop、restart三个不同参数时分别执行相应命令

(2)模拟一个多任务维护界面;当执行程序时先显示总菜单,然后进行选择后做相应维护监控操作

            ***************请选择**********
                 h 显示命令帮助
                 f 显示磁盘分区
                 d 显示磁盘挂载
                 m 显示内存信息
                 u 查看系统负载
                 q 退出程序
         *********************************

 10 #!/bin/bash
 11 function menu()
 12 {
 13         echo "h show help"
 14         echo "f show disk information"
 15         echo "d show disk mount information"
 16         echo "m show storage information"
 17         echo "u show system load"
 18         echo "q quit "
 19 }
 20 
 21 read -p "please input a choice(h|f|d|m|u|q):" choice
 22 case $choice in
 23         h)
 24                 menu
 25                 echo "show help"
 26                 ;;
 27         f)
 28                 blkid | cut -d ' ' -f 1
 29                 echo "disk information"
 30                 ;;
 31         d)
 32                 df -h
 33                 echo "mount information"
 34                 ;;
 35         m)
 36                 free -m
 37                 echo "storage information"
 38                 ;;
 39         u)
 40                 uptime
 41                 echo "system load"
 42                 ;;
 43         q)
 44                 exit
 45                 echo "quit"
 46                 ;;
 47 esac

 

(3)判断用户输入的字符串,如果是"hello",则显示"world";如果是"world",则显示"hello",否则提示"请输入hello或者world,谢谢!"

 10 #!/bin/bash
 11 read -p "Please a string:" string
 12 case $string in
 13         hello)
 14                 echo "world"
 15                 ;;
 16         world)
 17                 echo " hello"
 18                 ;;
 19         *)
 20                 echo "please input hello or world,thank you"
 21                 ;;
 22 esac

二.函数

1.什么是函数?
shell中允许将一组命令集合或语句形成一段可用代码,称为shell函数;给这段代码起个名字称为函数名,后续可以直接调用该段代码的功能

2.函数定义

3.函数的返回值

       return可以结束一个函数。类似于循环控制语句break(结束当前循环,执行循环体后面的代码)。 return默认返回函数中最后一个命令状态值,也可以给定参数值,范围是0-256之间。

4.实用案例:有颜色的字符串输出  以后标注字体颜色可以直接调用该函数

11 RED="\033[31m"
 12 GREEN="\033[32m"
 13 YELLOW="\033[33m"
 14 END="\033[0m"
 15 print_color_string(){
 16         color=$1
 17         string=$2
 18         case $color in
 19                 red)
 20                         echo -e "$RED $string $END"
 21                         ;;
 22                 green)
 23 
 24                         echo -e "$GREEN $string $END"
 25                         ;;
 26                 yellow)
 27 
 28                         echo -e "$YELLOW $string $END"
 29                         ;;
 30                 *)
 31                         echo $string
 32                         ;;
 33         esac
 39 print_color_string 'red' 'hello world'
 40 print_color_string 'green' 'hello world'
 41 print_color_string 'yellow' 'hello world'

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值