bash 检查用户输入是否正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/848342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:10:28  来源:igfitidea点击:

Check user input is correct

bash

提问by Johannes Schaub - litb

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.

我编写了一个 bash 脚本,它通过用户输入获取数字进行计算。我遇到的问题是,如果用户错误地输入了一个字母或一个空格,整个脚本就会失败,用户必须重新开始。

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?

必须有一种简单的方法来检查输入是否仅为数字,如果不小心输入了其他任何内容,它将再次要求输入?

回答by Johannes Schaub - litb

Use a while loop

使用while循环

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

回答by lhunath

And to avoid the heavy regex engine, use a simple glob:

为了避免繁重的正则表达式引擎,请使用一个简单的 glob:

if [[ ! $input || $input = *[^0-9]* ]]; then
    echo "Error: '$input' is not a number." >&2
fi

回答by Mike Q

Allows space before and after the number, _33, or 33___ , but not 3__3. and no letters. 0 or

允许数字前后有空格,_33 或 33___,但不允许 3__3。没有字母。0 或

-- Get input from user until Correct

-- 从用户获取输入直到正确

    # -- get input until Correct
    unset get_num
    while [[ ! ${get_num} =~ ^[0-9]+$ ]]; do
        echo "Please enter in a number:"
        read get_num
    done
    echo This is a number :  ${get_num}

-- Get input until Correct (within range)

-- 获取输入直到正确(在范围内)

    # -- get input until Correct (within range)
    unset get_num
    while [[ ! ${get_num} =~ ^[0-9]+$ ]]; do
        echo "Please enter in a number within range of (1-30):"
        read get_num
        ! [[ ${get_num} -ge 1 && ${get_num} -le 30  ]] && unset get_num
    done
    echo This is a number withn a range :  ${get_num}

-- get input until Correct (within range) (full regex way)

-- 获取输入直到正确(在范围内)(完整的正则表达式方式)

"Since regular expressions deal with text rather than with numbers, matching a number in a given range takes a little extra care. You can't just write [0-255] to match a number between 0 and 255. Though a valid regex, it matches something entirely different. [0-255] is a character class with three elements: the character range 0-2, the character 5 and the character 5 (again). This character class matches a single digit 0, 1, 2 or 5, just like [0125]." ~ http://www.regular-expressions.info/numericranges.html

“由于正则表达式处理的是文本而不是数字,因此匹配给定范围内的数字需要格外小心。你不能只写 [0-255] 来匹配 0 到 255 之间的数字。虽然是一个有效的正则表达式,它匹配完全不同的东西。[0-255] 是一个包含三个元素的字符类:字符范围 0-2、字符 5 和字符 5(再次)。这个字符类匹配单个数字 0、1、2 或5,就像[0125]一样。” ~ http://www.regular-expressions.info/numericranges.html

    # -- get input until Correct (within range) (another way)
    unset get_num
    while [[ ! ${get_num} =~ ^([1-9]|1[0-9]|2[0-9]|30)$ ]]; do
        echo "Please enter in a number within range of (1-30):"
        read get_num
    done
    echo This is a number withn a range :  ${get_num}

-- Get input, and check it only (no while loop)

-- 获取输入,并只检查它(没有 while 循环)

    # -- get input, and check it only (no while loop)
    unset get_num
    echo "Please enter in a number:"
    read get_num
    if [[ ! ${get_num} =~ ^[0-9]+$ ]] ;then 
        echo "${get_num} isn't a number" 
    else
        echo "${get_num} is a number"
    fi