LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-28-2017, 07:43 AM   #1
slayer_1994
Member
 
Registered: Feb 2017
Posts: 38

Rep: Reputation: Disabled
Date not returning


Hi There

I am trying to do the difference of two file dates, however when I do the some the return is blank.

Any help would be greatly appreciated.

Code:
current_time=`date +%s`
last_login_of_tim=`date -d @1489662376 +%s`

diff_sec=`(($current_time-$last_login_of_tim))`

echo $current_time
echo $last_login_of_tim
echo $diff_sec
Quote:
[rp1cem@wycvlapph036 self_monitoring]$ ./test.sh
1490704960
1489662376
Cheers
 
Old 03-28-2017, 07:53 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
try this:
Quote:
Originally Posted by slayer_1994 View Post
Hi There

I am trying to do the difference of two file dates, however when I do the some the return is blank.

Any help would be greatly appreciated.

Code:
current_time=`date +%s`
last_login_of_tim=`date -d @1489662376 +%s`

diff_sec=$(($current_time-$last_login_of_tim))

echo $current_time
echo $last_login_of_tim
echo $diff_sec


Cheers
 
1 members found this post helpful.
Old 03-28-2017, 07:59 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,952
Blog Entries: 13

Rep: Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984
Doing math like that is your problem.

Try something like, and note that you need the white spaces around the minus sign:
Code:
diff_sec=`expr $current_time - $last_login_of_tim`
 
Old 03-28-2017, 07:59 AM   #4
slayer_1994
Member
 
Registered: Feb 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
try this:

Great thank you What was I doing wrong? I assume this is showing in seconds how would I show in minutes?
 
Old 03-28-2017, 09:40 AM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Your syntax for Arithmetic Expansion is incorrect. I suspect you are assuming that `command` == $(command) is equivalent in similar cases.
However, while Command Substitution may be implemented with `command` or $(command), Arithmetic Expansion is only implemented by doing $((expression))

Using `command` is poor form however. It's hard to read and cannot be nested.
 
3 members found this post helpful.
Old 03-28-2017, 12:52 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
also:
Quote:
Originally Posted by slayer_1994 View Post
Code:
diff_sec=$((current_time - last_login_of_tim))
 
Old 03-28-2017, 01:01 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,952
Blog Entries: 13

Rep: Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984Reputation: 4984
An important point to observe here is that you did not know where the error resided in your script.

If you add a "set -xv" line near the start of the script, it would have enabled verbose output and informed you that there was a problem with your mathematical attempt. From there any of the offered solutions to fix that line are applicable.
 
1 members found this post helpful.
Old 03-28-2017, 01:27 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Some date math in http://www.linuxquestions.org/questi...rt-4175544200/ may help.
 
Old 03-29-2017, 05:35 AM   #9
slayer_1994
Member
 
Registered: Feb 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
Hi Guys

Thanks for all your help on this much appreciated

Usually for time you take the first 10 characters of a files name, what if the file I am looking at has the date within it?
File 'Wynyard_MTP_Primary-btstats-2017-03-29-10:34:50.xml'

This is the file I want to do the same the difference between the dates but as it has the date already in it won't I need to do something different?

Cheers
Alex
 
Old 03-29-2017, 09:34 AM   #10
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by slayer_1994 View Post
Hi Guys

Thanks for all your help on this much appreciated

Usually for time you take the first 10 characters of a files name, what if the file I am looking at has the date within it?
File 'Wynyard_MTP_Primary-btstats-2017-03-29-10:34:50.xml'

This is the file I want to do the same the difference between the dates but as it has the date already in it won't I need to do something different?

Cheers
Alex
Now you're looking to match a string.
That can be done with grep and regular expressions. Character Classes and Bracket Expressions may help you here (look in man grep)

For example, to match 4 digits, followed by a dash, then 2 digits I would do something like:

Code:
$ echo 'hello 5555-33 world' | grep -oE [[:digit:]]{4}-[[:digit:]]{2}
5555-33
Code:
       -E, --extended-regexp
              Interpret PATTERN as an extended regular  expression  (ERE,  see
              below).

       -o, --only-matching
              Print  only  the  matched  (non-empty) parts of a matching line,
              with each such part on a separate output line.
 
1 members found this post helpful.
  


Reply

Tags
date, newbie, scripting, shell script, unix


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
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 08:02 AM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 08:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:26 PM.

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