2016-02-02 50 views
1

因此,我已經用輸入腳本解決了我的第一個問題,但現在無論輸入什麼字母,都只添加數字。運行bash腳本時出現邏輯錯誤

這裏是代碼。任何幫助將不勝感激。

#!/bin/bash 

add() { 
     expr $x + $y 
} 

sub() { 
     expr $x - $y 
} 

mult() { 
     expr $x * $y 
} 

div() { 
     expr $x/$y 
} 

echo "Enter a for add, s for subtract, m for multiply or d for divide and 2 numbers" 
read choice x y 

if [ $choice=="a" ] 
then 
     add 
else 
     if [ $choice == "s" ] 
     then 
       sub 
     else 
       if [ $choice == "m" ] 
       then 
         mult 
       else 
         if [ $choice == "d" ] 
         then 
           div 

         fi 
       fi 
     fi 
fi 
+2

」沒有機會輸入「。不清楚你的意思,你認爲你的腳本可以被稱爲'myscript a 3 4'嗎?這是你的輸入,否則你必須添加'read x?'在腳本中輸入x值「'等等。另外'elif'會更適合你的二次測試。更好的辦法是使用'case $ op a)add ... ;;; ....; esac'等。更好地澄清你的Q文本,而不是迴應評論。祝你好運。 – shellter

+0

謝謝。我從添加輸入的兩種方法中改變了一半,並弄糊塗了。 – Bradg89

回答

2

首先的,您希望腳本read the values from the standard input,但你從參數恢復它。

第二個,你沒有傳遞參數給函數。

第三,您不使用函數內的參數。

第四,在使用表達式時,您不會讓操作符之間出現空格。

注意Rany Albeg的Wein說,這bash guide已經過時,他建議this one。我也推薦GNU official guideother formats)。

因此,假設你想用你的腳本一樣./my-script.sh m 2 3,這裏是你的代碼,但工作:

#!/bin/bash                                           

add() {                    
    expr $1 + $2                 
}                     

sub() {                    
    expr $1 - $2                 
}                     

mult() {                   
    expr $1 \* $2                 
}                     

div() {                    
    expr $1/$2                 
}                    

echo "Enter a for add, s for subtract, m for multiply or d for divide and 2 numbers" 
x=$2                    
y=$3                    

if [ $1 == "a" ]                 
then                    
    add $x $y                 
else                    
    if [ $1 == "s" ]                
    then                   
     sub $x $y                
    else                   
     if [ $1 == "m" ]               
     then                  
      mult $x $y               
     else                  
      if [ $1 == "d" ]              
      then                 
       div $x $y              
      fi                 
     fi                  
    fi                   
fi 

最後,這是你的腳本微創修改爲read the data from the standard input

#!/bin/bash                  

add() {                   
    echo "Result:"                                          
    expr $1 + $2                 
}                    

sub() {                   
    echo "Result:"                
    expr $1 - $2                 
}                    

mult() {                   
    echo "Result:"                
    expr $1 \* $2                
}                    

div() {                   
    echo "Result:"                
    expr $1/$2                 
}                    

echo "Enter a for add, s for subtract, m for multiply or d for divide and 2 numbers" 
read operation                 
echo "Read first parameter"              
read x                   
echo "Read second parameter"              
read y                   

if [ $operation == "a" ]               
then                    
    add $x $y                 
else                    
    if [ $operation == "s" ]              
    then                   
     sub $x $y                
    else                   
     if [ $operation == "m" ]             
     then                  
      mult $x $y               
     else                  
      if [ $operation == "d" ]            
      then                 
       div $x $y              
      fi                 
     fi                  
    fi  
fi 

另外,如果您遇到了一些麻煩,您可以在腳本的開頭設置#!/bin/bash -xv,將調試消息添加到腳本中。

+3

tldp Bash指南已過時,並且在某些情況下顯然是錯誤的。雖然部分可能是正確的,但建議避免使用它,特別是作爲一個begginer。我建議使用這個[Bash指南](http://mywiki.wooledge.org/BashGuide)來代替:-) –

+0

謝謝Rany,我編輯了回覆並添加了一個註釋。鏈接現在重定向您推薦的指南。此外,我添加了GNU Bash官方指南鏈接。 –

+0

太棒了!謝謝 :-) –

3

好像你想獲得X從第一和第二個參數($1$2)Y和讀操作(A,S,d,男)從標準輸入。 我修改您的代碼一點,以克服原劇本的問題,並根據我的假設提供結果:

#!/bin/bash 

# First number. 
x=$1 
# Second number. 
y=$2 
# Result of either addition, subtraction, division or multiplication of  $x and $y. 
result=0 

# Reads operation from user. 
read -ep "Enter a for add, s for subtract, m for multiply or d for divide: " operation 

case $operation in 
    a) result=$((x + y));; 
    s) result=$((x - y));; 
    d) result=$((x/y));; 
    m) result=$((x * y));; 
    *) printf '%s: %s\n' "$operation" "Unknown operation" >&2; exit 1;; 
esac 

printf 'result: %s\n' "$result" 

用法示例:(腳本名稱爲sof.sh

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: a 
result: 9 

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: m 
result: 20 

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: s 
result: 1 

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: d 
result: 1 

PS

請注意以下事項:

  • expr在古代是shell代碼用來做數學的程序。在像bash這樣的POSIX shell中,使用$((expression))。在bash,ksh88 +,mksh/pdksh或zsh中,您還可以使用((expression))或'let 表達式'。
  • 儘管沒有在腳本中使用originaly,但在Bash編程時,值得知道的是[[是一個類似於(但功能比[命令更強大)的bash關鍵字。看到這個Bash FAQTest and conditionals。 除非你正在爲POSIX sh寫作,否則建議使用[[。 「
相關問題