Bash:检查用户输入是否正确[重复]

时间:2021-04-27 15:43:10

This question already has an answer here:

这个问题在这里已有答案:

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?

必须有一个简单的方法来检查输入是否只是数字,如果意外输入任何其他内容,将再次请求输入?

3 个解决方案

#1


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

#2


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

#3


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)

    # -- 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

#1


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

#2


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

#3


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)

    # -- 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