Case $ variable name in
"First variable content ")<= It is recommended that the content of each variable be enclosed in double quotation marks. The keyword is parentheses.
ProgramSegment
;;<= Two consecutive semicolons are used at the end of each category!
"Second variable content ")
Program Section
;
*)<= The Last variable content will be replaced *.
Other program segments that do not contain the first variable content or the second variable content
Exit 1
;;
Esac
In general, in the "case $ variable in" syntax, the "$ variable" has two methods:
Direct release:As mentioned above. sh variable "to directly give the content of the variable $1, which is also in/etc/init. d directory.
Interactive:You can use the READ command to input the variable content.
Here is an example:
#! /Bin/bash
# Show "hello" from $1... by using case... esac
Case $1 in
"Hello ")
Echo "Hello, how are you? "
;;
"")
Echo "you must input some parameters, ex> {$0 someword }"
;;
*)
Echo "usage $0 {Hello }"
;;
Esac
Let's look at the next example:
#! /Bin/bash
# This script only accepts the fllowing parameters: One, two, three.
echo "this program will print your selection! "
Read-P" input your choice: "Choice
case $ choice in
" one ")
echo" your choice is one. "
;< br>" two ")
echo" your choice is two. "
;< br>" three ")
echo" your choice is three. "
;;< br> *)
echo" Usage: $0 {one | two | three} "
;;
esac