LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-02-2004, 07:53 PM   #1
Crashman
LQ Newbie
 
Registered: Feb 2004
Location: Misery ummm Missouri =)
Distribution: Slackware 9.1
Posts: 9

Rep: Reputation: 0
Angry Date calculations in BASH script


Hey all,

I've been fiddling with this way to long now with no luck. It seems like it should be simple, but my brain must think otherwise.....

I'm trying to perform a simple date calculation in a BASH script that tells how many days from now until my anniversary... before I forget that too!

I thought a simple script like this would work:

let y=date --date'03 Oct' +%j
let x=date +%j
let z=`expr $y - $x`
echo "You have $z days untill your anniversary"


But, BAH... doesn't work at all.

Any help would be greatly appreciated.
Thanks
 
Old 07-02-2004, 09:01 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Easily corrected...

Code:
#!/bin/bash

y=$(date --date '03 Oct' +%j)
x=$(date +%j)

((z=${y} - ${x}))

echo "You have ${z} days until your anniversary"

exit 0
 
Old 07-02-2004, 09:12 PM   #3
Crashman
LQ Newbie
 
Registered: Feb 2004
Location: Misery ummm Missouri =)
Distribution: Slackware 9.1
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks alot for that... you can't believe how nuts that one snippet of code has been driving me. LOL

Sometimes syntax is taxing....

Crashman
 
Old 07-02-2004, 09:41 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Yup, I know the feeling. I've come close to ripping out my hair at times trying to get the right number of backslashes for certain kinds of strings of text. Many here have shared your grief at one time or another.

As a side note, I thought I should also mention you can use backticks instead of the $( ) format. For example:
Code:
y=`date +%j`
It's a little less intrusive (visually speaking), but can cause some syntax grief because it's commonly mistaken as a single quote character.

Anyway, glad it's working for you. Just make sure the wife doesn't know you're "cheating"
 
Old 07-03-2004, 10:15 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I don't fully agree on the advice to give up the $() syntax.

Actually, it is an improvement of the original single backquote one, with which it was very hard to use imbricated scripts:

ex:
$(command $(cmd2 $(cmd3)))
`command \`cmd2 \\\`cmd3\\\`\``

It is also easier to find the start and ending part of the subcommand with editors like vi that jumps to the other end (command %).

Finally, it also has a useful shortcut $(<file) equivalent to `cat file`.

my 2 cents
 
  


Reply



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
Get file modification date/time in Bash script cmfarley19 Programming 16 04-15-2015 06:25 PM
Perl/bash - calculate date in future rose_bud4201 Programming 6 05-10-2009 05:59 AM
using bash in cron to get the date in filename rstoutmna Programming 1 12-29-2004 02:39 PM
Bash: add date to output line blizunt7 Programming 7 11-10-2004 03:45 AM
Date in a BASH script... soulsniper Linux - Software 7 11-22-2003 06:08 PM

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

All times are GMT -5. The time now is 02:45 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