ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Shell实操(九):判断输入内容直至指定内容退出

Shell实操(九):判断输入内容直至指定内容退出

目录

一、需求

二、实现及关键知识点

1、脚本内容

2、关键知识点

三、验证


一、需求

要求用户输入数值,输入其他提示输入有问题,直到用户输入“end”结束。

二、实现及关键知识点

1、脚本内容

# cat 9.sh
#! /bin/bashwhile :
doread -p "Please input a number:(Input "end" to quit) " nnum=`echo $n|sed -r 's/[0-9]//g'|wc -c`if [ $n == "end" ]thenexitfiif [ $num -ne 1 ]thenecho "What you input is not a number! Please try again"fi
done

2、关键知识点

(1)wc -c计算字符串长度,其中回车为一个字符长度

(2)sed -r 's/[0-9]//g'将字符串数字删除

(3)exit退出脚本

三、验证

# bash 9.sh
Please input a number:(Input end to quit) abc123
What you input is not a number! Please try again
Please input a number:(Input end to quit) 123
Please input a number:(Input end to quit) end

返回列表