Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅限bash整数_Bash_Integer_Evaluation - Fatal编程技术网

仅限bash整数

仅限bash整数,bash,integer,evaluation,Bash,Integer,Evaluation,我似乎不能只允许单整数输入。 如果有人使用abc,它就会起作用 但如果有人输入abc123或123abc,它仍然会将其视为有效整数 # input to be an integer. validate_integer(){ if [ ! "$#" -eq "1" ]; then error "Please enter one numberic value only" return 1 elif [[ "$1" =~ ^[[:al

我似乎不能只允许单整数输入。 如果有人使用abc,它就会起作用

但如果有人输入abc123或123abc,它仍然会将其视为有效整数

# input to be an integer.
validate_integer(){

    if [ !  "$#" -eq "1" ]; then
            error "Please enter one numberic value only"
            return 1
    elif [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
            error "Input must be a NUMBER"
            return 1
    else
            return 0
    fi
}
更改此行:

elif [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
为此:

elif ! [[ "$1" =~ ^[[:digit:]]+$ ]]; then
在这里,
^[[:digit:]+$
表示字符串必须从头到尾由数字组成,长度必须为1个或更多字符。我们用
来否定这一点,用于处理字符串不满足此条件(不是完全数字)时的情况。

请参阅此。