Linux Shell programming case statement

Source: Internet
Author: User

The case statement for Linux Shell programming is applicable to applications that require multiple branches. The format of the case Branch statement is as follows: case $ variable name in Mode 1) command sequence 1; Mode 2) command sequence 2; *) default command sequence executed ;; the esac case statement structure has the following characteristics: the end of a case line must be the word "in", and each mode must end with a right brace. Double Semicolon ";" indicates that the command sequence ends. In the matching mode, square brackets are used to indicate a continuous range, for example, [0-9]. vertical bars are used to represent or. The last "*)" indicates the default mode. When the previous modes cannot match the variable, the command sequence after "*)" is executed. Case statement example: the user enters a character from the keyboard, determines whether the character is a letter, number, or other character, and outputs the corresponding prompt information. #! /Bin/bashread-p "press some key, then press return:" KEYcase $ KEY in [a-z] | [A-Z]) echo "It's a letter. "; [0-9]) echo" It's a digit. "; *) echo" It's function keys, Spacebar or other ksys. "esac ###################################### ############### case word in [pattern [| pattern]...) list;]... esac case/esac standard usage is roughly as follows: case $ arg in pattern | sample) # arg in pattern or sample; pattern1) # Rg in pattern1; *) # default; esac arg is the parameter you have introduced. If the arg content conforms to the pattern project, the code below pattern will be executed, the code in this section ends with two semicolons. It can be noted that "case" and "esac" are symmetric. If you cannot remember them, simply turn "case" upside down. Example 1: paranoia #! /Bin/sh case $1 in start | begin) echo "start something"; stop | end) echo "stop something"; *) echo "Ignorant ";; esac executes [foxman @ foxman bash] # chmod 755 paranoia [foxman @ foxman bash] #. /paranoia Ignorant [foxman @ foxman bash] #. /paranoia start something [foxman @ foxman bash] #. /paranoia begin start something [foxman @ foxman bash] #. /paranoia stop something [foxman @ foxman bash] #. /para Noia end stop something Example 2: Many daemon of inetpanel are attached with a Shell Script for management, such as ndc for BIND, and apachectl for Apache. These management programs are all written using shell scripts. The following shows a shell script for inetd management. #! /Bin/sh case $1 in start | begin | commence)/usr/sbin/inetd; stop | end | destroy) killall inetd; restart | again) killall-HUP inetd; *) echo "usage: inetpanel [start | begin | commence | stop | end | destory | restart | again]"; esac Example 3: judge the system. Sometimes, the scripts you write may span several platforms, such as Linux, FreeBSD, and Solaris. However, there are more or less differences between platforms, sometimes you need to determine which platform is currently running. In this case, we can use uname to find out the system information. #! /Bin/sh SYSTEM = 'uname-S' case $ SYSTEM in Linux) echo "My system is Linux" echo "Do Linux stuff here... "; FreeBSD) echo" My system is FreeBSD "echo" Do FreeBSD stuff here... "; *) echo" Unknown system: $ SYSTEM "echo" I don't what to do... "; esac

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.