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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-28-2017, 07:43 AM
|
#1
|
Member
Registered: Feb 2017
Posts: 38
Rep: 
|
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
|
|
|
03-28-2017, 07:53 AM
|
#2
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
try this:
Quote:
Originally Posted by slayer_1994
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.
|
03-28-2017, 07:59 AM
|
#3
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,952
|
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`
|
|
|
03-28-2017, 07:59 AM
|
#4
|
Member
Registered: Feb 2017
Posts: 38
Original Poster
Rep: 
|
Quote:
Originally Posted by schneidz
try this:
|
Great thank you  What was I doing wrong? I assume this is showing in seconds how would I show in minutes?
|
|
|
03-28-2017, 09:40 AM
|
#5
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
|
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.
|
03-28-2017, 12:52 PM
|
#6
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
also:
Quote:
Originally Posted by slayer_1994
Code:
diff_sec=$((current_time - last_login_of_tim))
|
|
|
|
03-28-2017, 01:01 PM
|
#7
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,952
|
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.
|
03-28-2017, 01:27 PM
|
#8
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
|
|
|
03-29-2017, 05:35 AM
|
#9
|
Member
Registered: Feb 2017
Posts: 38
Original Poster
Rep: 
|
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
|
|
|
03-29-2017, 09:34 AM
|
#10
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
|
Quote:
Originally Posted by slayer_1994
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.
|
All times are GMT -5. The time now is 04:26 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|