我的脚本会询问姓名,电话号码和出生日期,然后将这些详细信息修改为名为“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
但是我不确定是什么导致不排序?
答案 0 :(得分:1)
首先删除“$(currentDay)”和“$(currentDay)”
周围的括号答案 1 :(得分:0)
你有一个错误,至少:
if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$(currentDay)" -lt "$($birthDay)" ]]
"$(currentDay)"
应该只是"$currentDay"
而"$($birthDay)"
应该只是"$birthDay"
。
你这里也有一个错字:
currentMonth=${currenDate:0:2}
那应该是currentDate
。将set -u
放在脚本的顶部以捕获此类错误。
另外,就像Mat说的那样,我没有看到任何排序代码:)