ksh date math


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh date math
# 1  
Old 12-20-2010
ksh date math

I have a requirement to remove all files from a directory execpt those >
(now -N hrs).

Once I can figure out how to calculate (now -N hrs) I was thinking of creating a temporary file with that time (now-N hrs) than using the find and -newer option.

Does anybody have any KSH code (needs to run on AIX, HPUX and Solaris), which can do this date math. If would be nice if it can return the
format back in the way the touch command expects, if not I guess I can
change the code to the format I need.

Thanks in advance to all who answer.
# 2  
Old 12-20-2010
This User Gave Thanks to vbe For This Post:
# 3  
Old 12-20-2010
VBE,

Great links, thanks.

I found what I needed but its with perl. Can somebody show me how I can
call this within a KSH script.

I want to do something like this calc_time=$(perl code .....)

Thanks



Code:
 
#!/usr/bin/perl -w
use strict;
use POSIX;
print strftime "%Y-%m-%d %H:%M:%S\n", localtime((time() - 7200));

# 4  
Old 12-20-2010
Code:
result=$(
/usr/bin/perl -w -e '
use strict;
use POSIX;
print strftime "%Y-%m-%d %H:%M:%S\n", localtime((time() - 7200)); ' 
)

echo "$result"

perl -e '[command goes here]' is for an inline command.

Except the strftime command produces a time string will not work with touch. touch puts an mtime timestamp on a file.
Should be (not checked):
Code:
"%Y%m%d%H%M%S"

which is YYYYmmddhhmiss

Check your manpage; some older versions of touch require touch -t YYYYmmddhhmi.ss [filename] <- note the extra dot

Last edited by jim mcnamara; 12-20-2010 at 12:35 PM..
This User Gave Thanks to jim mcnamara For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

2. Shell Programming and Scripting

ksh field math

Hi, I have a text like with many rows of data like: 7a,0,1182 7a,1,1040 7b,0,483 7b,1,242 7c,0,224 7c,1,877 I need to be able to take the value of the first field (i.e. 7a) and if there are multiples, add the value of the third field, (1182 + 1040) and insert the output of all of this in... (2 Replies)
Discussion started by: scriptr2be
2 Replies

3. UNIX for Dummies Questions & Answers

Unable to convert date into no. using date -d +%s syntax in ksh shell

hi friends, I m trying to write a script which compares to dates. for this i am converting dates into no using synatx as below v2=`date | awk '{print $2,$3,$4}'` v3=`date +%s -d "$v2"` this syntax is working in bash shell ,but fails in ksh shell. please suggest on this. (12 Replies)
Discussion started by: Jcpratap
12 Replies

4. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

5. Shell Programming and Scripting

Math action in ksh

Hello, I want to do div action in ksh and get the full result(with leftover). i.e. 4/3=1.33 and not 4/3=1 (4 Replies)
Discussion started by: LiorAmitai
4 Replies

6. Shell Programming and Scripting

How to Get 60 days Old date from current date in KSH script

Hi i am writing a cron job. so for it i need the 60 days old date form current date in variable. Like today date is 27 jan 2011 then output value will be stote in variable in formet Nov 27. i am using EST date, and tried lot of solution and see lot of post but it did not helpful for me. so... (3 Replies)
Discussion started by: Himanshu_soni
3 Replies

7. Shell Programming and Scripting

Why won't my Ksh do math with (( ))?

Hello, I'm usring Ksh on AIX 5.3. For some reason my K-Shell gives me an error when I try to use the math operators (( )). Can anyone tell me what's going on and how to fix it? Thanks so much! My K-Shell: />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10... (2 Replies)
Discussion started by: troym72
2 Replies

8. Shell Programming and Scripting

Need help with Date calculations in ksh

Hi Gurus, I am writing a script where we enter two dates, one a FROM DATE and the other a TO DATE. I need to validate that difference between the two dates is always less than or equal to 60 days. I could not find any date utility in ksh that could help me with this. Finally, I am deciding... (5 Replies)
Discussion started by: jidsh
5 Replies

9. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

10. UNIX for Advanced & Expert Users

date program in ksh

#Author : kkodava #!/usr/bin/ksh #Use of this program is You can findout the no of days & day of starting and ending dates #usage no_of_days startdate<yyyymmdd> enddate<yyyymmdd> syy=`echo $1|cut -c1-4` smm=`echo $1|cut -c5-6` sdd=`echo $1|cut -c7-8` eyy=`echo $2|cut -c1-4` emm=`echo... (1 Reply)
Discussion started by: krishna
1 Replies
Login or Register to Ask a Question