LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 04-25-2012, 12:22 PM   #16
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,561

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682

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: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
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: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
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: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,520
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
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: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
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: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,520
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,202

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
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: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
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: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,520
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,202

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
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
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 10:19 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration