Linux shell implementations two ways to get user input to a single character in a specified range _linux shell

Source: Internet
Author: User
Tags chmod

Method One:

Copy Code code as follows:

Echo ' echo {a.. Z} ' echo {A.. Z} ' >/tmp/letterfile
While Read-p ' please input a letter: ' Letter
Todo
((' Awk-v var= "$letter" ' Begin{print length (var)} ' = = 1)) && grep-q $letter/tmp/tmpfile && break
Done
Rm-f/tmp/letterfile
echo "Your input letter $letter"

[Root@station1 ~]# Cat a.sh

Copy Code code as follows:

Echo ' echo {a.. Z} ' echo {A.. Z} ' >/tmp/letterfile
While Read-p ' please input a letter: ' Letter
Todo
((' Awk-v var= "$letter" ' Begin{print length (var)} ' = = 1)) && grep-q $letter/tmp/tmpfile && break
Done
Rm-f/tmp/letterfile
echo "Your input letter $letter"

[Root@station1 ~]# chmod +x a.sh
[Root@station1 ~]#./a.sh
Please input a letter:123
Please input a LETTER:ABC
Please input a letter:4
Please input a letter: &
Please input a letter: (
Please input a letter:a
Your input letter A
[Root@station1 ~]#

The first use of expr length $letter replaces the wk-v var= "$letter" ' Begin{print length (Var)} ', but an error occurs when special characters such as * are entered.
As follows:
[Root@station1 ~]# Cat data

Copy Code code as follows:

While Read-p ' please input a letter: ' Letter
Todo
((' expr length $letter ' = 1) ') && expr $letter: [[: Alnum:]] >/dev/null && break
Done
echo "Your input letter $letter"

[Root@station1 ~]# Bash data
Please input a letter:123
Please input a LETTER:ABC
Please input a letter:a
Your input letter A
[Root@station1 ~]# Bash data
Please input a letter: *
Expr: syntax error
Data:line 3: (: = = 1:syntax Error:operand Expected (Error token is "= = 1")
Please input a letter:

See method Two:

Copy Code code as follows:

While Read-p ' please input a letter: ' Letter
Todo
[[' Echo $letter | awk '/^[[:alpha:]]$/{print ' 1 "} '-eq 1]] && break
Done
echo "Your input letter $letter"

As follows:
[Root@station1 ~]# Cat b.sh

Copy Code code as follows:

While Read-p ' please input a letter: ' Letter
Todo
[[' Echo $letter | awk '/^[[:alpha:]]$/{print ' 1 "} '-eq 1]] && break
Done
echo "Your input letter $letter"

[Root@station1 ~]# chmod +x b.sh
[Root@station1 ~]#./b.sh
Please input a letter: *
Please input a LETTER:ABC
Please input a letter:234
Please input a letter:)
Please input a letter:t
Input Letter T
[Root@station1 ~]#

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.