在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
方法一: 复制代码 代码如下: 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 最初使用expr length $letter 替代 wk -v var="$letter" 'BEGIN{print length(var)}'的,但是,当输入*等特殊字符时,会报错。 复制代码 代码如下: 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 来看方法二: 复制代码 代码如下: while read -p "please input a letter: " letter do [[ `echo $letter | awk '/^[[:alpha:]]$/{print "1"}'` -eq 1 ]] && break done echo "you input letter $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 ~]# chmod +x b.sh |
请发表评论