How to get timestamp of file into ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get timestamp of file into ksh script
# 1  
Old 04-25-2009
How to get timestamp of file into ksh script

Hi -
I need to run a script via cron which will do some ldapsearches. One of the search parameters will be modifytimestamp.

I need to have some way of storing the timestamp of the last time the script ran, and then retrieving that timestamp to use in the ldapsearch command (i.e. modifytimestamp >= retrieved_ts) for when the script next runs.

Any suggestions please? I guess I could "touch" a file as a marker before and after executing the ldapsearch but I am not sure how to get the timestamp of the file into the ldapsearch query.
Cheers
# 2  
Old 04-25-2009
how does your query look like. Is it in some predefined format like hh:mm

you can always get the timestamp from ls -l filename| awk '{sub(/:/,"",$8);print $8}'

store this into a variable and then compare it with the time from the date command
echo `date +"%k%M"`

cheers,
Devaraj Takhellambam
# 3  
Old 04-25-2009
Or with the stat command:

Code:
retrieved_ts=$(stat -c %y file | cut -d' ' -f1)

# 4  
Old 04-25-2009
Thanks for the replies.

Unfortunately I don't have the stat command available in my AIX envt.

The timestamp needs to be in this format : 20090425000000
so yyyymmddhhmmss

Maybe it would be easier to just write/append the timestamp to a file and read the last entry from that? I just feel there has to be a neater way of doing this!

How do I get the timestamp in the format I want from something like :
ls -l $FILE | awk '{sub(/:/,"",$8);print $8}' ?
# 5  
Old 04-25-2009
If the seconds don't matter you can do something like:

Code:
ls -l file | awk '{s=$6$7;gsub("-| |:","",s);print s}'

If you want two zero's after the time you can add "00" after the s in the print statement.

Last edited by Franklin52; 04-25-2009 at 08:38 AM..
# 6  
Old 04-25-2009
Quote:
Originally Posted by sniper57
Thanks for the replies.

Unfortunately I don't have the stat command available in my AIX envt.

The timestamp needs to be in this format : 20090425000000
so yyyymmddhhmmss

Maybe it would be easier to just write/append the timestamp to a file and read the last entry from that? I just feel there has to be a neater way of doing this!

How do I get the timestamp in the format I want from something like :
ls -l $FILE | awk '{sub(/:/,"",$8);print $8}' ?

what you can possibly do is ..in you ldap script, before it completes successfully, touch the logfile so tht the file will have the latest timestamp (you can write sme msg to the logfile)

then use

lastrun=`ls -l --time-style='full-iso' add | awk '{s=$6$7;gsub(/[:-]/,"",s);print substr(s,1,14)}' `
now=$(echo `date +"%Y%m%d%H%M%S"`)

then compare the above two variable

if [[ $now < $lastrun ]]; then
echo "Greater"
else
echo "Lower"
fi


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Script Closest Timestamp (LOG FILE)

Hi All, I need something where I could take the date from FILE1 and in this case the date is Thu Sep 10 13:48:42 EDT 2015 and I need it to match the closest PREVIOUS date in the log file FILE2 this means in this specific case it would be Thu Sep 10 2015 13:35:28. I only need the closest... (3 Replies)
Discussion started by: roberto999
3 Replies

2. Shell Programming and Scripting

How to get the timestamp for file using shell script

I am writing a script to move the executable from one node to another and take the backup of the existing one. For that I want to fetch the timestamp of file using shell script. Could anyone help? (2 Replies)
Discussion started by: gagankinra
2 Replies

3. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

4. Shell Programming and Scripting

Getting a relative timestamp from timestamp stored in a file

Hi, I've a file in the following format 1999-APR-8 17:31:06 1500 3 45 1999-APR-8 17:31:15 1500 3 45 1999-APR-8 17:31:25 1500 3 45 1999-APR-8 17:31:30 1500 3 45 1999-APR-8 17:31:55 1500 3 45 1999-APR-8 17:32:06 1500 3 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

5. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

6. UNIX for Advanced & Expert Users

In ksh find out the timestamp of current date?

Normally we can use %s to find out the time in second since 1970. But in my ksh, this format option is not available. Example- date +%s 1268103151 above script command won't work in ksh. Can you guys provide its equivalent ? (3 Replies)
Discussion started by: boy18nj
3 Replies

7. Shell Programming and Scripting

Timestamp to date conversion in ksh

Hi, I have a file containing timestamp( Example given below). How can i get date(mmd-dd-yyyy) from it? ($> cat file1.txt 2008-11-24 05:17:00.7043) Thanks, Sri (2 Replies)
Discussion started by: srilaxmi
2 Replies

8. UNIX for Advanced & Expert Users

how to get timestamp of a file in a perl script

I have seen this posting here: perl -e '@d=localtime ((stat(shift))); printf "%02d-%02d-%04d %02d:%02d:%02d\n", $d,$d+1,$d+1900,$d,$d,$d' file1.txt I need to use it inside a perl script. Can anybody please help me out. (5 Replies)
Discussion started by: biswajeet.beher
5 Replies

9. AIX

timestamp in .sh_history using ksh shell

Hello Everyone: Does anyone know how I will setup my account to put timestamp in my .sh_history? I do not hold the root account. I am using AIX 5 and ksh shell. I tried every solution I can find in the internet but nothing seems to work OR I am just applying those in the wrong way. Anyone knows... (4 Replies)
Discussion started by: Orbix
4 Replies

10. Shell Programming and Scripting

I want to get timestamp from a text file using ksh

for example : 07:25 Thanks (1 Reply)
Discussion started by: vinna
1 Replies
Login or Register to Ask a Question