Linux shell provides two methods to obtain a single character in the specified range of user input.

Source: Internet
Author: User

Method 1:

Copy codeThe Code is as follows: echo 'echo {a. z} ''echo {A. Z}'>/tmp/letterfile
While read-p "please input a letter:" letter
Do
('Awk-v var = "$ letter" 'In in {print length (var)} ''= 1 )) & grep-q $ letter/tmp/tmpfile & break
Done
Rm-f/tmp/letterfile
Echo "you input letter $ letter"

[Root @ station1 ~] # Cat a. sh

Copy codeThe Code is as follows: echo 'echo {a. z} ''echo {A. Z}'>/tmp/letterfile
While read-p "please input a letter:" letter
Do
('Awk-v var = "$ letter" 'In in {print length (var)} ''= 1 )) & grep-q $ letter/tmp/tmpfile & break
Done
Rm-f/tmp/letterfile
Echo "you 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:
You input letter
[Root @ station1 ~] #

Initially, use expr length $ letter to replace wk-v var = "$ letter" 'In in {print length (var)} '. However, when you enter special characters such, an error is reported.
As follows:
[Root @ station1 ~] # Cat data

Copy codeThe Code is as follows: while read-p "please input a letter:" letter
Do
('Expr length $ letter '= 1) & expr $ letter: [[: alnum:]>/dev/null & break
Done
Echo "you input letter $ letter"

[Root @ station1 ~] # Bash data
Please input a letter: 123
Please input a letter: abc
Please input a letter:
You input letter
[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:

Method 2:

Copy codeThe Code is as follows: while read-p "please input a letter:" letter
Do
[['Echo $ letter | awk'/^ [[: alpha:] $/{print "1"} ''-eq 1] & break
Done
Echo "you input letter $ letter"

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

Copy codeThe Code is as follows: while read-p "please input a letter:" letter
Do
[['Echo $ letter | awk'/^ [[: alpha:] $/{print "1"} ''-eq 1] & break
Done
Echo "you 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
You 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.