bash脚本。如何将用户的选择添加到txt文件中

时间:2013-10-27 10:01:54

标签: bash

我想让用户键在选项1-3中,然后保存,然后与.txt文件中的其他输入一起保存。我尝试过使用开关,但它似乎不起作用.. 只有bash的初学者才能进行..

    echo  "Select the Employee Type:"
echo "1- Salaried Employee"
echo "2- Hourly Employee"
echo "3- Commission Employee"
echo -en "Enter Selection: "
read choice

 case $choice in
         "1")  
    Type="Salaried Employee"
      break
      ;;
      "2") 
    Type = "Hourly Employee"
      break
      ;;
      "3") 
    Type ="Commission Employee"
      break
      ;;
          *) echo "Error: Please enter a choice between 1 - 7"
             echo -en "Hit [Enter] to return..."
             read
           ;;
  esac

   `if [ `count_lines "^${Name},"` -ne 0 ]                                                
   then
       echo "Error: Entry already exist!!"
   else 
       confirm "Are you sure you want to add the entry?[y/n]" 
       if [ $? -ne 0 ]
       then
           echo "Previous entry not updated!"           
       else 
           echo "Record is updated with new employee!!"
           echo "${Name},${Job},${Department},${Salary},${Type}" >> $PAYROLL
       fi       
   fi`

1 个答案:

答案 0 :(得分:0)

这个脚本出了几个问题:

  • 在案例替代结束时,只有;; - 没有break(跳出do / for / while)
  • if之前和fi导致语法错误之后会有额外的反引号;删除它们
  • =周围必须没有空格,例如Type = "Hourly Employee"Type ="Commission Employee"