LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2012, 12:22 PM   #16
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895

Does this work?
Code:
if (( ($year % 400) == 0 )) || (( $year % 4 == 0 && $year % 100 !=0 )); then
  echo "This is a leap year"
else 
  echo "this is not a leap year"
fi
 
Old 04-25-2012, 02:09 PM   #17
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
Quote:
Originally Posted by grail View Post
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
Any idea why?
 
Old 04-25-2012, 02:16 PM   #18
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
Quote:
Originally Posted by michaelk View Post
Does this work?
Code:
if (( ($year % 400) == 0 )) || (( $year % 4 == 0 && $year % 100 !=0 )); then
  echo "This is a leap year"
else 
  echo "this is not a leap year"
fi
Yes, the code works when run as:
Code:
root@localhost:~/script# ./lyear.sh 
The current month is April
04 has 30 days
but not when run as:
Code:
root@slocalhost:~/script# sh lyear.sh 
The current month is April
lyear.sh: 13: Syntax error: word unexpected (expecting ")")
I am confused now as I thought you had to specify the script was a bash script before running?!
 
Old 04-25-2012, 08:43 PM   #19
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 04-26-2012, 05:11 AM   #20
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
Quote:
Originally Posted by catkin View Post
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"?

Last edited by shayno90; 04-26-2012 at 05:38 AM.
 
Old 04-26-2012, 06:57 AM   #21
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
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
 
Old 04-26-2012, 07:07 AM   #22
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Something like
Code:
if [[ $1 = '' ]]; then
    read -p 'Enter year > ' year_in
else
    year_in=$1
fi
For robustness your script could check that year_in is an integer before doing arithmetic on it -- and an unsigned integer for sanity.
 
Old 04-26-2012, 07:19 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am a bit lost at what you want?
Code:
year=`date +%Y`
getyear=("$1"(year))
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
 
Old 04-26-2012, 08:10 AM   #24
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
Quote:
Originally Posted by grail View Post
I am a bit lost at what you want?
Code:
year=`date +%Y`
getyear=("$1"(year))
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).
 
Old 04-26-2012, 08:26 AM   #25
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by shayno90 View Post
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.
 
Old 04-26-2012, 10:51 AM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here is a possible way to check you are getting up to 4 and only 4 digits:
Code:
regex='^[1-9][0-9]{,3}$'

user_date=$1

while [[ ! $user_date =~ $regex ]]; do read -p "Invalid value, please try again :> " user_date; done
 
Old 08-22-2018, 07:43 PM   #27
kieranpilot
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
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
 
Old 08-23-2018, 02:35 AM   #28
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@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.
 
Old 08-23-2018, 05:50 PM   #29
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Let GNU date do the calculations:
Code:
currmonth=`date +%Y-%m`
days=`date -d "$currmonth-01 + 1 month - 1 day" +%d`
echo "The current month is $currmonth and has $days days"
 
1 members found this post helpful.
  


Reply

Tags
bash scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to print out "n" days ago's year, month and day? ArthurHuang Programming 3 02-10-2011 08:33 PM
Is it a leap year? jdwalk Linux - Newbie 4 02-22-2010 06:06 PM
LXer: Taking the Vista leap? LXer Syndicated Linux News 0 05-13-2008 09:20 AM
LXer: Taking the Vista leap? LXer Syndicated Linux News 0 05-13-2008 08:40 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration