为什么if语句不起作用?

时间:2013-11-07 15:10:46

标签: bash

我不确定为什么我的if语句不起作用。即使$ EmployeeType为Commission或Hourly,它也会提示输入Weekly Salary。我应该使用if语句而不是elif吗?我尝试使用所有if语句,但一直有错误“语法错误接近意外令牌”}“”。 请善意的建议。

输出:

Enter Payroll of an employee 

Enter employee name: Mary
Mary is Hourly employee.

Enter Weekly Salary :




payroll_employee()
{

   echo
   echo "[Option: $input]"
   echo "Enter Payroll of an employee "
   echo
   echo -en "Enter employee name: "
   read Name

  #Retrieve current entry into individual fields                                                        
  line=`grep -i "$Name" $PAYROLL`
  Name=`echo $line | cut -d "," -f1`
  EmployeeID=`echo $line | cut -d "," -f2`
  EmployeeHP=`echo $line | cut -d "," -f3`
  EmployeeType=`echo $line | cut -d "," -f4` 

  #Check if entry exist in records
   if [ `count_lines "^${Name},"` -eq 0 ]
   then
       echo "Error: This particular record does not exist!!"
   else
      echo "$Name is ${EmployeeType} employee."


   if [ "$EmployeeType"="Salaried" ]
   then
    echo $EmployeeType  
    echo -en "Enter Weekly Salary :"
    read wsalary
    echo

   elif ["$EmployeeType"="Hourly" ]
   then
    echo -en "Enter hourly wage:"
    read hsalary
    echo -en "Enter hours worked this week:"
    read hours
    let "hwages = hsalary * hours"
    echo -en "Gross wages: \$$hwages"
    echo 

   elif ["$EmployeeType"="Commission" ]
   then
    echo -en "Enter Weekly Total Sales:"
    read csales
    echo -en "Gross wages:"
    read cwages
    echo 

   fi
   fi
   echo -en "Hit [Enter] to return to main menu..."
   read 

}

1 个答案:

答案 0 :(得分:3)

这是问题所在:

if [ "$EmployeeType"="Salaried" ]

=运营商需要空格:

if [ "$EmployeeType" = "Salaried" ]

在这里:

elif [ "$EmployeeType" = "Hourly" ]

在这里:

elif [ "$EmployeeType" = "Commission" ]