[SOLVED] Bash script for printing days in a specific month (taking into account a leap year)
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.
hmmm ... can see a typo but not read the questions and answers
It would appear that the code in post #6 works just fine for me:
Code:
$ ./leap_year.sh
The current month is April
04 has 30 days
Is that the same way you are running the code?
Yes running the script from the script directory works as you run it but not the other way!
Code:
root@localhost:~/script# ./leaptest.sh
The current month is April
04 has 30 days
but not this way
Code:
root@localhost:~/script# sh leaptest.sh
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: 04 not in expected 01-12 range: 04
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
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
Thanks I invoked the wrong bash shell when running the script!
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
# This script will test the user input to see if we're in a leap year or not.
year=`date +%Y`
getyear=("$1"(year))
if [ ! $getyear == `date +%Y` ]; then
echo "Year must be in the format YYYY!"
exit
fi
if [ $[$getyear % 400] -eq "0" ]; then
echo "This is a leap year. February has 29 days."
elif [ $[$getyear % 4] -eq 0 ]; then
if [ $[$getyear % 100] -ne 0 ]; then
echo "This is a leap year, February has 29 days."
else
echo "This is not a leap year. February has 28 days."
fi
else
echo "This is not a leap year. February has 28 days."
fi
How can I get the user input to read in the format of "date +%Y"?
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
# This script will test the user to see if we're in a leap year or not.
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
if (( ("$year" % 400 == "0") )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )); then
echo "$year is a leap year"
else
echo "This is not a leap year"
fi
How were these ever to be the same? Also the second causes an error when run at the command line, even if you turn $1 into just 1.
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
How were these ever to be the same? Also the second causes an error when run at the command line, even if you turn $1 into just 1.
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
I wasn't sure how to assign the argument from the command line (i.e., ./lyear 2000) and then ensure the argument (2000) is passed/formatted according to `date +%Y` (YYYY) (of course I don't want the result from date, just how to format it like date).
I wasn't sure how to assign the argument from the command line (i.e., ./lyear 2000) and then ensure the argument (2000) is passed/formatted according to `date +%Y` (YYYY) (of course I don't want the result from date, just how to format it like date).
Both the argument passed from the command line and the date output are strings so no special formatting is required.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.