linux shell實(shí)現(xiàn)獲取用戶輸入指定范圍的單個(gè)字符的兩種方法
方法一:
echo `echo {a..z}``echo {A..Z}` > /tmp/letterfile
while read -p "please input a letter: " letter
do
((`awk -v var="$letter" 'BEGIN{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
echo `echo {a..z}``echo {A..Z}` > /tmp/letterfile
while read -p "please input a letter: " letter
do
((`awk -v var="$letter" 'BEGIN{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: a
you input letter a
[root@station1 ~]#
最初使用expr length $letter 替代 wk -v var="$letter" 'BEGIN{print length(var)}'的,但是,當(dāng)輸入*等特殊字符時(shí),會(huì)報(bào)錯(cuò)。
如下:
[root@station1 ~]# cat data
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: a
you input letter a
[root@station1 ~]# bash data
please input a letter: *
expr: 語法錯(cuò)誤
data: line 3: ((: == 1: syntax error: operand expected (error token is "== 1")
please input a letter:
來看方法二:
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 ~]# cat b.sh
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 ~]#
相關(guān)文章
shell腳本批量創(chuàng)建用戶的方法小結(jié)
有些面試題中會(huì)問到批量創(chuàng)建用戶的題目,大體是用循環(huán)去進(jìn)行創(chuàng)建,但有時(shí)也會(huì)有一些額外的附加條件,下面這篇文章主要給大家介紹了關(guān)于shell腳本批量創(chuàng)建用戶的相關(guān)資料,需要的朋友可以參考下2022-03-03Shell中實(shí)現(xiàn)整數(shù)自增的幾種方法示例
Linux Shell中寫循環(huán)時(shí),常常要用到變量的自增,下面這篇文章主要給大家分享了關(guān)于Shell中實(shí)現(xiàn)整數(shù)變量自增的幾種方法,包括declare -i來聲明、使用let命令、使用(())以及使用expr命令等等方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08hi 感恩節(jié)——Linux基礎(chǔ)教程之mysql和php
這篇文章主要介紹了hi 感恩節(jié)——Linux基礎(chǔ)教程之mysql和php的相關(guān)資料,需要的朋友可以參考下2015-11-11Shell腳本中使用getopts處理多命令行選項(xiàng)
今天小編就為大家分享一篇關(guān)于Shell腳本中使用getopts處理多命令行選項(xiàng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03shell腳本學(xué)習(xí)指南[六](Arnold Robbins & Nelson H.F. Beebe著)
這篇文章主要介紹了shell腳本學(xué)習(xí)指南[六](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以參考下2014-02-02使用shell腳本找出網(wǎng)站的空頁面和404錯(cuò)誤頁面
這篇文章主要介紹了使用shell腳本找出網(wǎng)站的空頁面和404錯(cuò)誤頁面,本文使用一句話實(shí)現(xiàn),需要的朋友可以參考下2014-11-11