一、语法格式
case 变量引用(${}) in
value1) #变量的值是value1,则执语句1,语句2,······
语句1
语句2
······
;; #每个分支结束时都需要以双分号结尾
value2) #变量的值是value2,则执语句3,语句4,······
语句3
语句4
······
;;
value3) #变量的值是value3,则执语句5,语句6,······
语句5
语句6
······
;;
*) #其他任意情况,则执行语句7,语句8,······;支持字符通配globbing,*为任意长度的任意字符;支持star|Star)这类格式;不支持正则表达式
语句7
语句8
······
;;
esac #语句以esac结束
二、练习
示例1:写一个脚本,能接受参数gzip、bzip2或xz,而后能将/etc/目录归档备份至/backup目录,并以参数指定的形式压缩存放;文件名称包含脚本执行时刻的时间
#!/bin/bash
# Date: 2015-04-12
# Description:
# Version: 1.0
if [ ! -e /backup ];then #这里可以用[ -d /backup ] || mkdir /backup 替代
mkdir /backup
fi
Command=$1
if [ -z $1 ]; then #如果脚本给定的是空值,则默认输入gzip
Command=gzip
fi
if [ $Command == gzip ];then
tar czfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.gz /etc/
[ $? -eq 0 ] && echo "Backup etc successfully.(gzip)"
elif [ $Command == bzip2 ];then
tar cjfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.bz2 /etc/
[ $? -eq 0 ] && echo "Backup etc successfully.(bzip2)"
elif [ $Command == xz ];then
tar cJfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.xz /etc/
[ $? -eq 0 ] && echo "Backup etc successfully.(xz)"
else
echo "Usage:`basename $0`{gzip | bzip2 | xz }."
exit 5
fi
练习1:用case语句完成示例1要求
脚本1:
#!/bin/bash
# Date: 2015-04-12
# Description:
# Version: 1.0
if [ ! -e /backup ];then #这里可以用[ -d /backup ] || mkdir /backup 替代
mkdir /backup
fi
Command=$1
if [ -z $1 ]; then #如果脚本给定的是空值,则默认输入gzip
Command=gzip
fi
case $Command in
gzip)
tar czfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.gz /etc/
[ $? -eq 0 ] && echo "Backup etc successfully.(gzip)"
;;
bzip2)
tar cjfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.bz2 /etc/
[ $? -eq 0 ] && echo "Backup etc successfully.(bzip2)"
;;
xz)
tar cJfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.xz /etc/
[ $? -eq 0 ] && echo "Backup etc successfully.(xz)"
;;
*)
echo "Usage:`basename $0`{gzip | bzip2 | xz }."
exit 5
;;
esac
脚本2:
#!/bin/bash
# Date: 2015-04-12
# Description:
# Version: 1.0
if [ ! -e /backup ];then #这里可以用[ -d /backup ] || mkdir /backup 替代
mkdir /backup
fi
Command=$1
if [ -z $1 ]; then #如果脚本给定的是空值,则默认输入gzip
Command=gzip
fi
case $Command in
gzip)
tar czfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.gz /etc/
Ret=$?
;;
bzip2)
tar cjfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.bz2 /etc/
Ret=$?
;;
xz)
tar cJfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.xz /etc/
Ret=$?
;;
*)
echo "Usage:`basename $0`{gzip | bzip2 | xz }."
exit 5
;;
esac
[ $Ret -eq 0 ] && echo "Backup etc successfully.($Command)" #通过每个循环分支的返回值
示例2:写一个脚本:可以接受一个参数,其使用形式如下:
script.sh {start|stop|restart|status}
如果参数为start,创建空文件/var/lock/subsys/script,并显示“Starting script successfully.”;
如果参数为stop,则删除文件/var/lock/subsys/script,并显示“Stop script finished.”;
如果参数为restart,则删除文件/var/lock/subsys/script后重新创建,并显示“Restarting script successfully.”;
如果参数为status,那么:
如果/var/lock/subsys/script文件存在,则显示为“script is running.”
否则,则显示为“script is stopped.”
其它任何参数:则显示“script.sh {start|stop|restart|status}”
#!/bin/bash
# Date: 2015-04-13
# Author: ArvinLau
# Description:
# Version: 1.0
FileName=`basename $0`
FilePath="/var/lock/subsys/$FileName" #变量替换,使用双引号
if [ $# -lt 1 ]; then
echo "Usage:$0 {start | restart | stop | status}"
exit 5
fi
if [ $1 == start ]; then
touch $FilePath
echo "Starting $FileName successfully."
elif [ $1 == stop ]; then
rm -f $FilePath
echo "Stop script finished."
elif [ $1 == restart ]; then
rm -f $FilePath
touch $FilePath
echo "Restarting script successfully."
elif [ $1 == status ]; then
if [ -e $FilePath ]; then
echo "Script is running."
elif [ ! -e $FilePath ]; then
echo "Script is stopped."
fi
else
echo "Usage:$0 {start | restart | stop | status}"
exit 7
fi
练习2:用case语句完成示例2的要求
#!/bin/bash
# Date: 2015-04-13
# Author: ArvinLau
# Description:
# Version: 1.0
FileName=`basename $0`
FilePath="/var/lock/subsys/$FileName" #变量替换,使用双引号
if [ $# -lt 1 ]; then
echo "Usage:$0 {start | restart | stop | status}"
exit 5
fi
case $1 in
start)
touch $FilePath
echo "Starting $FileName successfully." ;;
stop)
rm -f $FilePath &> /dev/null
echo "Stop script finished." ;;
restart)
rm -f $FilePath &> /dev/null
touch $FilePath
echo "Restarting script successfully." ;;
status)
if [ -e $FilePath ]; then
echo "Script is running."
else
echo "Script is stopped."
fi
*)
echo "Usage:$0 {start | restart | stop | status}"
exit 7 ;;
esac
练习3:判断当前Linux发行版是RedHat,Fedora,CentOS还是其他,
方法:取得/etc/issue文件第一行的第一个单词后进行比较
#!/bin/bash
# Date: 2015-04-13
# Author: ArvinLau
# Description:
# Version: 1.0
Name=`head -1 /etc/issue | cut -d' ' -f1`
case $Name in
CentOS)
echo "The system is $Name." ;;
Fedora)
echo "The system is $Name." ;;
RedHat)
echo "The system is $Name." ;;
*)
echo "The system is $Name or Unknown system."
;;
esac
来源:oschina
链接:https://my.oschina.net/u/4298883/blog/4317864