LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Date not returning (https://www.linuxquestions.org/questions/linux-newbie-8/date-not-returning-4175602695/)

slayer_1994 03-28-2017 07:43 AM

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

schneidz 03-28-2017 07:53 AM

try this:
Quote:

Originally Posted by slayer_1994 (Post 5689200)
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


rtmistler 03-28-2017 07:59 AM

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`

slayer_1994 03-28-2017 07:59 AM

Quote:

Originally Posted by schneidz (Post 5689208)
try this:


Great thank you :) What was I doing wrong? I assume this is showing in seconds how would I show in minutes?

Sefyir 03-28-2017 09:40 AM

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.

ondoho 03-28-2017 12:52 PM

also:
Quote:

Originally Posted by slayer_1994 (Post 5689200)
Code:

diff_sec=$((current_time - last_login_of_tim))


rtmistler 03-28-2017 01:01 PM

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.

Habitual 03-28-2017 01:27 PM

Some date math in http://www.linuxquestions.org/questi...rt-4175544200/ may help.

slayer_1994 03-29-2017 05:35 AM

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

Sefyir 03-29-2017 09:34 AM

Quote:

Originally Posted by slayer_1994 (Post 5689675)
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.



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