Case of multi-branch statement, and shift use

Source: Internet
Author: User
Tags case statement

Sometimes a long if statement makes a program very difficult to read, and with a case, you can make the case statement more concise

The case syntax is in the following format:

Case expression inpattern1) suite1;; PATTERN2) Suite2;; ... patternn) Suiten;; *) Other_suite;; Esac

Patterns can be used for each pattern in case:
A|b:a or B
*: matches any character of any length;
?: matches any single character;
[-]: Range matching


Examples of Use:

Write a script that accepts this format
script.sh {Start|stop|restart|status}
1, if start, then create/VAR/LOCK/SUBSYS/SCRIPT.SH, show the success of the start;
2. If the parameter is stop, delete the/var/lock/subsys/script.sh, and the display stops successfully.
3, if restart, then delete, then create, display success;
4, if the status, if the file exists, then display running, otherwise, the display stopped

#!/bin/bashscript=$0lockfile=/var/lock/subsys/script.sh[ $# -lt 1 ] &&  echo  "usage:  $script  {start|stop|restart|staus}"  && exit 6case $1  in  start)         touch  $lockfile          echo  "$script  service start"          ;;   stop)         rm -f  $lockfile          echo  "$script  service stop"          ;;   restart)         rm -f  $lockfile          touch  $lockfile         echo   "$scrtip  service restart"         ;;   status)         if [ -f  $lockfile  ];then           echo  "$script  service is running"          else          echo   "$script  service is stop"         fi         ;;   *)         echo  "usage:  $script  {start|stop| Restart|staus} "                 ;; Esac


Write a simple script
1, prompt the user to enter an arbitrary character;
2, can judge this character is a number, letter or special character;

#!/bin/bash#while true;doread-p "Enter a char:" Char [["$char" = = "Quit"]] && exit 8case $char in [1-9])  echo "$char is digit";;        [A-z])   echo "$char is letter";; *) echo "Special" Esacdone


Write a script that can be packaged and backed up in the/etc/directory, with a backup location of/backup/etc-date. suffix
1. Display the following menu to the user:
XZ) XZ Compress
GZIP) gzip Compress
BIP2) bzip2 Compress
2, according to user-specified compression tool using tar packaging compression;
3, the default is XZ; input errors require the user to re-enter;

#!/bin/bash#dir= "/etc/*" bak= "/backup/etc-" [ -d backup ] | |  MKDIR /BACKUPCAT << EOFXZ)  xz compressgzip)  gzip compressbzip2)  bzip2 compressEOFwhile true;doread -p  "please enter a tool: "  toolstools=${tools:-xz}case  $tools  in  xz)          backuptools=J        end=xz         break        ;;   gzip)         backuptools=z         end=gz        break         ;;   BZIP2)         backuptools=j         end=bz2        break        ;;   *)         echo  "Wrong option,please enter  again "Esacdone tar -c${backuptools}f ${bak} ' date +%f-%t '. $end   $dir


Write a script to complete the following functions
Note: This script can create an alias for the specified network card, specify the address; Use format: Mkethalias.sh-v|--verbose-i|--interface EthX
The 1|-i option is used to specify the NIC;
2, if the network card exists: in the command line, please specify an alias;
3, let the user specify IP and mask;
4. The user can use the-V or--verbose option at the same time: if it is used, the configuration result is displayed after the configuration is completed; otherwise, it is not displayed;

#!/bin/bash#debug=0[ $# -lt 1 ] && echo  "usage: $0 {-v|-- VERBOSE -I|--INTERFACE ETHX} "WHILE [ $# -GE 1 ];d o case $1  In   -v|--verbose)         debug=1         shift 1 ;;    -i|--interface)         interface=$2         shift 2 ;;     *)         echo  "usage: $0 {-v|- -VERBOSE -I|--INTERFACE ETHX} "        shift $*         ;;  esacdone#echo  "$interface   $debug" if ! ip link show  $interface   &> /dev/null;then        echo  "NetworkCard no such "        exit 6firead -p "  enter network_alias:  " network_aliasread -p " PLEASE ENTER IPADDR:   " IPaddrread -p " please enter netmask:  " netmaskip addr add  dev  $interface  ${IPaddr}/${netmask} label  $network _alias[  "$debug"  -eq  1 ] && ip addr show  $interface  | |  echo  "Setup successfuly"



This article is from "Rookie Diary" blog, please make sure to keep this source http://zkxfoo.blog.51cto.com/1605971/1754613

Case of multi-branch statement, and shift use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.