I’m trying to write a code where an organization is organizing an virtual event with a raffle and is identifying a subset of the attendees
that born in a certain month. Therefore, the event organizer needs to list only eligible members. The
members’ information is given in a CSV file, and the record format in this data file is as follow:
firstName,lastName,YYYYMMDD
However, the year can either be in a Gregorian calendar, where the current year is 2022, or a Buddhist calendar, where the current year is 2565.
2
Answers
There are (at least) two reasons why the script doesn’t show anything with the example data.
d="${line##,}
, as this just tries to remove a leading comma. The line should be:Finally, I’m not sure that
echo "%sn" "$line"
is going to give the intended output. You probably confusedecho
andprintf
.There are several issues with the code in the question, including syntax errors, logic errors, and some stylistic issues (e.g. use of magic numbers, unnecessary repetition). This Shellcheck-clean code attempts to fix most of them:
10#
inbirth_month=${months[10#${yyyymmdd:4:2}]}
forces the month number to be treated as a decimal (base 10) number. Otherwise08
and09
will cause errors as invalid octal numbers. See Value too great for base (error token is "08").May guest.csv
and theguest.csv
contents given in the question because the only person born in May is only 7 years old.