Reputation: 87
My script asks for a name, phone number, and date of birth and then amends these details to a comma separated value file called “birthday.csv”.
It then Sorts “birthday.csv” by date of birth. The newly sorted file is then be displayed. It also calculates the age of each person and the number of entries in the file.
My problem is, it takes in all the information, but it then will not sort the file before printing the contents.
Here is the code:
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;
I think the problem lies with
if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$(currentDay)" -lt "$($birthDay)" ]]
then
let Age=currentYear-birthYear-1
else
let Age=currentYear-birthYear
fi
But I'm not sure what is causing that to not sort?
Upvotes: 1
Views: 277
Reputation: 312
Firstly remove the brackets around "$(currentDay)" and "$(currentDay)"
Upvotes: 1
Reputation: 182000
One bug you have, at least:
if [[ "$currentMonth" -lt "$birthMonth" ]] || [[ "$currentMonth" -eq "$birthMonth" && "$(currentDay)" -lt "$($birthDay)" ]]
"$(currentDay)"
should be just "$currentDay"
and "$($birthDay)"
should be just "$birthDay"
.
You also have a typo here:
currentMonth=${currenDate:0:2}
That should be currentDate
. Put set -u
at the top of your script to catch such mistakes.
Also, like Mat said, I don't see any sorting code :)
Upvotes: 0