problem doing the time calculations in shell.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem doing the time calculations in shell.
# 1  
Old 05-16-2007
problem doing the time calculations in shell.

I am trying to do a shell script to monitor if any files went through in the last hour.

There is a script in cron that runs every sec checking to see if a file is there and ftp the file out of this folder.

Now I just want to add a block of code that will check to see no files went in the last hour send an email.

ftp----------> folder to monitor ----------->ftp

This is what i came up with so far. I am getting a little problem calculating the time. If someone could help me out here please.



Code:

Code:
#Set the path to the folder to monitor
SUBJECT="DWH FILES WERE NOT SENT IN THE LAST HOUR"
FILE1="/home/tibco/javastuff/test/dwh*"


cd /home/tibco/javastuff/test/

###### Run list command
ls -l

time=`echo $line | awk '{ split($8,a,":"); print (a[1]*60)+a[2] }'`
FILEMATCH=`echo $line | awk '{ print $9 }'`


if [ $FILE1 = $FILEMATCH ]
  then
     echo "$time" > dwhtime.txt

     else
######## Run date command ##########
          date
          time2=`echo $line | awk '{ split($4,a,":"); print (a[1]*60)+a[2] }'`
       if [ $time2 -gt $time ]
           then
           mail -s "$SUBJECT" testemail <<EOF
          $SUBJECT
          $time
EOF
        fi

fi

# 2  
Old 05-16-2007
Jonathan,
One way you can do this is:
Code:
for newFile in `find . -type f -newer special_named_file`
do
  echo "File = "$newFile
done
touch special_named_file

# 3  
Old 05-16-2007
thats a good idea i will do that. Abou tthe time calcuations, do you have any idea how i should do this

In the code i am taking the time used in the ls -l command for that file and I am writing the time to a file, so when another file comes through it will overwrite the old time. So my issue is when no files comes for an hour I want it send an email

So would it be $time2 - $time1 = $b

if $b is greater than 60 mins then send email

How would i do this part?
# 4  
Old 05-16-2007
You missed the whole point of Shell_Life's comment. The idea is to ask "find" for a list of files that are newer than 60 minutes. If that list is empty, you send mail. Touching the file after the loop finishes would work if your script is run every 60 minutes. If that is not the case, you could construct a timestamp 60 minutes in the past, and touch the file with the -t option at the start of the script. But to answer your question, the easiest way to check if a file is 60 minutes old or not would be to use perl. Here are a couple of perl one liners you can use in your script:
Code:
$ perl -e '@d=localtime ((stat(shift))[9]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' file1.txt
20070204053859
$ perl -e '@d=localtime time()-3600; printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'
20070516121610
$

The first outputs a timestamp for the file mentioned as the last argument. The second output a timestamp 60 minutes in the past.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mathematical calculations using shell

Dear All, I read some variables in a file and assigned as name for each of them. If I do echo I am able to see the values as 1.0E-05,3.4,5.0E-03 etc, Now I want to do some mathematical operations with them. Lets say 1 1.0E-05*5.0E-03 expected ans is 5.0E-08 2 1.0E-05/5.0E-03 expected... (9 Replies)
Discussion started by: linuxUser_
9 Replies

2. Shell Programming and Scripting

Problem with calculations

grep Quality abc.txt | awk -F"=" '{print $2}' o/p is given as 70/70 49/70 I want in the below format (percentage format) 100% 70% help me!!!!:confused::confused::confused: ---------- Post updated at 09:59 AM ---------- Previous update was at 09:57 AM ---------- Cell 01 -... (3 Replies)
Discussion started by: nikhil jain
3 Replies

3. Shell Programming and Scripting

Adding time to date time in UNIX shell scipting

I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as... (3 Replies)
Discussion started by: codehelp04
3 Replies

4. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

5. UNIX for Dummies Questions & Answers

Doing calculations with bc on one field

Hello, I have to turn: Apple Inc.:325,64:329,57 into Apple Inc.:325,64:329,57:3,93 3,93=329,57-325,64. My code: cat beurs.txt | sed 's/\(*\):\(*\),*\(*\):\(*\),\(*\)/\4\.\5-\2\.\3/' beurs.txt | bc| tr '.' ',' | sed 's/^-*,/0,/' > winstmarges.txt; paste -d: beurs.txt winstmarges.txt; rm... (5 Replies)
Discussion started by: ikke008
5 Replies

6. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

7. Shell Programming and Scripting

shell execution time calculations

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script. Does anyone have any examples they have used. I have been setting up two date variables (one at... (1 Reply)
Discussion started by: Omar Bacha
1 Replies

8. UNIX for Advanced & Expert Users

Time Calculations & Conversions

I have script that runs based on time variables passed in the command line. The first command argument is a timer, in seconds, of how often to execute a certain loop in the script. The second command argument is the end time of the script, in military time. Below is an example of the command line:... (1 Reply)
Discussion started by: Nysif Steve
1 Replies

9. UNIX for Dummies Questions & Answers

Time Calculations

I'm trying to have a loop print out statistics every X number of seconds. How can I add a specific number of seconds to a time variable and make a comparison? Thanks ahead of time. For example: startTime = `date +%H%M%S` currentTime = $startTime executeTime = startTime + X # X is equal... (5 Replies)
Discussion started by: Nysif Steve
5 Replies

10. Shell Programming and Scripting

Time difference calculations

Hi All.. Does anyone have a useful function where I can enter two date/timestamps and it calculates the difference in time in hours, minutes and seconds between the 2? Any feedback much appreciated. :D Kind Regards Satnam (1 Reply)
Discussion started by: satnamx
1 Replies
Login or Register to Ask a Question