Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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 - Fatal编程技术网

检查输入是否为整数并等于bash中的特定数字

检查输入是否为整数并等于bash中的特定数字,bash,Bash,尝试检查用户输入的值是否为整数,以及是否等于35或75。如果输入的值是一个整数,等于35或75,那么我希望通知用户这一点。如果输入的值不是35或75(例如字符串、null或不同的整数),那么我的目标是让用户知道它无效,并让他们重试 read-p'请输入一个等于35或等于75的整数:'value' 如果[$value=~^[0-9]+$]&&([$value-eq 35]| |[$value-eq 75] 回显“输入可接受” 出口1 其他的 echo“该值无效。请重试。” fi 出口0 我一直收

尝试检查用户输入的值是否为整数,以及是否等于35或75。如果输入的值是一个整数,等于35或75,那么我希望通知用户这一点。如果输入的值不是35或75(例如字符串、null或不同的整数),那么我的目标是让用户知道它无效,并让他们重试

read-p'请输入一个等于35或等于75的整数:'value'
如果[$value=~^[0-9]+$]&&([$value-eq 35]| |[$value-eq 75]
回显“输入可接受”
出口1
其他的
echo“该值无效。请重试。”
fi
出口0

我一直收到的错误是第10行:意外标记“else”附近的语法错误

您丢失了一个
,然后
。我会这样整理:

#!/bin/bash

read -r -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ "$value" =~ ^[0-9]+$ ]] && [ "$value" -eq 35 ] || [ "$value" -eq 75 ]
    then
    echo "The input is acceptable"
    exit 0

else
    echo "The value is invalid. Try again."

fi

exit 0
更新:为了不断提示用户输入,请执行以下操作:

#!/bin/bash

while 
  read -r -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value
do
if [[ "$value" =~ ^[0-9]+$ ]] && [ "$value" -eq 35 ] || [ "$value" -eq 75 ]
    then
    echo "The input is acceptable"
    exit 0
else
    echo "The value is invalid. Try again."

fi
done

exit 0

你丢失了一个
,然后
。我会这样整理:

#!/bin/bash

read -r -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ "$value" =~ ^[0-9]+$ ]] && [ "$value" -eq 35 ] || [ "$value" -eq 75 ]
    then
    echo "The input is acceptable"
    exit 0

else
    echo "The value is invalid. Try again."

fi

exit 0
更新:为了不断提示用户输入,请执行以下操作:

#!/bin/bash

while 
  read -r -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value
do
if [[ "$value" =~ ^[0-9]+$ ]] && [ "$value" -eq 35 ] || [ "$value" -eq 75 ]
    then
    echo "The input is acceptable"
    exit 0
else
    echo "The value is invalid. Try again."

fi
done

exit 0

当答案正确时,我不确定退出1是否正确,我猜您想这样做:

#!/bin/bash

read -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ $value =~ ^[0-9]+$ && ($value -eq 35 || $value -eq 75) ]]; then
  echo "The input is acceptable"
else
  echo "The value is invalid. Try again."
  exit 1
fi

exit 0
我个人会将其简化为:

#!/bin/bash

read -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ "${value}" == "35" || "${value}" == "75" ]]; then
  echo "The input is acceptable"
else
  echo "The value is invalid. Try again."
  exit 1
fi

exit 0

当答案正确时,我不确定退出1是否正确,我猜您想这样做:

#!/bin/bash

read -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ $value =~ ^[0-9]+$ && ($value -eq 35 || $value -eq 75) ]]; then
  echo "The input is acceptable"
else
  echo "The value is invalid. Try again."
  exit 1
fi

exit 0
我个人会将其简化为:

#!/bin/bash

read -p 'Please enter an integer that is either equal to 35 or equal to 75: ' value

if [[ "${value}" == "35" || "${value}" == "75" ]]; then
  echo "The input is acceptable"
else
  echo "The value is invalid. Try again."
  exit 1
fi

exit 0

您缺少一个右括号。第一个条件应该用双括号括起来;单个条件不支持正则表达式。@NigelWash:补充oguz ismail的正确commo:看看man test,看看单括号中允许什么。您缺少一个右括号。第一个条件应该用dou括起来ble括号;单括号不支持正则表达式。@NigelWash:补充oguz ismail的正确Commo:看一下人工测试,看看单括号中允许什么。嗨,托马斯,非常感谢你的帮助!有没有办法创建一个while循环,在输入数字35或75之前不断提示用户?当然,这是一个更新后的答案。嗨,托马斯,非常感谢你的帮助!有没有办法创建一个while循环,在输入数字35或75之前不断提示用户?当然,这是更新后的答案。