我的脚本要求输入姓名、电话号码和出生日期,然后将这些详细信息修改为一个名为“birthday.csv”的逗号分隔值文件。
然后它按出生日期对“birthday.csv”进行排序。然后显示新排序的文件。它还计算每个人的年龄和文件中的条目数。
我的问题是,它接受了所有信息,但是在打印内容之前它不会对文件进行排序。
这是代码:
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
但我不确定是什么导致它无法排序?