Use of case in shell scripts

Source: Internet
Author: User

The case selection statement in the shell script can be combined with the read instruction to achieve a good interactive response operation, case receives one or more parameters passed in by the read instruction, and then the case is selected according to the parameters.

The syntax for case is as follows

 Case  $Char  in         | c)                1                ;;          #每一个选择都以双;; End         | m)                 2                 ;;             *   )               #  * unmatched to match other values                                                                                                                                                                                  echo  "error"               ;; Esac           #case的结束语句是以esac End

The following combination of a simple small function, the script also uses the method of the function; Simply view system Information

#!/bin/BashEcho "Check system run status"Echo "Show Cpuinfo:c/c"Echo "Show Memery used:m/m"Echo "Show Disk use status:d/n"Echo "Show System user login:u/n"Echo "Show System load average:l/l"Echo "show System Ip address:i/i"Read_input () {Read-TTen-P"Please Input c/m/d/u/l/i:" Char}show_status () { Case$Char inchC|c)Cat/proc/cpuinfo |grep-o-i'Model name.*'                ;; M|m) Free-m;; D|d)DF-h;; U|u)W                ;; L|l) Top|Head-1|Cut-D" "-F One- the                ;; I|i)ifconfig|grep-O"[0-9.]\{7,\}"|Head-1                ;; * )                Echo "The characters you have entered is wrong. Hints"                ;;Esac} forIinch$(seq 1 Ten) DoRead_inputshow_statusif[$i-eqTen]; Then        Echo "the maximum number of times the query has been reached and the script exits;"fi Done

Look at the test results.

[Email protected] home]#./test.SHCheck system run Statusshow cpuinfo:c/C Show Memery used:m/m show Disk use Status:d/N Show System userLogin: u/n Show System load Average:l/lshow System Ip address:i/iplease Input C/m/d/u/l/i:the characters you have entered is wrong. Hintsplease Input C/m/d/u/l/i:m Total Used Freeshared buffers Cachedmem:1869        165       1703          0          +          --/+ Buffers/cache: the       1773Swap:1999          0       1999Please Input C/m/d/u/l/I:cmodel Name:intel (R) Core (TM) i5-3317UCPU @1. 70GHzmodel Name:intel (R) Core (TM) i5-3317UCPU @1. 70GHzplease Input C/m/d/u/l/i:

This allows you to interactively pass parameters, and you can set the number of selections through the loop, read-t limit wait for input length, etc.

Use of case in shell scripts

Related Article

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.