如何提示用户输入两个数字,然后选择使用条件语句中的算术操作员在bash脚本中使用的算术运算符?

发布于 2025-01-30 12:34:06 字数 725 浏览 5 评论 0原文

我正在尝试编程一个代码,其中提示用户输入2个数字,然后选择执行操作员。我试图运行此操作,但是如果语句不起作用,我不知道问题所在。

  echo "Enter 1st number: "
  read num1
  echo "Enter 2nd number: "
  read num2
  echo "a. Add b. Subtract c. Multiply d. Divide"
  echo "Enter Operator: "
  read opr

  if [[$opr==A]]
  then
          sum=$(($num1 + $num2))
          echo "The sum is $sum."
  elif [[$opr==B]]
  then
          diff=$(($num1 - $num2))
          echo "The difference is $diff"
  elif [[$opr==C]]
  then
          prod=$(($num1 * $num2))
          echo "The product is $prod"
  elif [[$opr==D]]
  then
          quot=$(($num1 / $num2))
          echo "The quotient is $quot"
  else
          echo "Invalid. Please enter A, B, C, and D only."
  fi

I am trying to program a code where a user is prompted to input 2 numbers and choose an operator to execute. I tried to run this but the if statements don't work and I don't know the problem.

  echo "Enter 1st number: "
  read num1
  echo "Enter 2nd number: "
  read num2
  echo "a. Add b. Subtract c. Multiply d. Divide"
  echo "Enter Operator: "
  read opr

  if [[$opr==A]]
  then
          sum=$(($num1 + $num2))
          echo "The sum is $sum."
  elif [[$opr==B]]
  then
          diff=$(($num1 - $num2))
          echo "The difference is $diff"
  elif [[$opr==C]]
  then
          prod=$(($num1 * $num2))
          echo "The product is $prod"
  elif [[$opr==D]]
  then
          quot=$(($num1 / $num2))
          echo "The quotient is $quot"
  else
          echo "Invalid. Please enter A, B, C, and D only."
  fi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如痴如狂 2025-02-06 12:34:06

中的错误[[撇开,我建议使用case语句:

case $opr in
  
  A)
      sum=$(($num1 + $num2))
      echo "The sum is $sum."
      ;;
  B)
      diff=$(($num1 - $num2))
      echo "The difference is $diff"
      ;;
  C)
      prod=$(($num1 * $num2))
      echo "The product is $prod"
      ;;
  D)
      quot=$(($num1 / $num2))
      echo "The quotient is $quot"
      ;;
  *)
      echo "Invalid. Please enter A, B, C, and D only."
      ;;
esac

Error in [[ aside, I would recommend using a case statement instead:

case $opr in
  
  A)
      sum=$(($num1 + $num2))
      echo "The sum is $sum."
      ;;
  B)
      diff=$(($num1 - $num2))
      echo "The difference is $diff"
      ;;
  C)
      prod=$(($num1 * $num2))
      echo "The product is $prod"
      ;;
  D)
      quot=$(($num1 / $num2))
      echo "The quotient is $quot"
      ;;
  *)
      echo "Invalid. Please enter A, B, C, and D only."
      ;;
esac
深白境迁sunset 2025-02-06 12:34:06

需要在所有中放置在双方平方括号之间

if [[ $opr == A ]]

如果语句:此外:bash对案例敏感,则 :

if [[ ${opr^^} == A ]]

上面的语法将$ opr变量的值转换为大写,无论您输入什么。

You need to put spaces between double square brackets in all if statements:

if [[ $opr == A ]]

Moreover: bash is case sensitive, so if you want to match both uppercase and lowercase letters from your input request, you should use something like this:

if [[ ${opr^^} == A ]]

That above syntax converts the value of $opr variable to uppercase, whatever you typed.

终陌 2025-02-06 12:34:06

我建议这样做:

read -p "Enter 1st number: " num1
read -p "Enter 2nd number: " num2
echo -e "a. Add b. Subtract c. Multiply d. Divide" && read -p "Enter Operator: " opr

case $opr in
    'a'|'A')
          sum=$(($num1 + $num2))
          echo "The sum is $sum.";;
    'b'|'B')
        diff=$(($num1 - $num2))
        echo "The difference is $diff";;
    'c'|'C')
        prod=$(($num1 * $num2))
        echo "The product is $prod";;
    'd'|'D')
        quot=$(($num1 / $num2))
        echo "The quotient is $quot";;
    *)
        echo "Invalid. Please enter A, B, C, and D only.";;
esac

它更清洁,并取得了理想的结果。

I would suggest this:

read -p "Enter 1st number: " num1
read -p "Enter 2nd number: " num2
echo -e "a. Add b. Subtract c. Multiply d. Divide" && read -p "Enter Operator: " opr

case $opr in
    'a'|'A')
          sum=$(($num1 + $num2))
          echo "The sum is $sum.";;
    'b'|'B')
        diff=$(($num1 - $num2))
        echo "The difference is $diff";;
    'c'|'C')
        prod=$(($num1 * $num2))
        echo "The product is $prod";;
    'd'|'D')
        quot=$(($num1 / $num2))
        echo "The quotient is $quot";;
    *)
        echo "Invalid. Please enter A, B, C, and D only.";;
esac

It's a lot cleaner and achieves your desired result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文