1

我的脚本要求输入姓名、电话号码和出生日期,然后将这些详细信息修改为一个名为“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

但我不确定是什么导致它无法排序?

4

2 回答 2

1

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

于 2013-01-18T14:57:03.890 回答
0

您至少有一个错误:

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

"$(currentDay)"应该是公正"$currentDay"的,"$($birthDay)"应该是公正"$birthDay"的。

你这里还有一个错字:

currentMonth=${currenDate:0:2}

那应该是currentDate。放在set -u脚本的顶部以捕获此类错误。

另外,就像 Mat 说的,我没有看到任何排序代码 :)

于 2013-01-17T22:56:38.427 回答