Write a shell script where User input this birthday and output will be his next 4-5 years birthday with which day[mon-sunday]

I've tried in java also bash. In java it workS 60% just show the day with leap year based.But in bash it doesn't work properly[yeap year issue ].

//JAVA CODE
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("Enter Your Birthday;");
        System.out.println("follow [month-date-year] sequence:");
        Scanner sc=new Scanner(System.in);
        String date=sc.next();
        //FILL CODE
        Birthday a= new Birthday();
        System.out.println(a.findDay(date));

    }
}
//
import java.util.Date;
import java.text.*;
public class Birthday {
    public String findDay(String date)
    {
        //FILL CODE
        SimpleDateFormat sf = new SimpleDateFormat("MM-dd-yy");
        sf.setLenient(false);
        try {
            Date date1=sf.parse(date);
            sf=new SimpleDateFormat("EEEE");
            return sf.format(date1);

        } catch(ParseException e) {
            return "Invalid Date";
        }
    }
}

Hello,

Welcome - we hope you find this to be a friendly and helpful place. I see you have another thread related to this same question open presently, so most likely I will close the most recent one and link it back to this one, as generally we want to have one thread per topic, with there being little real benefit to having multiple threads open for the same thing.

In any case, the first question we have to ask is whether or not this is related to academic work, schoolwork, homework or coursework of any kind ? If so, then we can still help you, but this needs to be very openly and clearly declared up-front from the very beginning.

Secondly, we really do need to see the actual Bash you are having trouble with. There doesn't appear to be much point in opening a thread about a shell scripting question, and then giving us Java source code. You mention the Bash "doesn't work properly", so please show us what you've written, as well as samples of the exact input data it reads, or a sample of a full interactive session of you running the script, and we will perhaps find that we are in a position to be able to assist you further.

So, if you could please address all of the above points for us, we can then take things from there.

2 Likes
#!/bin/sh
echo "Enter YOUR DATE: "  
echo "Follow this[yearmonthdate] format:" 
read d  
day=${d#??????}
temp=${d#????}
month=${temp%??}
year=${d%????}
DOW=$(cal $month $year | awk '
BEGIN{split("Sunday Monday Tuesday Wednesday Thursday Friday Saturday",dow);dow[0]=dow[7]}
NR==3{t=7-NF+d;print dow[t%7];exit}' d=$day)
echo $DOW

I got this code on this website. but if I input 29 February,
2003 which is not leap year it show the day
,

image

here java input and output
image

Hello,

Thank you for providing your Bash code. However, you've not covered the first point that I asked for clarification on: is this coursework, schoolwork, homework or academic work of any kind whatsoever ? If it is, then that needs to be made clear before we proceed any further. So if you could confirm that one way or the other, that would be great. Thanks !

2 Likes

sorry, I forget . This is my classwork . Course CODE - CSE 310

1 Like

faculty: Mr. Md. Ashiqur Rahman
Southeast University, Dhaka
Course code-CSE 310

1 Like

Do you mind showing us where you found this shell script on our forum, I am missing something here...
Have you tried with other dates and even years? I would not be surprised you found the output be Saturday...

In log in option it logged me here. Isn't these are similar website??

Hello,

Out of interest, is there a particular reason you went for the awk-based solution there, rather than the (potentially far simpler) GNU date-based one that is mentioned on that same thread ? Personally, if I wanted to get the long-form day of the week for a known or given date, and I was on a system with the GNU utilities available, I'd go with the date approach every time.

yeah I've tried other dates .It show the day but when i input leap year then it show wrong
image

the 13th march 2020 is a friday...
What I wanted you see is that on non leap year with that script, 29 FEB = 01 MAR = same day
So its not all that wrong: the day after the 28th Feb 2003 is a Saturday...
Hmmm interesting no? So how will you correct the script now?

I added to the script what was missing at the beginning, nothing more, and this is my output:

Vics-MacBook-Pro: $ ./dow
Enter YOUR DATE:
Follow this[yearmonthdate] format:
20030229
D=29 M=02 Y=2003
month=02, Feb
invalid date
Vics-MacBook-Pro: $ ./dow
Enter YOUR DATE:
Follow this[yearmonthdate] format:
20030228
D=28 M=02 Y=2003
month=02, Feb
Friday
Vics-MacBook-Pro: $ ./dow
Enter YOUR DATE:
Follow this[yearmonthdate] format:
20030301
D=01 M=03 Y=2003
Saturday
Vics-MacBook-Pro: $ ./dow
Enter YOUR DATE:
Follow this[yearmonthdate] format:
20040229
D=29 M=02 Y=2004
Leap Year
Sunday
Vics-MacBook-Pro: $ ./dow
Enter YOUR DATE:
Follow this[yearmonthdate] format:
19570421
D=21 M=04 Y=1957
Sunday
Vics-MacBook-Pro: $

So there isn't much missing to give the output you are looking for: just a loop:

$ ./dow
Enter YOUR DATE:
Follow this[yearmonthdate] format:
20220419
D=19 M=04 Y=2022
Tuesday
Wednesday
Friday
Saturday

While at it:
As you have pointed out the leap year case, how will you treat those unlucky/lucky babies born on the 29th Feb?
As your intent was to output his next 4 coming birthdays
Now if you wanted to give the Day of the birth of a person AND his next 4-5 birthdays to come, once here I see no difficulties...
Curious to see how you solved the problem

Faculty mention birthday, so I think you are right. If he said find the day of user date then it will be e problem. I'm noob in shell script so I can't think other option to slove this problem
.

So here, as I am of French speaking language, not sure what I should understand:
Birthday = anniversary or day of birth?
It changes things as it anniversary, year is current and so you display the day of birthday this year and coming 3-4.

the case of 29.02 is pending... but if you dont need to give the year, the person born on that day usually choose another day for birthday for all administrative purpose and so you could consider the date as " Not being treated "
in other word the person born on that day knows very well the coming issue and if he enters 29.02 it is just to see what happens so displaying Not being treated is, a way to say you know the problem and it is at that person to give the answer: on what day do you celebrate when not leap year...

So your task is if you want to use that shell script, as I did, you will have to write a bit of code before or more precisely in between the variables declaration and input - and - the awk lines:

That is to write a test on 29.02, if you are to enter the year then your test need to treat the leap year ( if leap year, if month 02 if day 29... now you have it all)
To not make a fool of you, I would test year in between 1901-2099 and display Year=1901-2099 to avoid complicated calculation to find leap year, in between those 2 values its dead easy...
When you are into writing script and you want to be sure what is going on, display using echo every bit of process, this is the beginning of debugging tricks

my result: look at what is displayed...

vbes-MacBook-Pro:~ $ ./dow
Enter YOUR DATE: (between 1901-2099)
Follow this[yearmonthdate] format:
19970229
D=29 M=02 Y=1997
Valid=1
not a leap year
month=02, Feb
invalid date
vbes-MacBook-Pro:~ $
vbes-MacBook-Pro:~ $ ./dow
Enter YOUR DATE: (between 1901-2099)
Follow this[yearmonthdate] format:
19970228
D=28 M=02 Y=1997
Valid=1
not a leap year
month=02, Feb
1997 = Friday
1998 = Saturday
1999 = Sunday
2000 = Monday
2001 = Wednesday
vbes-MacBook-Pro:~ $ ./dow
Enter YOUR DATE: (between 1901-2099)
Follow this[yearmonthdate] format:
20000229
D=29 M=02 Y=2000
Valid=0
Leap Year
2000 = Tuesday
vbes-MacBook-Pro:~ $

And to get you working and showing I did use the script you gave
Here is with blanks the part of the script with my test that produces above output, up to you to figure out what is missing, but you see my usage of echo... or you write your own test following your ideas ( yes many solutions...)
I don't write bash shell to be the most portable possible, so you will see what many consider obsolete writing, feel free to correct, it will show you have read and understood what is going on if you can rewrite in more current style

#!/bin/sh
echo "Enter YOUR DATE: (between 1901-2099)"
echo "Follow this[yearmonthdate] format:"
read d

day=${d#??????}
temp=${d#????}
month=${temp%??}
year=${d%????}
let i=0 # Used after for displaying the 4 years

echo D=$day" "M=$month" "Y=$year

let Valid=(calculation to find leap year)
echo Valid=$Valid
if [ $Valid ...... ]
then
   echo  " not a leap year"
   if [ "$month" .... ]
   then
      echo month=$month, Feb
      if [ $day ...     ]
      then
         echo invalid date
         exit 2
      fi
   fi
else
   echo "Leap Year"
   let i=4
fi

Being a noob is no excuse to not try to progress and work, so now the next thing we want to see is the test completed or your test proposal with its output, all published here, so we can validate your work or correct you if still some issues but I believe there should not be because you have it all, in order to continue further.

Then next step ( only once this one is successful... ):
Are you to use awk etc.. or as suggested using date is accepted ( if you have GNU date...)?

This could be an acceptable solution:
You display day of birth and the current plus next 3 birthdays day

$  ./dow
Enter YOUR DATE: (between 1901-2099)
Follow this[yearmonthdate] format:
19640228
D=28 M=02 Y=1964
Valid=0
Leap Year
1964 = Friday
2022 = Monday
2023 = Tuesday
2024 = Wednesday
2025 = Friday
$  ./dow
Enter YOUR DATE: (between 1901-2099)
Follow this[yearmonthdate] format:
19640229
D=29 M=02 Y=1964
Valid=0
Leap Year
When leap year birthday, current and following years display 1rst March for non-leap year
1964 = Saturday
2022 = Tuesday
2023 = Wednesday
2024 = Thursday
2025 = Saturday

Especially if you test that the year entered is within the values 1901-2099... I am sure your teacher would appreciate such effort...

image
ok , thank you.

OK so we close the thread