LinuxQuestions.org
Visit Jeremy's Blog.
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 03-17-2009, 01:15 PM   #1
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Rep: Reputation: 15
Bash scripting question


I've started writing a script that checks for folder changes. In the end this script will email me when something has been created in the folder (right now, because I can't figure that out, it checks for modifications to the folder). I'm not very good at bash scripting but this is my terrible work in progress:

Code:
date='date +%F'
time='date +%l%M'
moddate='stat -c %y /home/abc/incoming/*'

echo 'Date is: '
$date
echo 'Time is: '
$time
echo 'Mod date of all files in /home/abc/incoming are:'
$moddate
Is there any way I can create a variable that has the date in it, minus fifteen minutes? I would like to to able to have this script email me any time something is changes (so if the mod date is greater than my variable minus 15 minutes, it will mail me. This will be used in a cron and run every 10 minutes).

Can I get any further guidance?
 
Old 03-17-2009, 02:00 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Sure you can. It's called arithmetic expansion. See the line I added in red for example.
Code:
date=`date +%F`
time=`date +%l%M`
time2=`expr $(( $time - 15 ))`
moddate=`stat -c %y /home/abc/incoming/*`

echo "Date is: $date"
echo "Time is: $time"
echo "Time less 15 minutes is: $time2"
echo "Mod date of all files in /home/abc/incoming are:"
echo "$moddate"
Also, note that when running a command in a subshell such as your fist line, you use the backtick (the key next to the 1), not the single quote.
 
Old 03-17-2009, 02:14 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
time=`date +%l%M`
time2=`expr $(( $time - 15 ))`
Uhhm, this doesn't work, because the time format isn't a decimal number. It's currently 3:10 am where I am, and your formula gives me a time of "296".

You need to format the time in minutes, then convert it back to h+m after subtracting the 15. I might try to work out how to do it myself, but it's past my bedtime here. I'll leave it to someone who knows more about manipulating times.

PS: Using $(command) for command substitution is even better than using backticks, which are now discouraged in modern bash scripting.

Last edited by David the H.; 03-17-2009 at 02:18 PM.
 
Old 03-17-2009, 02:32 PM   #4
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
Cool, thanks for the replies. I have modified it, but I can't get the new time2 variable to work since I cahnged the outputting of my time to match that of the stat command.

Script:
Code:
date=$(date +%F)
time=$(date +%H:%M:%S)
time2=$(expr $(( $time -0.15 )))
moddate=$(stat -c %y /home/abc/incoming/*)

echo 'Date is: '$date
echo 'Time is: '$time
echo 'Mod date of all files in /home/abc/incoming are:'
echo $moddate
Output:
Code:
./script.sh: line 3: 08: value too great for base (error token is "08")
Date is: 2009-03-17
Time is: 08:15:12
Mod date of all files in /home/abc/incoming are:
2009-03-16 11:47:17.000000000 -0700 2009-03-12 08:16:46.000000000 -0700
EDIT:

Managed to get it to do what I want with this code:
Code:
date=$(date +%F)
time0=$(date +%H:)
time1=$(date +:%S)
time2=$(date +%M)
time3=$(expr $(( $time2 -15 )))
timeFinal=$time0$time2$time1
moddate=$(stat -c %y /home/abc/incoming/*)

echo 'Date is: '$date
echo 'Time is: '$timeFinal
echo 'Mod date of all files in /home/abc/incoming are:'
echo $moddate
echo 'Time minus 15 minutes: '$time0$time3$time1
now is there a way to check the date (date AND time) against the output of $moddate to filter out which files have been updated and which have not?

Output of new code:
Code:
Date is: 2009-03-17
Time is: 08:27:21
Mod date of all files in /home/abc/incoming are:
2009-03-16 11:47:17.000000000 -0700 2009-03-12 08:16:46.000000000 -0700
Time minus 15 minutes: 08:12:21

Last edited by hawk__0; 03-17-2009 at 02:46 PM.
 
Old 03-17-2009, 02:34 PM   #5
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 7,392

Rep: Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416
Quote:
Is there any way I can create a variable that has the date in it, minus fifteen minutes?
Probably simpler using unix timestamps. For example, type "date +%s" in a terminal (without quotes) and you will get a response similar to: 1237313861 which is the unix timestamp for whatever time you enter the command. To get the time 15 minutes earlier you simply subtract 900. Unix timestamps are measured in seconds, 15 (minutes) times 60 (seconds) = 900.

To start your script getting time:


Code:
#!/bin/bash

d1=`date +%s`
d2=$(($d1 - 900))
echo $d2
You can see the output by simply typing the "date +%s" in a terminal and after having saved the above 3 lines in a bash executable, execute it and look at the two 10 digit outputs. The difference should be close to 900, takes a few seconds to enter the command so it won't be exact.

There really isn't any need to convert to Year, Month, Day format if all yu want is to find files modified in last 15 minutes. Just use find command with -mtime option. Also, in the above script there will be no need for the "echo" line, that's just so you can see what it does.
 
Old 03-17-2009, 03:13 PM   #6
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
Yes, I'm aware of the echo thing. I just liek to know waht's going on with my code so I put them in temporarily. Genius! I completely forgot about unix time. Way better solution.

But how would I go about using mtime to find times greater than 15 minutes ago, or just display all mtimes?
 
Old 03-17-2009, 03:53 PM   #7
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by hawk__0 View Post
Is there any way I can create a variable that has the date in it, minus fifteen minutes?
Use syntax like this:
Code:
#!/bin/bash
MDATE=$(date +"%F" -d "now -15 minutes")
MTIME=$(date +"%l%M" -d "now -15 minutes")
MODDATE=$(stat -c %y ~/lq/*)

echo "Date is: $MDATE"
echo "Time is: $MTIME"
echo "Mod date of all files in ~/lq/ are:"
echo "$MODDATE"
 
Old 03-17-2009, 06:09 PM   #8
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 7,392

Rep: Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416Reputation: 1416
Quote:
find /home/abc/incoming/* -mmin -15
This should tell you which files in that directory were modified in the last 15 minutes. Not really sure that's what you're looking for?
 
Old 03-17-2009, 06:35 PM   #9
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Quote:
Originally Posted by David the H. View Post
Uhhm, this doesn't work, because the time format isn't a decimal number. It's currently 3:10 am where I am, and your formula gives me a time of "296".
Duh, good point. I tested it when my local time was after the quarter hour! I was more focused on the arithmetic expansion part of the question rather than the correct time format.
 
  


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
Bash scripting question flagg0204 Programming 1 11-19-2005 04:31 PM
Bash Scripting Question fiod Linux - Newbie 4 11-19-2005 06:09 AM
bash scripting question Red Squirrel Linux - General 1 07-11-2005 10:57 PM
Bash Scripting Question jsimmonds Programming 2 06-16-2005 10:07 AM
bash scripting question mehesque Programming 2 03-07-2004 02:37 PM

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

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