我在下面的代码中做错了什么?
我正在替换文本文件中的工资数据,但Telephone number
正在更新字段(第 3 列)而不是salary
0 的字段(第 5 列)。
在下面的示例中,Ruben 的计算工资为 500。
我想要的输出是:
Ruben,1223,97707001,Salaried,500
但相反,我得到了这个(用 535 替换 9770 和 7001 之间的零):
Ruben,1223,9775007001,Salaried,0
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`
Salary=`echo $line | cut -d "," -f5`
#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 swages
if [ -z $swages ]
then
swages=$Salary
else
grep -vi "$Name" $PAYROLL > tmpfile #Perform updating to salary field entry
grep -x "$line" $PAYROLL | sed -e "s/$Salary/$swages/" >> tmpfile
mv tmpfile $PAYROLL
echo "$Name's weekly payroll has been updated to \$$swages!!"
fi
echo
}
示例代码:
update_employee()
{
echo
echo "[Option: $input]"
echo "Updating employee record... "
echo "Please enter the name of the employee to update: "
echo -en "[1]Name: "
read update_name
#Retrieve current entry into individual fields
line=`grep -i "$update_name" $PAYROLL`
oldname=`echo $line | cut -d "," -f1`
oldjob=`echo $line | cut -d "," -f2`
olddept=`echo $line | cut -d "," -f3`
oldsal=`echo $line | cut -d "," -f4`
#Check if entry to update exist in records
if [ `count_lines "^${update_name},"` -eq 0 ]
then
echo "Error: This particular record does not exist!!"
else
while [ "$choice" != "6" ]
do
update_menu #Display update menu for user input,allows update of individual field or all at once
read update_choice
case $update_choice in
"1") echo -en "Please enter employee's new name: "
read new_name
if [ -z $new_name ]
then
new_name=$oldname
elif [ `count_lines "^${new_name},"` -ne 0 ] #Check if name already exist in records
then
echo "Error: Employee [$new_name] already exist in records!"
else
grep -vi "$oldname" $PAYROLL > tmpfile #Perform updating to name field entry
grep -x "$line" $PAYROLL | sed -e "s/$oldname/$new_name/" >> tmpfile
mv tmpfile $PAYROLL
echo "Employee's name $oldname has been updated to [$new_name]!!"
fi
break
;; }
我所做的只是再添加一列。
Salary=`echo $line | cut -d "," -f5`