2015-11-08 25 views
1

這裏是我的shell腳本,EXPR顯示錶達式,而不是解決它

#! /bin/sh 
# basic calculator 
echo "Please input your choice" 
printf " 1.Addition \n 2.SUbstraction \n 3.Multiplication \n 4.Division\n" 
read choice 
case "$choice" in 
    1) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t=$(expr "$n1"+"$n2");echo "$n1+$n2=$t";; 
    2) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t='expr $n1-$n2';echo "$n1-$n2=$t";;  
    3) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t='expr $n1\*$n2';echo "$n1*$n2=$t";; 
    4) echo "Enter number 1:";read n1;echo "Enter number 2:";read n2;t='expr $n1/$n2';echo "$n1/$n2=$t";; 
esac 

這裏是我的輸出,

Script started on Sunday 08 November 2015 12:05:21 PM IST 
Please input your choice 
1.Addition 
2.SUbstraction 
3.Multiplication 
4.Division 
1 
Enter number 1: 
5 
Enter number 2: 
6 
5+6=5+6 

的問題是,我的expr的心不是真正解決表達式

回答

2

expr一些版本則建議使用shell算術來代替:如果需要

$ echo $((5+6)) 
11 
$ echo $((5>=6)) 
0 

使用shell算術使用空格分開整數是沒有必要的。

的EXPR工具使得論據 可以 是運營商和其可以是操作數的參數之間沒有詞法區別。與操作符在詞彙上相同的操作數將被視爲語法錯誤。

The syntax of the expr command in general is historic and inconvenient. 
New applications are advised to use shell arithmetic rather than expr 
3

空白事項:

$ expr 5+6 
5+6 
$ expr 5 + 6  
11 

做算術,你需要給3個不同的參數。

1

您正在使用單引號('),而不是爲其他調用expr反引號(`)。但正如我將指出的那樣,幾乎沒有任何理由使用expr來執行算術。

相關問題