Need help to output the Chinese animal sign according to the year of birth
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need help to output the Chinese animal sign according to the year of birth
Hello.
I have wrote a bash shell script that will output your age in years and months, the day of the week you were born and the amount of days you have seen in the 21 century. I want the script to also output the Chinese animal sign according to your year of birth. The symbols repeat every 12 years. Example:
I was thinking about using a case statement along with something like this Sign=$(($Year % 12)). It's not really working out for me so can you guys help me out?
Thanks.
The $1 would be input from command line - to incorporate this in your script you can determine where to get the $year variable from - I just did the above so I'd have a self contained script.
The "% 12" is the modulo function that returns the remainder of the division. Since you're on a 12 year cycle you'll have repeating remanders 0 through 11 (12 different values) and Monkey is the one that has 0 (whereas Rat has a remainder of 4). So the script starts at Monkey even if that isn't where the Chinese calendar starts. It still gives you the right values.
Now I have a new problem. If I enter the date 18900831 I get an error. I think it's because of the method I used to get the day of the week the user was born. here's what I did; Dow=$(date -d "$BirthDate" | cut 1-4). BirthDate being what the user entered. It's in the format YYYYMMDD.
I have realized that the date command would not display a date of 1901 or earlier. Is there another way that I can get around this? Or another method I could use to output the day of the week the user was born?
Thanks in advance.
If you are to do a lot of date manipulation shell script is not a good choice - it is really not suitable for the task... You will end up spawning lots of processes to date and other programs to get your job done. It would be better to use a language which can manipulate dates more easily.
Perl is a good choice IMO since it is installed on most systems, and it has a few easy-to-use modules suited to the task, specifically Date::Parse and Date::Format. An example of it's use:
Code:
% cat test_program.pl
#!/usr/bin/perl
use strict;
use warnings;
use Date::Format;
use Date::Parse;
print time2str("Day of week is: %a. (%C)\n", str2time("20071021"));
% ./test_program.pl
Day of week is: Sun. (Sun Oct 21 00:00:00 BST 2007)
If you are to do a lot of date manipulation shell script is not a good choice - it is really not suitable for the task... You will end up spawning lots of processes to date and other programs to get your job done. It would be better to use a language which can manipulate dates more easily.
Perl is a good choice IMO since it is installed on most systems, and it has a few easy-to-use modules suited to the task, specifically Date::Parse and Date::Format. An example of it's use:
Code:
% cat test_program.pl
#!/usr/bin/perl
use strict;
use warnings;
use Date::Format;
use Date::Parse;
print time2str("Day of week is: %a. (%C)\n", str2time("20071021"));
% ./test_program.pl
Day of week is: Sun. (Sun Oct 21 00:00:00 BST 2007)
just curious, can these modules format dates before 1901? as what OP has requested?
So after ghostdog74's question, and the subsequent discovery that the Date::Parse / Date::Format modules don't seem to like dates before 1970, and I think probably after 2038 too, I found the Date::Calc module, which is happier with a wider range of dates (back to 1 AD). Here's my modified program. The only sacrifice using this module is that you must extract the year, month and day yourself from the date string. This is trivial in Perl however, using regular expressions (there are other methods too, but I like perl RE's so much I never miss a chance to use them).
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw(Day_of_Week Day_of_Week_Abbreviation);
foreach (qw(20071021 20060101 19700101 19000101 18000101 01000101)) {
if (/^(\d\d\d\d)(\d\d)(\d\d)$/) {
my $dow = Day_of_Week($1, $2, $3);
my $dayname = Day_of_Week_Abbreviation($dow);
printf("date=%s, dow=%s, day name=%s\n", $_, $dow, $dayname);
}
else {
warn "expected YYYYMMDD format, got: $_";
}
}
The output looks like this:
Code:
date=20071021, dow=7, day name=Sun
date=20060101, dow=7, day name=Sun
date=19700101, dow=4, day name=Thu
date=19000101, dow=1, day name=Mon
date=18000101, dow=3, day name=Wed
date=01000101, dow=5, day name=Fri
...use the numeric day of week if you prefer it, or the day name as you see fit.
I appreciate all the help guys, but the script must be in a bash shell. I was thinking about doing something with the calender such as;
cal $month $year | cut -c 1-2, which will single out "Sun" over the dates bellow it. I'm not sure how accurate it would be though because if I do something like this; cal 06 1984 | cut -c 1-2 | grep 1. it will display the 10 and 17th. I think this method would also require much mode code than I already have.
Hope you guys understand what I'm saying above, and remember that the script must be in a bash shell environment.
few possibilities
1) OP only knows bash and doesn't want to use anything else
2) doing assignments/homework where its usually under some restrictions
3) system only has bash ??
4) etc etc
few possibilities
1) OP only knows bash and doesn't want to use anything else
2) doing assignments/homework where its usually under some restrictions
3) system only has bash ??
4) etc etc
Yes I know, and I want to know which one.
Code:
case $reason in
1)
echo "/weird/, but whatever floats your boat..."
;;
2)
echo "Then I don't want to be too detailed."
echo "Yes, this is what I was asking really."
;;
3)
echo "Unlikely."
echo "Embedded systems would have a smaller shell."
echo "Other system would probably have at least perl."
;;
*)
echo "So let's hear it."
;;
esac
LOL, Case 2 is correct. At this point it would just be for future knowledge since I already submitted the assignment which was due before 11:59pm yesterday.
I'm still curious though.
Well, you could write your own date functions - perhaps there exists on the net a bunch of shell functions for this purpose which you can copy-paste. It's a bit of a pain in the backside though if you want to do it properly - handling localized versions of day names and so on. The western calendar is not so simple either as you go back - there are different calendars, skipped days and so on.
If you can write a library of bash functions for handling dates efficiently inside bash, I'm sure there would be a lot of interest - it is a very frequently asked "howto" question.
Incidentally, zsh has a pre-written module for sort of thing. zsh has a lot of nice extras like this. The only problem is that you can get addicted to them, and one day you need to work on a machine where you cannot use zsh and it feels like walking in sand.
I actually wrote a shell script once for a co-worker who didn't want to use Perl that did all its date calculations by using the "cal" command (and of course date to get today's date). Not pretty but functional.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.