2015-07-20 41 views
0

後,我跑了一個簡單的腳本,我得到這個消息:的bash語法錯誤:未接受令牌艾利芙

我的代碼

line 9: syntax error near unexpected token elif line 9: elif [ $sales -le 20000 ];

#!/bin/bash 
    echo "Please enter the how much sales you have done" 
    read sales 
    if [ $sales -le 5000 ]; 
    then 
     echo "your profit is $[$sales*0.25]" 
    elif [ $sales -le 10000 ]; 
     echo "your profit is $[$sales*0.1]" 
    elif [ $sales -le 20000 ]; 
     echo "your profit is $[$sales*0.15]" 
    elif [ $sales -le 40000 ]; 
     echo "your profit is $[$sales*0.2]" 
    else 
     echo "your profit is $[$sales*0.25]" 
    fi 
+4

你缺少了'then's – 123

+1

你你的權利,謝謝:) – Mariel

+1

http://shellcheck.net會抓住這些錯誤,你和那麼,一旦你解決了這些問題,還有其他一些問題。 –

回答

4
  1. 你錯過thenelif
  2. 你不能在bash中做浮點數學。

你可以使用這個腳本:

#!/bin/bash 
read -p "Please enter the how much sales you have done: " sales 
if [ $sales -le 5000 ]; then 
    pp='0.25' 
elif [ $sales -le 10000 ]; then 
    pp='0.1' 
elif [ $sales -le 20000 ]; then 
    pp='0.15' 
elif [ $sales -le 40000 ]; then 
    pp='0.2' 
else 
    pp='0.25' 
fi 
echo "your profit is " $(bc -l <<< "$sales * $pp") 
+0

是否有另一種方法在bash中進行浮點數學運算? – Mariel

+0

沒有本機BASH不允許浮點數學。你需要使用一個外部工具,比如'bc','awk'等。 – anubhava

+0

我明白了,非常感謝! – Mariel