Shell case ESAC Statement "Go"

Source: Internet
Author: User
Tags case statement

This article was reproduced from: http://c.biancheng.net/cpp/view/7006.html

C Language Chinese network launched remedial classes, including "c language tutorial, C + + tutorial, algorithm/Data Structure Tutorial", all are one-to-one teaching: a one-to-one tutoring + one-to-one answer + assignment + Project Practice + permanent learning. QQ Online, respond at any time!

Case ... Esac is similar to the switch ... case statement in other languages, and is a multi-branch selection structure.

The case statement matches a value or a pattern, and if the match succeeds, the matching command is executed. The case statement is in the following format:
Case value in mode 1)    Command1    command2    command3    ;; Mode 2)    Command1    command2    command3    ;; *)    Command1    command2    command3    ;; Esac
The case works as shown above. The value must be followed by the keyword in, and each pattern must end with a closing parenthesis. The value can be either a variable or a constant. The matching discovery value conforms to a pattern, in which all commands begin to execute until;;.;; Similar to break in other languages, it means jumping to the end of the entire case statement.

The value will detect each pattern that matches. Once the pattern matches, the other mode is no longer resumed after the corresponding command is executed. If there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command.

The following script prompts you to enter 1 to 4 to match each pattern:
  1. Echo ' Input a number between 1 to 4 '
  2. echo ' Your number is:\c'
  3. Read Anum
  4. Case $aNum in
  5. 1) echo ' you select 1 '
  6. ;;
  7. 2) echo ' you select 2 '
  8. ;;
  9. 3) echo ' you select 3 '
  10. ;;
  11. 4) echo ' you select 4 '
  12. ;;
  13. *) echo ' Do not select a number between 1 to 4 '
  14. ;;
  15. Esac
Enter different content, there will be different results, for example:
Input a number between 1 to 4Your number Is:3you Select 3

One more example:
  1. #!/bin/bash
  2. Option="${1}"
  3. Case ${option} in
  4. - f) FILE="${2}"
  5. echo "File name is $FILE"
  6. ;;
  7. - D) DIR="${2}"
  8. echo "Dir name is $DIR"
  9. ;;
  10. *)
  11. echo "' BaseName ${0} ': Usage: [-f file] | [-D directory] "
  12. exit 1 # Command to come out of the program with status 1
  13. ;;
  14. Esac
Operation Result:
$./test.shtest.sh:usage: [-f filename] | [-D directory]$./test.sh-f index.htm$ vi test.sh$./TEST.SH-F index.htmfile name is index.htm$./test.sh-d unixdir N AME is unix$

Shell case ESAC Statement "Go"

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.