学习笔记-bash编程之case语句

case语法格式:

case expression in
pattern1)
    suit1
    ;;
pattern2)
    suit2
    ;;
....
patternen)
    suiten
    ;;
*)
    剩余条件
    ;;
esac

case中各模式可以使用模式:

  a|b:a或者b
  *:匹配任意长度的任意字符
  ?:匹配任意单个字符
  [-]:范围匹配 1-9 

写一个脚本:接受格式为script {start|stop|restart|status}
1.如果是start,则创建/var/lock/subsys/script.sh,显示启动成功
2.如果是stop,则删除/var/lock/subsys/script.sh,显示停止成功
3.如果是restart,则先删除,再创建,显示成功
4.如果status,如果文件存在,则显示running,否则显示stopped

for语句

#!/bin/bash

myService=`basename $0`
lockFile="/var/lock/subsys/$myService"

[ $# -ge 1 ] || echo "Usage `basename $0` {start|stop|restart|status}"

if [ "$1" == "start" ]; then
        touch $lockFile
        echo "Starting $myService OK"
elif [ "$1" == "stop" ];then
        rm -f $lockFile
        echo "Stopping $myService OK"
elif [ "$1" == "restart" ];then
        rm -f $lockFile
        touch $lockFile
        echo "Restaring $myService OK"
elif [ "$1" == "status" ];then
        if [ -f $lockFile ];then
                echo "$myService is running"
        else
                echo "$myService is stopped"
        fi
else
        echo "Usage: $myService {start|stop|restart|status}"
        exit 3
fi

case语句

#!/bin/bash

myService=`basename $0`
lockFile="/var/lock/subsys/$myService"

[ $# -ge 1 ] || echo "Usage `basename $0` {start|stop|restart|status}"

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 "Restaring $myService OK"
        ;;
"status")
        if [ -f $lockFile ];then
                echo "$myService is running"
        else
                echo "$myService is stopped"
        fi
        ;;
*)
        echo "Usage: $myService {start|stop|restart|status}"
        exit 3
        ;;
esac

练习:写一个简单脚本:
1,提示用户输入一个任意字符;
2,能判断此字数是数字,字母或特殊字符;


#/!bin/bash

while true; do
read -p "Enter a char: " char
[ "$char" == "quit" ] && break

case $char in 
[[:digit:]])
        echo "this is a number"
        ;;
[[:alpha:]])
        echo "this is a alpha"
        ;;
*)
        echo "this is a special"
        ;;
esac
done

练习:写一个脚本,能对/etc/命令进行打包备份,备份位置为/backup/etc-日期:后缀
1.显示如下菜单给用户
xz) xz compress
gzip)gzip compress
bizp) bzip2 compress
2.根据用户指定的压缩工具使用tar打包工具压缩
3.默认为xz

#!/bin/bash
[ -d  /backup ] || mkdir /backup
cat << EOF
xz) xz compress
gzip) gzip compress
bzip2) bzip2 compress
EOF

while true; do
        read -p "Enter your select: " select
        select=${select:-xz}
        case $select in
        xz)
                tar Jcf /backup/ect-`date "+%F-%H-%M-%S"`.tar.xz /etc/*
                break ;;
        gzip)
                tar zcf /backup/ect-`date "+%F-%H-%M-%S"`.tar.gz /etc/*
                break ;;
        bzip2)
                tar jcf /backup/ect-`date "+%F-%H-%M-%S"`.tar.bz2 /etc/*
                break ;;
        *)
                echo "wrong option:please retry input"
        esac
done

练习:写一个脚本,完成以下功能
说明:此脚本能为指定网卡创建别名,指定地址;使用格式:makethalias.sh -v | –verbose -i ethX
1.-i 选项指定网卡
2.如果网卡存在:在命令行,请用户指定一个别名
3,让用户指定IP个掩码
4.用户可以同时使用-v或-verbose选项;如果使用了,则在配置完成后,显示配置结果;否则,则不予显示;

#!/bin/bash

#!/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 or agrumnets."
                echo "Usage: `basename $0` [-v|--verbose] -i|--interface Interface"
                shift $#
                ;;
        esac
done

echo "Interface=$ethcard Verbose=$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

猜你喜欢

转载自blog.csdn.net/weixin_36209467/article/details/82502278