Bash script for printing days in a specific month (taking into account a leap year)
I have created a rather long winded bash script to print the number of days in the month, and give information about leap years if the current month is February. However I am having difficulty running it and need a better approach to scripting it if you can help? I am testing with if/elsif/else before trying a case function.
Code:
#!/bin/bash |
You can use "cal" to get the calendar for any month. In your script you're apparently only interested in the current month so the following would get number of days for current month:
Code:
cal |egrep -v [a-z] |wc -w You can also specify a given month/year with cal. So if you wanted to see if February 2012 was a leap year you could do: Code:
cal 2 2012 |egrep -v [a-z] |wc -w Code:
cal 2012 |egrep -v "[a-z]|[0-9][0-9][0-9]" |wc -w If interested only in the current year you can specify it with the date command: Code:
cal $(date +%Y) |egrep -v "[a-z]|[0-9][0-9][0-9]" |wc -w |
Thanks for that MensaWater. I will use that in the future as much simpler. However I am practising some bash scripting with "if/else" statements and was looking for a simpler way to condense the script and achieve the same result as using "cal".
|
M1=$01 sets the value of M1 to the value of variable 0 with literal string 1 added to it. Try M1=01 etc.
|
Code:
#!/bin/bash |
Thanks for that info catkin as I needed to some assistance in trying to formulate an approach to scripting this issue.
Code:
#!/bin/bash The current month is April leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found Programming error: $month not in expected 01-12 range: 04 It has a problem with the final "fi" in the script. |
Personally I would change the ifs into a case to make it cleaner looking:
Code:
#!/bin/bash |
No you need to check your typing:
Code:
if (( $year % 400] != 0 )); then |
@grail: the OP is practising with if-elif-else-fi before moving on to case. And well spotted on that typo :)
|
Quote:
I wanted to try "if/else" statements before moving on to "case" statements. |
FYI leap year check If I remember correctly is:
if(year%400 ==0 || (year%100 != 0 && year%4 == 0)) |
Quote:
Code:
if ( $year % 400 == 0 ) || ($year % 100 != 0 && $year % 4 == 0); then Code:
if ( ($year % 400 == 0 ) || ($year % 100 != 0 && $year % 4 == 0) ); then gives the same output error: leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found leaptest.sh: 40: [[: not found Programming error: 04 not in expected 01-12 range: 04 Line 40 is : Code:
38 else |
Sorry, I did not put the formula in proper bash syntax.
Code:
if (( ($year % 400) == 0 )) || (( ($year%4) == 0 ) && ($year%100) !=0) )); then |
Quote:
Code:
if (( ($year % 400) == 0 )) || (( ($year%4) == 0 ) && ($year%100) !=0) )); then The test expression brackets need to be changed. The nested if/else statement for leap year is the issue as the syntax doesn't allow the script to run and results in previous error messages. |
Quote:
It would appear that the code in post #6 works just fine for me: Code:
$ ./leap_year.sh |
Does this work?
Code:
if (( ($year % 400) == 0 )) || (( $year % 4 == 0 && $year % 100 !=0 )); then |
Quote:
Code:
root@localhost:~/script# ./leaptest.sh Code:
root@localhost:~/script# sh leaptest.sh |
Quote:
Code:
root@localhost:~/script# ./lyear.sh Code:
root@slocalhost:~/script# sh lyear.sh |
When invoked as sh (which may or may not be a link to bash on your system), bash runs in POSIX mode and the full bash feature set is not available. Try bash 1year.sh
|
Quote:
I will add this to the script also to see if I can get user input in the form of one argument (enter a year in the terminal to check if it is a leap year.) Code:
#!/bin/bash |
Did it using the read function but would like to know is it possible to do so from adding an argument to the script itself? (i.e. ./lyear.sh 2000)
Code:
#!/bin/bash |
Something like
Code:
if [[ $1 = '' ]]; then |
I am a bit lost at what you want?
Code:
year=`date +%Y` You have solved with the read option which takes the entered value and assigns it to the variable supplied. How about you take out the middle man and apply it to the variable yourself, which would then be a parameter from the command line :) |
Quote:
|
Quote:
|
Here is a possible way to check you are getting up to 4 and only 4 digits:
Code:
regex='^[1-9][0-9]{,3}$' |
Clean way to do it using cal
As seen here :
YEAR=2020 MONTH=2 for DAY in $(DAYS=`cal $MONTH $YEAR | awk 'NF {DAYS = $NF}; END {print DAYS}'` && seq -f '%02G' $DAYS) ;do DATE="$YEAR-$MONTH-$DAY" echo $DATE done |
@kieranpilot - You have posted to a thread which has had no activity in six years. Although your information may be relevant it is best to start your own thread if you are experiencing a similar problem to attract attention of currently active members.
Additionally, advertising is not permitted in the forums, so your promotional post has been moderated. If you wish to advertise please visit the LQ Jobs Marketplace or contact us via the link on any page. |
Let GNU date do the calculations:
Code:
currmonth=`date +%Y-%m` |
All times are GMT -5. The time now is 09:35 AM. |