击:检查用户输入的是正确的 [英] Bash: check user input is correct

查看:112
本文介绍了击:检查用户输入的是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个bash脚本它通过用户输入需要的数字进行计算。我的问题是,如果用户键入一个字母或错误的空间,整个脚本失败,用户必须重新开始。

I wrote a bash script which takes numbers for calculation via user input. The problem I have is if the user types a letter or a space by mistake, the whole script fails and the user must start again.

有必须检查输入一个简单的方法仅仅是数字会要求再次输入,如果别的输入意外?

There must be an easy way to check the input is numeric only that will ask for the input again if anything else is input accidentally?

推荐答案

使用whil​​e循环

Use a while loop

number=""
while [[ ! $number =~ ^[0-9]+$ ]]; do
    echo Please enter your age
    read number
done
echo You are $number years old

这篇关于击:检查用户输入的是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆