您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。