鞭尾需要三个数字才能继续

鞭尾需要三个数字才能继续

我有一个鞭尾形式,提示用户输入。有没有办法强迫某人输入三个数字,然后才允许他继续?

NUMBERS=$(whiptail --inputbox "Enter three numbers only" 8 78 000
--title "Three Numbers" 3>&1 1>&2 2>&3)

exitstatus=$? if [ $exitstatus = 0 ]; then
    echo "User selected Ok and entered " $NUMBERS else
    echo "User selected Cancel." fi

echo "(Exit status was $exitstatus)"

答案1

您可以检查用户输入的内容并whiptail循环运行,直到他们输入有效值:

#!/usr/bin/env bash

## I included the 000 in the regex below since
## you are populating your field with it. I assume you
## want 000 + 3 numbers.
until [[ $numbers =~ ^000[0-9]{3}$ ]]; do
        numbers=$(whiptail --inputbox "Enter three numbers only" 8 78 000 --title "Three Numbers" 3>&1 1>&2 2>&3)
        exitstatus=$?
        if [ $exitstatus = 0 ]; then
                echo "User selected Ok and entered " $numbers
        else
                echo "User selected Cancel."
                break;
        fi
done

echo "(Exit status was $exitstatus)"

请注意,我还将 更改NUMBERSnumbers。作为一般规则,请避免在 bash 脚本中使用大写变量名称,因为全局环境变量都是大写的,这可能会导致混乱。

相关内容