LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2013, 09:25 AM   #16
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444

Save/Restore your IFS..

Code:
OrigIFS="$IFS"
IFS=-
 .....
 .....
IFS="$OrigIFS"
As it might interfere with things you do later in your script
 
Old 09-26-2013, 09:29 AM   #17
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
SO i am here
Code:
string=$1
IFS=-
set $string
year=$3
mounth=$2
day=$1
an=`date +%Y`
anne=`expr $an - $year`
echo "Age : $anne ans"
how i can find how many days it rest until his next birthday?
 
Old 09-26-2013, 09:38 AM   #18
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
Quote:
Originally Posted by sparky90ful View Post
SO i am here
Code:
string=$1
IFS=-
set $string
year=$3
mounth=$2
day=$1
an=`date +%Y`
anne=`expr $an - $year`
echo "Age : $anne ans"
how i can find how many days it rest until his next birthday?
Looking at your code, your calculation for the age is based on a wrong assumption.

You can't just subtract years to get someones age.

For example, my birthday is 16th October, so until that date I'm actually a year younger than a simple subtraction.

Have another think around what colucix suggested, "seconds since epoch".
 
Old 09-26-2013, 09:40 AM   #19
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
add a year
find difference from today ( in seconds )
do maths to get from seconds to days

for fun, instead of exiting count down the remaining seconds
read about printf
http://wiki.bash-hackers.org/commands/builtin/printf
( in all honesty,. I skimmed that.. not certain how good a read it is )
 
Old 09-26-2013, 09:44 AM   #20
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
until now i have this
Code:
string=$1
IFS=-
set $string
year=$3
mounth=$2
day=$1
an=`date +%Y`
luna=`date +%m`
anne=`expr $an - $year`
if [[ $luna < $mounth ]]
	then 
	anne=`expr $an - $year + 1`
fi
echo "Age : $anne ans"
9

now i have to find how many days it is until his next birthday how do i do that (1 year have 365 days not much not less)

ps:that is good for ages ??

Last edited by sparky90ful; 09-26-2013 at 09:45 AM.
 
Old 09-26-2013, 09:49 AM   #21
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
it got my age wrong
 
Old 09-26-2013, 09:50 AM   #22
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
Quote:
Originally Posted by sparky90ful View Post
ps:that is good for ages ??
Still doesn't take into account the day of the month. Run it for a birthday of yesterday in 2000 and of tomorrow in 2000 and you'll see.
 
Old 09-26-2013, 09:50 AM   #23
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
it got my age wrong
why where is the error for my byrthday date is god
 
Old 09-26-2013, 09:56 AM   #24
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
Quote:
Originally Posted by Firerat View Post
it got my age wrong
Mine too.
 
Old 09-26-2013, 09:57 AM   #25
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Mine too.
wrong with 1 in plus or with one in minus

Code:
string=$1
IFS=-
set $string
year=$3
mounth=$2
day=$1
an=`date +%Y`
luna=`date +%m`
anne=`expr $an - $year`
if [[ $luna < $mounth ]]
	then 
	anne=`expr $an - $year - 1`
fi
echo "Age : $anne ans"
now is good?

Last edited by sparky90ful; 09-26-2013 at 09:59 AM.
 
Old 09-26-2013, 09:58 AM   #26
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
dates are awful for calulations

it has been hinted at a few times now

you need to use the date command

man date
Code:
       %r     locale's 12-hour clock time (e.g., 11:11:04 PM)

       %R     24-hour hour and minute; same as %H:%M

       %s     seconds since 1970-01-01 00:00:00 UTC

       %S     second (00..60)

       %t     a tab
and ..
1 - -1 = 2
 
Old 09-26-2013, 09:59 AM   #27
Pap
Member
 
Registered: May 2011
Distribution: Salix 14.1 GNU/Linux, 64-bit
Posts: 70

Rep: Reputation: 28
The following script will compute and print the time elapsed since the date you give as an argument and present time:
#!/bin/bash
Code:
function elapsed_time {
  local user_date=$(date --date=$1 +%s)
  local now=$(date +%s)
  local dt=$((now-user_date))
  local dyears=$((dt / 31536000))
  if [ $dyears -gt 0 ];then echo -n "$dyears years, ";fi
  dt=$((dt % 31536000))
  local dmonths=$((dt / 2592000))
  if [ $dmonths -gt 0 ];then echo -n "$dmonths months, ";fi
  dt=$((dt % 2592000))
  local ddays=$((dt / 86400))
  if [ $ddays -gt 0 ];then echo -n "$ddays days, ";fi
  dt=$((dt % 86400))
  local dhours=$((dt / 3600))
  if [ $dhours -gt 0 ];then echo -n "$dhours hours, ";fi
  dt=$((dt % 3600))
  local dminutes=$((dt / 60))
  if [ $dminutes -gt 0 ];then echo -n "$dminutes minutes, ";fi
  dseconds=$((dt % 60))
  echo $dseconds seconds.
}
echo -n "Elapsed time: ";elapsed_time $1
Save it as, say, "elapsed_time", make it executable, and provide the date as in the example below:
Code:
./time_elapsed 12-May-1993
Elapsed time: 20 years, 4 months, 22 days, 17 hours, 3 minutes, 8 seconds.
As you can see, the script expects a date in the dd-mmm-yyyy format, so it calculates time elapsed till the beginning of the specified day; for example, time elapsed from 26-Sep-2013, 00:00, till the time I am typing this is:
Code:
./time_elapsed Sep-26-2013
Elapsed time: 17 hours, 12 minutes, 40 seconds.
With some modifications, the script can do much more things, such as automatically calculating the time till next Christmas, or the time till your next birthday, but I'll leave that as an exercise to the reader

Last edited by Pap; 09-26-2013 at 10:16 AM.
 
Old 09-26-2013, 10:08 AM   #28
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,166
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Quote:
Originally Posted by sparky90ful View Post
is what i must create file.sh is the script writed by me and when user is run it with some parameter i must have the result for nomather which will be the parameter for now i have this
Code:
string=$1
IFS=-
set $string
year=$3
mounth=$2
day=$1
y must compare with the actual date to find the age
Date formats are different for different regions of the planet.
Is that expected input always supposed to be MM-DD-YYYY? (05-12-1993)
That may be taken as Dec 12, 1993 in Europe...I think.(I'm an American m-d-y is the 'norm' here.)

You may wish to advise the user of the expected input format (echo "Please use MM-DD-YYYY as the format")
Search MINPARAMS here...

some hints for "date math"
Code:
date +"%m-%d-%Y"
date +"%m-%d-%Y" --date="+6 months +29 days"
date +"%m-%d-%Y" --date="+365 days"
date +"%m-%d-%Y" --date="+364 days"
date +"%m-%d-%Y" --date="+11 months 26 days"
Very flexible.

That should keep you from scratching your eyes out.

Good Luck and let us know...

and what Pap posted.

Last edited by Habitual; 09-26-2013 at 10:09 AM.
 
Old 09-26-2013, 10:11 AM   #29
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by sparky90ful View Post
wrong with 1 in plus or with one in minus


now is good?
afraid not
try these two dates

28-09-1900
28-10-1900

they should give the same answer ( today is 26-09-2013 dd-mm-yyyy )
but they don't
 
Old 09-26-2013, 10:12 AM   #30
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Pap View Post
The following script will compute and print the time elapsed since the date you give as an argument and present time:
#!/bin/bash
Code:
function elapsed_time {
  local user_date=$(date --date=$1 +%s)
  local now=$(date +%s)
  local dt=$((now-user_date))
  local dyears=$((dt / 31536000))
  if [ $dyears -gt 0 ];then echo -n "$dyears years, ";fi
  dt=$((dt % 31536000))
  local dmonths=$((dt / 2592000))
  if [ $dmonths -gt 0 ];then echo -n "$dmonths months, ";fi
  dt=$((dt % 2592000))
  local ddays=$((dt / 86400))
  if [ $ddays -gt 0 ];then echo -n "$ddays days, ";fi
  dt=$((dt % 86400))
  local dhours=$((dt / 3600))
  if [ $dhours -gt 0 ];then echo -n "$dhours hours, ";fi
  dt=$((dt % 3600))
  local dminutes=$((dt / 60))
  if [ $dminutes -gt 0 ];then echo -n "$dminutes minutes, ";fi
  dseconds=$((dt % 60))
  echo $dseconds seconds.
}
echo -n "Elapsed time: ";elapsed_time $1
Save it as, say, "elapsed_time", make it executable, and provide the date as in the example below:
Code:
pap@debian:/pap/scripts$ ./time_elapsed 12-May-1993
Elapsed time: 20 years, 4 months, 22 days, 17 hours, 3 minutes, 8 seconds.
With some modifications, the above script can also be used to compute the time till your next birthday, but I'll leave that as an exercise to the user

it gives me this
Code:
01-r3p05:ex_4 florea_g$ ./incercare.sh 15-May-1990
Elapsed time: date: illegal option -- -
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
43 years, 9 months, 9 days, 14 hours, 10 minutes, 50 seconds.
301-r3p05:ex_4 florea_g$
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Date and Time Calculations in Perl (or other) taylor_ma Programming 9 05-03-2012 09:27 PM
[SOLVED] Date Calculations using script!! anishkumarv Linux - Newbie 9 09-24-2011 04:36 PM
Using Perl to perform calculations in Bash w1k0 Programming 5 06-07-2009 09:10 PM
Date calculations on Sun sparc ukursat Linux - Newbie 1 12-05-2008 11:01 PM
Date calculations in BASH script Crashman Programming 4 07-03-2004 11:15 AM


All times are GMT -5. The time now is 06:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration