Reading a date from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a date from a file
# 1  
Old 02-27-2012
Reading a date from a file

Seems like this should be an easy thing to do (in bash shell), but can't get it figured out. We don't have an kind of scheduling functionality on our test system, so I'm trying to emulate having a process run every 8 hours, by using a control file with a date, and checking it every few minutes. Here's some psuedo-code for what I want to do:

Read date from a file (just needs to be year-month-day-hour, in whatever format will work)
If current datetime is >= value in the file...
Fire off my desired process
Add eight hours to date from the file
Write new date back to file
endif

I would think +%Y%m%d%H format would be best for comparing the dates.
And seems like 'date -d "8 hours"' would work for doing the right date math.
But, I can't figure out how to read the value from the file, and have it treated as a valid date for the "date -d" function.
# 2  
Old 02-27-2012
What system/shell are you using? You do not have cron?
# 3  
Old 02-27-2012
On GNU/Lunix, with bash. Cron is not allowed. Tidal scheduler is used on production, but a real pain to set up on dev for testing.
# 4  
Old 02-28-2012
try this
Code:
#!/bin/bash

sec2date()
{
     perl -e 'use POSIX qw(strftime);
              $mt = strftime "%c", localtime($ARGV[0]);
              print $mt,"\n";' $1

}
# start here
# assumes you want to run right now, then every 8 hours
#  %c is the current default locale's version for date

/usr/local/bin/date +%c  > datefile

# this is the number of seconds between runs 3600 sec = 1 hour
export lapse=$(( 3600 * 8 ))

# loop forever
while :
do
  testdate=$(cat datefile)                            # read date time from file
  when=$(/usr/local/bin/date --date="$testdate" +%s)  # turn date into seconds
  now=$(/usr/local/bin/date +%s)                      # right now in seconds
  
  if [[ $now -ge $when ]] ; then
     echo "running my job  here"   # change this to run a job
     when=$(( $when + $lapse ))
     sec2date $when > datefile
  fi
  
  sleep 1      
done

# 5  
Old 02-28-2012
Works like a charm! I was thinking this should be something doable in shell, without resorting to perl; but guess some of the date conversion tricks aren't that simple. Easy enough to slip in the perl code, though. Thanks!!
# 6  
Old 02-28-2012
If you have the strftime built-in added to your bash you can use that. Most sites do not do that. You need bash source, for examples see Chris FA Johnson's site:

Using and Writing Bash Dynamically Loadable Built-In Commands by Chris F.A. Johnson
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

2. UNIX for Dummies Questions & Answers

How to pick the latest file with date as one among the file name.( not exactly present date.)?

i have files like 1)20131112_abc_01.csv and 2)20131113_abc_01.csv and 3)20131113_abc_02.csv when i try to fetch the file in the next day. it shud pick the third file.. plz help me.. and i use `date +"%Y%m%d"` command to fetch..it fetches the current date... (2 Replies)
Discussion started by: applepie
2 Replies

3. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

4. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

5. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

6. UNIX for Dummies Questions & Answers

Help in reading the date from the input file name

Hi, I need to read the date from the input file. The format of the input file is as follows: a_b_c_yyyymmdd.txt I need to read the date(yyyymmdd) part from the name of the input file. Would really appreciate if someone can help me in this regard Thanks a lot. (1 Reply)
Discussion started by: Sunny_teotia
1 Replies

7. UNIX for Dummies Questions & Answers

issue on reading the file and appending date

Hi Am having issue on appending time stamp I know the exact file names in the directory like a.dat b.dat c.dat e.dat f.dat I want to read all these file names and append the timestamp to each files like a.dat.20090604,b.dat.20090604 and move to the different directory. ... (3 Replies)
Discussion started by: bobprabhu
3 Replies

8. UNIX for Dummies Questions & Answers

get the latest file by reading the date in the filename.

Hi, I grep for a pattern in a list of files. "grep -i -l $pattern *.datx*" it may give me n number of files. say for eg, it gives me 2 files. lock_eicu_20071228_00000000.dat_20071228_05343100 lock_eicu_20080501_00000000.dat_20080501_05343900 out of these 2 files I need to get the... (7 Replies)
Discussion started by: prsshini
7 Replies

9. UNIX for Dummies Questions & Answers

Move A File With Same Date,don't Change The Desitination Dir Date

Assume, I created one file three years back and I like to move the file to some other directory with the old date (Creation date)? Is it possible? Explain? (1 Reply)
Discussion started by: jee.ku2
1 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question