case语句的语法格式:
case expression in
pattern1)
suite1
;;
pattern2)
suite2
;;
...
patternn)
suiten
;;
*)
other_suite
;;
esac
一、编写一个测试脚本,名字为myservice.sh
根据输入的选项,创建相应的文件
#/bin/bash
#
myService=`basename $0`
lockFile="/var/lock/subsys/$myService"
[ $# -lt 1 ] && echo "Usage: $myService {start|stop|restart|status}" && exit 4
case $1 in
'start')
touch $lockFile
echo "Starting $myService OK"
;;
'stop')
rm -f $lockFile
echo "Stopping $myService OK"
;;
'restart')
rm -f $lockFile
touch $lockFile
echo "Restarting $myService OK"
;;
'status')
if [ -f $lockFile ];then
echo "$myService is runing"
else
echo "$myService is stopped"
fi
;;
*)
echo "Usage: $myService {start|stop|restart|status}"
exit 3
esac
执行脚本:
[root@192 src]# ./myservice.sh
Usage: myservice.sh{start|stop|restart|status}
[root@192 src]# ./myservice.sh status
myservice.sh is stopped
[root@192 src]# ./myservice.sh start
Starting myservice.sh OK
[root@192 src]# ./myservice.sh status
myservice.sh is runing
[root@192 src]# ./myservice.sh stop
Stopping myservice.sh OK
[root@192 src]# ./myservice.sh restart
Restartingmyservice.sh OK
二、练习:写一个脚本
1、提示用户输入一个任意字符
2、能判断此字符是数字、字母或特殊字符
vim user.sh
#!/bin/bash
#
while true;do
read -p " Enter a char: " char
[[ "$char" == 'quit' ]]&& break
case $char in
[a-z])
echo "word"
;;
[0-9])
echo "digital"
;;
*)
echo "special"
;;
esac
done
脚本执行结果:
[root@192 src]# ./user.sh
Enter a char: 9
digital
Enter a char: a
word
Enter a char: >
special
Enter a char: quit
[root@192 src]#
三、练习:写一个脚本,能对/etc/目录进行打包备份,备份位置为/backup/etc-日期.后缀
1、显示如下菜单给用户:
xz) xz compress
gzip) gzip compress
bip2) bzip2 compress
2、根据用户指定的压缩工具使用tar打包压缩
3、默认为xz:输入错误则需要用户重新输入
[root@192 src]# cat backup.sh
#/bin/bash
#
[ -d /backup ] || mkdir /backup
cat << EOF
Please choose a compress tool:
xz) xz compress
gzip) gzip compress
bzip2) bzip2 compress
EOF
while true; do
read -p "Your option: " option
option=${option:-xz}
case $option in
xz)
compressTool='J'
suffix='xz'
break ;;
gzip)
compressTool='z'
suffix='gz'
break ;;
bzip2)
compressTool='j'
suffix='bzip2'
break ;;
*)
echo "wrong option."
;;
esac
done
tar ${compressTool}cf /backup/etc-`date+%F-%H-%M-%S`.tar.$suffix /etc/*
脚本执行结果:
[root@192 src]# ./backup.sh
Please choose a compress tool:
xz) xz compress
gzip) gzip compress
bzip2) bzip2 compress
Your option: xz
tar: 从成员名中删除开头的“/”
[root@192 src]# cd /backup/
[root@192 backup]# ls
etc-2016-08-13-22-46-26.tar.xz
可以看到已经按照要求生成相应的备份文件。
四、练习:写一个脚本,完成如下功能
说明:此脚本能够为指定网卡创建别名、指定地址;使用格式:mkethalias -v| --verbose -i ethx
1、-i选项用于指定网卡
2、如果网卡存在:在命令行,请用户指定一个别名
3、让用户指定IP和掩码
4、用户可以同时使用-V或--verbose选项:如果使用了,显示配置结果:否则,则不予显示
vim scriptargs.sh
#/bin/bash
#
debug=0
while [ $# -ge 1 ];do
case $1 in
-i|--interface)
ethcard="$2"
shift 2
;;
-v|--verbose)
debug=1
shift
;;
*)
echo "Wrong options arguments."
echo "Usage: `basename $0` [-v|--verbose] -i|--interfaceInterface"
shift $#
;;
esac
done
#echo "Interface: $ethcard, VerboseFlag: $debug "
! ifconfig $ethcard &> /dev/null&& echo "No this interface ..." && exit 3
read -p "Enter an alias: "ethAlias
read -p "Enter IP: " ipAddr
read -p "Mask: " netMask
ifconfig $ethAlias $ipAddr netmask $netMask
[ $debug -eq 1 ] && ifconfig$ethAlias
执行结果:
[root@192 src]# vim scriptargs.sh -i eth0
[root@192 src]# ./scriptargs.sh -i eth0
Enter an alias: eth0:2
Enter IP: 192.168.1.5
Mask: 255.255.255.0
[root@192 src]# ifconfig
eth0 Link encap:Ethernet HWaddr00:0C:29:D9:A2:5D
inet addr:192.168.10.102 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fed9:a25d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16489 errors:0 dropped:0 overruns:0 frame:0
TX packets:8692 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1345332 (1.2 MiB) TXbytes:945356 (923.1 KiB)
eth0:2 Link encap:Ethernet HWaddr00:0C:29:D9:A2:5D
inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0(0.0 b)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。