esac
在上面这个
hello-3.sh
的案例当中,如果你输入『
sh hello-3.sh test
』来执行,
那么屏幕上就会出
现『
Usage hello-3.sh {hello}
』的字样,告知执行者½能够使用
hello
喔~
这样的方式对于需要某些
固定字符串来执行的变量内容就显的更加的方便呢!
这种方式你真的要熟悉喔!这是因为
早期系统
的很多服务的启动
scripts
都是使用这种写法的
(CentOS 6.x
以前
)
。
虽然
CentOS 7
已经使用
systemd
,不过仍有数个服务是放在
/etc/init.d/
目录下喔!例如有个名为
netconsole
的服务在该目录
下,
那么你想要重新启动该服务,是可以这样做的
(
请注意,要成功执行,还是得要具有
root
身份
才行!一般账号能执行,但不会成功!
)
:
/etc/init.d/netconsole restart
重点是那个
restart
啦!如果你使用『
less /etc/init.d/netconsole
』去查阅一下,就会看到他使用的是
case
语法,
并且会规定某些既定的变量内容,你可以直½下达
/etc/init.d/netconsole
,
该
script
就
会告知你有哪些后续½的变量可以使用啰~方便吧!
^_^
一般来说,使用『
case $
变量
in
』这个语法中,当中的那个『
$
变量
』大致有两种取得的方式:
.
直½下达式
:例如上面提到的,利用『
script.sh variable
』
的方式来直½给予
$1
这个变量的内容,这也
是在
/etc/init.d
目录下大多数程序的设计方式。
.
½互式
:透过
read
这个指令来让用户输入变量的内容。
这么说或许你的感受性还不高,好,我们直½写个程序来玩玩:让使用者能够输入
one, two, three
,
并且½用户的变量显示到屏幕上,如果不是
one, two, three
时,就告知使用者½有这三种选择。
[dmtsai@study bin]$
vim show123.sh
#!/bin/bash
# Program:
#
This script only accepts the flowing parameter: one, two or three.
# History:
# 2015/07
/17
VBird
First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "This program will print your selection !"
# read
-
p "Input your choice: " choice
#
暂时
取消,可以替
换
!
# case ${choice} in
#
暂时
取消,可以替
换
!
case ${1} in
#
现
在使用,可以用上面
两
行替
换
!
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;