第七章实验(四):case

case语句:针对变量的不同取值,分别指向不同的命令序列。

变量值一般来自于位置变量,如$1,$2等,也可以来自于read生成的变量。

1.变量来自于read变量:

[root@ns bin]# cat key_hit_case.sh

#!/bin/bash

read -p "Please hit a key:" KEY

case $KEY in

[a-z]|[A-Z])

echo "your key is alph."

;;

[0-9])

echo "your key is num."

;;

*)

echo "your key may be is #!@ and so on."

;;

esac

clip_image002

clip_image004

2.变量来自于位置变量:

[root@ns bin]# cat prog_case.sh

#!/bin/bash

#chkconfig: - 98 10

#description: A script for case by linuxfan.cn.

case $1 in

start)

echo "hehe is starting."

sleep 2

;;

stop)

echo "hehe is stoping."

sleep 2

;;

restart)

$0 stop

$0 start

;;

*)

echo "Usage:$0 {start|stop|restart}"

exit 1

;;

Esac

clip_image006

clip_image008

[root@ns bin]#

3.系统服务脚本:

手写一边vsftpd系统脚本,并添加中文解释。

[root@ns bin]# vim vsftpd

#!/bin/bash

#

### BEGIN INIT INFO

# Provides: vsftpd

# Required-Start: $local_fs $network $named $remote_fs $syslog

# Required-Stop: $local_fs $network $named $remote_fs $syslog

# Short-Description: Very Secure Ftp Daemon

# Description: vsftpd is a Very Secure FTP daemon. It was written completely from

# scratch

### END INIT INFO

# vsftpd This shell script takes care of starting and stopping

# standalone vsftpd.

#

# chkconfig: - 60 50 ##设置允许级别和开、关机的启动顺序

# description: Vsftpd is a ftp daemon, which is the program \

# that answers incoming ftp service requests.

# processname: vsftpd

# config: /etc/vsftpd/vsftpd.conf

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

RETVAL=0

prog="vsftpd"

start() { ##编写start函数,函数将许多的操作集成

# Start daemons.

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 1

[ -x /usr/sbin/vsftpd ] || exit 1

if [ -d /etc/vsftpd ] ; then

CONFS=`ls /etc/vsftpd/*.conf 2>/dev/null`

[ -z "$CONFS" ] && exit 6

PROC_FAILED=0

for i in $CONFS; do

site=`basename $i .conf`

echo -n $"Starting $prog for $site: "

daemon /usr/sbin/vsftpd $i

RETVAL=$?

echo

if [ $RETVAL -eq 0 ] && [ ! -f /var/lock/subsys/$prog ]; then

touch /var/lock/subsys/$prog

elif [ $RETVAL -ne 0 ]; then

ps -FC vsftpd | grep "$i" > /dev/null

RETVAL=$?

if [ $PROC_FAILED -eq 0 ] && [ $RETVAL -ne 0 ]; then

PROC_FAILED=1

fi

fi

done

if [ $RETVAL -eq 0 ] && [ $PROC_FAILED -ne 0 ]; then

RETVAL=1

fi

else

RETVAL=1

fi

return $RETVAL

}

stop() {

# Stop daemons.

echo -n $"Shutting down $prog: "

killproc $prog

RETVAL=$?

echo

[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog

return $RETVAL

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

restart|reload)

stop

start

RETVAL=$?

;;

condrestart|try-restart|force-reload)

if [ -f /var/lock/subsys/$prog ]; then

stop

start

RETVAL=$?

fi

;;

status)

status $prog

RETVAL=$?

;;

*)

echo $"Usage: $0 {start|stop|restart|try-restart|force-reload|status}"

exit 1

esac

exit $RETVAL

:wq

clip_image010

[root@ns bin]#

[root@ns bin]# cp vsftpd /etc/init.d/vsftpd ##复制vsftpd脚本到启动目录下

[root@ns bin]# chmod +x /etc/init.d/vsftpd ##授权

[root@ns bin]# chkconfig --add vsftpd ##添加系统服务

[root@ns bin]# chkconfig vsftpd on ##设置开机启动

[root@ns bin]# chkconfig --list vsftpd

vsftpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

[root@ns bin]# /etc/init.d/vsftpd restart ##重启vsftpd验证是否成功

关闭 vsftpd: [确定]

为 vsftpd 启动 vsftpd: [确定]

[root@ns bin]#

clip_image012