---
title: "我使用过的Linux命令之case - Bash的多分支判断语句"
author: "codingstandards"
date: "2010-10-11T09:15:23Z"
tags: ["Bash", "F#", "Linux", "编程", "设计模式"]
categories: ["操作系统"]
slug: "linux-case-bash"
draft: false
lastmod: "2011-04-10T11:54:21Z"
id: 781017
url: "https://codingstandards.iteye.com/blog/781017"
---

# 我使用过的Linux命令之case - Bash的多分支判断语句

本文链接：[http://codingstandards.iteye.com/blog/781017](/blog/781017)    （转载请注明出处）

 

## 用途说明

case结构用于多种情况的条件判断。类似于其他编程语言中的switch/case语句，但从语法形式上讲，有很大的不同。

## 常用格式

case 字符串 in

    模式)

        语句

        ;;

    模式2 | 模式3)

         语句

         ;;

    \*)

         默认执行的 语句

         ;;

esac

提示：esac就是case反过来写。

## 使用示例

### 示例一

```
show_baby() {
    case $1 in
	    男 | M)
		    echo "是位小少爷"
			;;
		女 | F)
		    echo "是位小千金"
			;;
		*)
		    echo "有没有搞错"
			;;
	esac
}

show_baby 男
show_baby M
show_baby 女
show_baby F
show_baby 0
```

 

\[root@jfht ~]# show\_baby() {  
&gt;     case $1 in  
&gt;     男 | M)  
&gt;     echo "是位小少爷"  
&gt; ;;  
&gt; 女 | F)  
&gt;     echo "是位小千金"  
&gt; ;;  
&gt; \*)  
&gt;     echo "有没有搞错"  
&gt; ;;  
&gt; esac  
&gt; }  
\[root@jfht ~]#  
\[root@jfht ~]# show\_baby 男  
是位小少爷  
\[root@jfht ~]# show\_baby M  
是位小少爷  
\[root@jfht ~]# show\_baby 女  
是位小千金  
\[root@jfht ~]# show\_baby F  
是位小千金  
\[root@jfht ~]# show\_baby 0  
有没有搞错  
\[root@jfht ~]#

 

### 示例二 init脚本

```
#!/bin/bash
#
#       /etc/rc.d/init.d/atd
#
# Starts the at daemon
#
# chkconfig: 345 95 5
# description: Runs commands scheduled by the at command at the time \
#    specified when at was run, and runs batch commands when the load \
#    average is low enough.
# processname: atd

# Source function library.
. /etc/init.d/functions

test -x /usr/sbin/atd || exit 0

RETVAL=0

#
#       See how we were called.
#

prog="atd"

start() {
        # Check if atd is already running
        if [ ! -f /var/lock/subsys/atd ]; then
            echo -n $"Starting $prog: "
            daemon /usr/sbin/atd
            RETVAL=$?
            [ $RETVAL -eq 0 ] && touch /var/lock/subsys/atd
            echo
        fi
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc /usr/sbin/atd
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/atd
        echo
        return $RETVAL
}


restart() {
        stop
        start
}

reload() {
        restart
}

status_at() {
        status /usr/sbin/atd
}

case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload|restart)
        restart
        ;;
condrestart)
        if [ -f /var/lock/subsys/atd ]; then
            restart
        fi
        ;;
status)
        status_at
        ;;
*)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
esac

exit $?
exit $RETVAL
```

 

\[root@smsgw root]# ls -l /etc/init.d/atd  
\-rwxr-xr-x    1 root     root         1176 2004-10-06  /etc/init.d/atd  
\[root@smsgw root]# service atd  
Usage: /etc/init.d/atd {start|stop|restart|condrestart|status}

\[root@smsgw root]# service atd status  
atd (pid 1899) is running...

\[root@smsgw root]# service atd restart  
Stopping atd:                                              \[  OK  ]  
Starting atd:                                              \[  OK  ]  
\[root@smsgw root]#

 

## 问题思考

1\. case命令中的模式可以有哪些？

2\. 总结一下init脚本的写法。

## 相关资料

【1】Bash Guide for Beginners [7.3. Using case statements](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html)    [7.3. 使用case语句](http://blog.chinaunix.net/u2/61276/showart_477817.html)

【2】鸟哥的私房菜 [利用 case ..... esac 判斷](http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#case)

【3】Unix/Linux shell程序设计-&lt;控制结构&gt;-[case语句](http://hi.bccn.net/space-108519-do-blog-id-11561.html) - 日志 - neverTheSame - 编程中国

 

返回 [我使用过的Linux命令系列总目录](/blog/786653)