这个脚本不会按年龄正确sorting

我的脚本要求input姓名,电话号码和出生date,然后将这些详细信息修改为一个名为“birthday.csv”的逗号分隔值文件。

然后按出生datesorting“birthday.csv”。 然后显示新sorting的文件。 它还计算每个人的年龄和文件中的条目数量。

我的问题是,它包含所有的信息,但是在打印内容之前,它不会对文件进行sorting。

这里是代码:

a=0 while [ $a -lt 2 ]; do echo Please enter a first name read firstName echo Please enter last name read lastName echo Please enter phone number read phoneNumber echo Please enter date of birth - format dd/mm/yyyy read dob echo "$firstName,$lastName,$phoneNumber,$dob" >> Birthdays.csv echo If you would like to add another person press 1 or enter 2 to proceed read a done INPUT=./Birthdays.csv OLDIFS=$IFS IFS="," [ -f ${INPUT} ] && while read Name Surname Telephone DOB do birthMonth=${DOB:0:2} birthDay=${DOB:3:2} birthYear=${DOB:6:4} currentDate=`date +%d/%m/%Y` currentMonth=${currenDate:0:2} currentDay=${currentDate:3:2} currentYear=${currentDate:6:4} if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$(currentDay)" -lt "$($birthDay)" ]] then let Age=currentYear-birthYear-1 else let Age=currentYear-birthYear fi echo "Name : $Name" echo "Surname : $Surname" echo "Telephone : $Telephone" echo "DOB : $DOB" echo "Age : $Age" echo "##########################################" done < $INPUT IFS=$OLDIFS echo $DATE exit 0; 

我认为问题在于

 if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$(currentDay)" -lt "$($birthDay)" ]] then let Age=currentYear-birthYear-1 else let Age=currentYear-birthYear fi 

但我不确定是什么原因导致不能sorting?

Solutions Collecting From Web of "这个脚本不会按年龄正确sorting"

首先删除“$(currentDay)”和“$(currentDay)”周围的括号

你有一个bug,至少:

 if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$(currentDay)" -lt "$($birthDay)" ]] 

"$(currentDay)"应该只是"$currentDay""$($birthDay)"应该只是"$birthDay"

你在这里也有一个错字:

 currentMonth=${currenDate:0:2} 

那应该是currentDate 。 把你的脚本的顶部set -u来捕捉这样的错误。

另外,像马特说,我没有看到任何排序代码:)