shell

shell.2.7 bash脚本编程(case语句)

 

case语句语法结构:

case支持glob风格的通配符:
* :  任意长度的任意字符;
? :  任意单个字符;
[] :  范围内的任意单个字符;
a|b :  a或b;

case $VARAIBLE in	
PAT1)
  分支1
  ;;
PAT2)
  分支2
  ;;
...
*)
  分支n
  ;;
esac

 

脚本示例1:

显示一个菜单给用户;
cpu) displsy cpu information;
mem) displsy memory information;
disk) displsy disks information;
quit) quit;

要求,提示用户给出自己的选择;正确的选择则给出相应的信息,否则提示重新选择正确的选项;

#!/bin/bash
#

cat << EOF
cpu)displsy cpu information;
mem)displsy memory information;
disk)displsy disks information;
quit)quit;
------------------------------
EOF

read -p "choses you option:" option


while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ]; do
  echo
cat << EOF
cpu)displsy cpu information;
mem)displsy memory information;
disk)displsy disks information;
quit)quit;
--------------------------------
EOF
  echo 
  echo "just one of the four arg."
  read -p "choses you option:" option
done


if [ "$option" == "cpu" ]; then
  lscpu
elif [ "$option" == "mem" ]; then
  free -m
elif [ "$option" == "disk" ]; then
  fdisk -l /dev/sd[a-z]
else
  echo "quit"
  exit 0
fi
#!/bin/bash
#

cat << EOF
cpu)displsy cpu information;
mem)displsy memory information;
disk)displsy disks information;
quit)quit;
------------------------------
EOF

read -p "choses you option:" option


while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ]; do
  echo
cat << EOF
cpu)displsy cpu information;
mem)displsy memory information;
disk)displsy disks information;
quit)quit;
--------------------------------
EOF
  echo 
  echo "just one of the four arg."
  read -p "choses you option:" option
done


case $option in
cpu)
  lscpu
  ;;
mem)
  free -m
  ;;
disk)
  fdisk -l /dev/[hs]d[a-z]
  ;;
*)
  echo "quit."
  exit 0
esac

示例2:
写一个服务框架脚本;其中有个文件,$lockfile,其值为’/var/lock/subsys/SCRIPT_NAME’;
此脚本可接受start,stop,restart,status四个参数之一;
如果参数非此四个,则提示使用帮助后退出;
start状态时,创建lockfile,并显示启动;
stop状态时,删除lockfile,并显示停止;
restart状态时,先删除此文件,然后再创建,并提示重启完成;
status状态时,如果文件存在,则显示running,如果不存在,则显示为stopped;

#!/bin/bash
#

lockfile=/var/lock/subsys/$0

if [ $# -lt 1 -o $# -gt 1 ]; then
  echo "$0 's arg : {start|stop|restart|status}."
  exit 22
fi

b_name=$(basename $0)
blockfile=/var/lock/subsys/$b_name

case $1 in
start)
  if [ -f $lockfile ]; then
    echo "file is already exitses. it is started."
  else
    touch $lockfile
    [ $? -eq 0 ] && echo "start:file is created."
  fi
  ;;
stop)
  if [ -f $lockfile ]; then
    rm -rf $lockfile
    [ $? -eq 0 ] && echo "stop:file is delete."
  else
    echo "it is already stop."
  fi
  ;;
restart)
  if [ -f $lockfile ]; then
    rm -rf $lockfile
    touch $lockfile
    [ $? -eq 0 ] && echo "restart:file is already exitses.restart now."
  else
    touch $lockfile
    [ $? -eq 0 ] && echo "restart 2 : file is no exitses.create now..."
    echo "file create finished."
  fi
  ;;
status)
  if [ -f $lockfile ]; then
    echo "running : start status."
  else
    echo "stopped : stop status."
  fi
  ;;
*)	
  echo "$0 's arg : {start|stop|status|restart}"
  echo "quit"
  exit 11
  ;;
esac

 

Leave a Reply

Your email address will not be published. Required fields are marked *