2017-01-22 173 views
-1

我沒有得到正確的輸出結果。它不是將輸入與附加成本相乘,而是按照原來的方式顯示輸入。我需要將人數乘以他們可能基於選擇的額外費用。爲什麼我沒有得到正確的乘法輸出?

#!/bin/bash 

read -p "Number of Adult Tickets:(0 for none) " integer1  
read -p "Number of Junior Tickets:(0 for none) " integer2 

echo ""  
echo "$integer1 Adults"  
echo "$integer2 Juniors" 
echo "" 

num1=$50  
num2=$20  
num3=$5  

echo "Season Pass Add-On:"  
echo "1)Add Seasonal Parking"  
echo "2)Add Season Pass For Theme Park"  
echo "7)Just Buy Tickets For Today"  
echo "8)I wanna exit." 

echo "" 
read -p "What would the guest like to do? " guest 
echo ""  

case $guest in 
    1)  
     echo "Add Seasonal Parking"  
     echo ""  
     echo "Add an additional $num1 for every adult pass"  
     echo ""  
     echo "For Adult Tickets "$((integer1 + num1))""  
     echo "For Junior Tickets "$((integer2 + num1))""  
     ;;  
    2)  
     echo "Add Season Pass For Theme Park"  
     echo ""  
     echo "Add an additional $num2 for every adult pass"  
     echo "Add an additional $num3 for every junior pass"  
     echo ""  
     echo "For Junior Tickets "$((integer2 * num2))""  
     echo "For Adult Tickets "$((integer1 * num3))""  
     echo "For Junior Tickets "$((integer2 * num2))""  
     echo "For Adult Tickets "$((integer1 * num3))""  
     ;; 
    3)  
     echo "Just buy tickets for today"  
     exit 0  
     ;;  
    4)  
     echo "invalid entry. You are exiting"  
     exit 1  
     ;; 
esac  

exit 0 

回答

2

的問題是

num1=$50 
num2=$20 
num3=$5 

的美元符號的美元符號在Bash的特殊的意義,所以它不會作爲一個普通數字工作。將這些常量存儲爲不帶幣種,然後將其添加到結果中。

美元符號需要用反斜槓轉義才能用echo顯示。

echo "For Adult Tickets \$$((integer1 + num1))" 
+0

非常感謝! – pinook

相關問題