这个脚本不会正确地排序按年龄 [英] This script wont sort correctly by age

查看:146
本文介绍了这个脚本不会正确地排序按年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本要求输入姓名,电话号码和出生日期,然后修正这些细节名为birthday.csv逗号分隔值文件。

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".

据然后通过出生日期排序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.

下面是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;

我认为问题在于

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?

推荐答案

首先去掉括号$(CURRENTDAY)和$(CURRENTDAY)

Firstly remove the brackets around "$(currentDay)" and "$(currentDay)"

这篇关于这个脚本不会正确地排序按年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆