小编Ale*_*lfe的帖子

在shell脚本中使用expr进行乘法运算

我正在制作一个基本计算器来进行加,减,乘和除.

加法有效,但不是乘法.当我试图繁殖时,我得到"你没有正确运行程序"的回复:

$ ./calculator 4 + 5
9
$ ./calculator 4 * 5
You did not run the program correctly
Example: calculator 4 + 5
Run Code Online (Sandbox Code Playgroud)

我在google上搜索过,在那里我找到了\\*代码,但仍然无法正常工作.有人能为我提供解决方案或解释吗?

这是我的代码

#!/bin/bash

if [ $# != 3 ]; then  
  echo You did not run the program correctly   
  echo Example: calculator 4 + 5                
  exit 1 
fi

if [ $2 = "+" ]; then    
  ANSWER=`expr $1 + $3`    
 echo $ANSWER 
fi

if [ $2 = "*" ]; then
  ANSWER=`expr $1 \\* $3`  
  echo …
Run Code Online (Sandbox Code Playgroud)

unix linux bash shell expr

8
推荐指数
2
解决办法
7万
查看次数

标签 统计

bash ×1

expr ×1

linux ×1

shell ×1

unix ×1