简体   繁体   English

shell中两个输入变量的乘积

[英]product of two input variable in shell

The program works fine in case " + " ," - "," /" 该程序在“+”,“ - ”,“/”的情况下工作正常

Example : 示例:

./calculate.sh 1 + 2 ==> output is 3.

But the problem here is when I type : 但问题出在我输入的时候:

./calculate.sh 1 * 2 ==>output is "Please ..."

I've tried "\\*" like this but it seem the variable $2 can not get what I want. 我试过像这样的"\\*"但似乎变量$ 2无法得到我想要的东西。

Anyone can help me what is the problem here ? 任何人都可以帮我解决这里的问题吗?

#!/bin/sh
if [ $# -ne 3 ]
 then
      echo "Please input a number , an operator + or - or * or /"
 else
      case "$2" in 
            "+") echo "Sum is `expr $1 + $3`";;
            "-") echo "Substraction is `expr $1 - $3`";;
            "*") echo "Product is `expr $1 \* $3`";;
            "/") echo "Division is `expr $1 / $3`";;
      esac
fi

You should use it like this 你应该像这样使用它

./calculate.sh 2 \* 3

Or 要么

./calculate.sh 2 "*" 3

Or 要么

./calculate.sh 2 '*' 3

You are using "\\*" which is wrong. 您正在使用“\\ *”这是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM