case $1 in
word1 )
action1
;;
word2)
action2
;;
........
*)
action_last
esac
实验1:服务的查看
9 #!/bin/bash
10 case $1 in
11 status)
12 systemctl $1 $2
13 ;;
14
15 start)
16 systemctl $1 $2
17 ;;
18
19 stop)
20 systemctl $1 $2
21 ;;
22
23 *)
24 echo inupt error
25 ;;
26 esac
执行脚本
[root@server210 mnt]# sh case.sh status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
Active: activating (start) since Fri 2018-03-09 07:21:52 EST; 30s ago
Main PID: 1967 (httpd)
CGroup: /system.slice/httpd.service
└─1967 /usr/sbin/httpd -DFOREGROUND
Mar 09 07:22:12 server210 httpd[1967]: AH00557: httpd: apr_sockaddr_info_ge...0
Mar 09 07:22:12 server210 httpd[1967]: AH00558: httpd: Could not reliably d...e
Hint: Some lines were ellipsized, use -l to show in full.
[root@server210 mnt]# sh case.sh start httpd
[root@server210 mnt]# sh case.sh status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
Active: active (running) since Fri 2018-03-09 07:22:32 EST; 3s ago
Main PID: 1967 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─1967 /usr/sbin/httpd -DFOREGROUND
├─1984 /usr/sbin/httpd -DFOREGROUND
├─1985 /usr/sbin/httpd -DFOREGROUND
├─1986 /usr/sbin/httpd -DFOREGROUND
├─1987 /usr/sbin/httpd -DFOREGROUND
└─1988 /usr/sbin/httpd -DFOREGROUND
Mar 09 07:22:12 server210 httpd[1967]: AH00557: httpd: apr_sockaddr_info_ge...0
Mar 09 07:22:12 server210 httpd[1967]: AH00558: httpd: Could not reliably d...e
Mar 09 07:22:32 server210 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.