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 ~]#