A bug analysis of case condition control statement in shell script _linux shell

Source: Internet
Author: User
Tags arithmetic case statement lowercase uppercase letter

In the shell script, you find a problem with the case statement.
This method of specifying lowercase letters [A-z] and uppercase letters [A-z] is no use.

The following conditions occur:

Copy Code code as follows:

[Root@station1 ~]# Cat case.sh
#!/bin/bash
While:
Todo
Echo-n "Input a Letter:"
Read Var
Case "$var" in
[A-z] echo "lowercase letter";;
[A-z] echo "uppercase letter";;
[0-9]) echo "Digit";;
*) echo "punctuation, whitespace, or other";;
Esac
Done
[Root@station1 ~]# Bash case.sh
Input a letter:a
Lowercase Letter
Input a letter:a
Lowercase Letter
Input a Letter:2
Digit
Input a letter:0
Digit
Input a Letter:b
Lowercase Letter
Input a Letter:y
Lowercase Letter
Input a letter: ^c
[Root@station1 ~]#

You can see that when the input is uppercase and lowercase, it will output "lowercase letter"

When I was puzzled, the miracle happened ....

Copy Code code as follows:

[Root@station1 ~]# Bash case.sh
Input a Letter:z
Uppercase letter
Input a letter:

When we enter Capital Z, we finally have the result we want: uppercase letter
Later in the man bash document there was no description of the "-" representation of the scope, the value said to match "-", put "-" into [] the front or the last side.
case word in [[] pattern [| pattern] ...) list; Esac
A case Command-expands word, and tries to match it against all pattern in turn, using the same matching rules as fo R pathname
Expansion (Pathname expansion below). The word is expanded using tilde expansion, parameter and variable expansion, arithmetic sub-
Stitution, command substitution, process substitution and quote removal. Each pattern examined is expanded using tilde expansion, param-
Eter and variable expansion, arithmetic substitution, command substitution, and process substitution. If The shell option Nocasematch is
Enabled, the match is performed without regard to the case of alphabetic characters. When a match is found, the corresponding list is
Executed. If the;; Operator is used, no subsequent matches are attempted after the the Using;& in place of;; Causes
Execution to continue with the list associated with the next set of patterns. Using;; & in place of;; Causes the shell to test the next
The pattern list in the statement, if all, and execute any associated list on a successful match. The exit status is zero if no pattern
Matches. Otherwise, it is the exit status of the last command executed in list.

Look at the following code:

Copy Code code as follows:

[Root@station1 ~]# Cat case.sh
#!/bin/bash
While:
Todo
Echo-n "Input a Letter:"
Read Var
Case "$var" in
[A-c]) echo "lowercase letter";;
[A-z] echo "uppercase letter";;
[0-9]) echo "Digit";;
*) echo "punctuation, whitespace, or other";;
Esac
Done
[Root@station1 ~]# Bash case.sh
Input a letter:a
Lowercase Letter
Input a Letter:b
Lowercase Letter
Input a Letter:c
Lowercase Letter
Input a Letter:d
Uppercase letter
Input a Letter:e
Uppercase letter
Input a letter: ^c
[Root@station1 ~]#

The

can see that it is encoded in such a way that it is Aabbccddee...yyzz
. This is a small bug, if you really want to achieve the results we want, you can use POSIX [: Upper:].
Personal Thoughts: Sometimes this is not a bad thing, and maybe you can use the bug to do something.

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.