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