Use case ..... Esac judgment

Source: Internet
Author: User

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

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.