KSH script Not working (calculate days since 1/1/2000 given day 4444)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH script Not working (calculate days since 1/1/2000 given day 4444)
# 1  
Old 03-28-2012
KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10.

If I do a grep -i version /usr/dt/bin/dtksh I get
@(#)Version M-12/28/93d
@(#)Version 12/28/93
@(#)Version M-12/28/93

This is correct for ksh93...right?

Script that doesn't work:

#!/usr/dt/bin/dtksh

printf "%(%d %b %Y)T\n" "Jan 1 2000 + 4444 days"

Output (this is all I get):
(0

I should get 02 Mar 2012


Also, can someone tell me how to add a variable in place of 4444? Can I just do '$num_days'
# 2  
Old 03-28-2012
Umm, I have Version M-12/28/93e and I get:

Code:
$ printf "%T\n" now
ksh93: printf: T: unknown format specifier

So I'm guessing you need a later version for this feature.

Your probably going to have to resort to perl for this sort of thing, eg from_epoch.pl:

Code:
#!/usr/bin/perl
use Time::Local;
use POSIX qw(strftime);
print POSIX::strftime("%d %b %Y\n", localtime(timelocal(0,0,0,1,0,2000) + $ARGV[0]*24*3600));

Code:
$ ./from_epoch.pl 4444
02 Mar 2012


Last edited by Chubler_XL; 03-28-2012 at 11:05 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-28-2012
Thanks for info...I'll give it a try tomorrow. Will this perl code work with any version...I am using Solaris 10. Just curious because someone else gave me some perl code and I couldn't get it to work. Also, could someone tell me how to save this output to variable? Is it possible to run your code first and then run some csh code I already have written (can I just enter #!/usr/bin/csh and append my code to your perl code...in other words have one script that has both perl and csh)? If this isn't possible how do you write the output of the perl code to a file?
# 4  
Old 03-29-2012
I don't have Solaris 10 to test this on but considering it's only using
Time and POSIX there is a very good change it will work fine.

From a csh script you should be able to do.

Code:
set num_days=4444
set mydate=`perl -e 'use Time::Local;use POSIX qw(strftime);print POSIX::strftime("%d %b %Y\n", localtime(timelocal(0,0,0,1,0,2000) + $ARGV[0]*24*3600));' $num_days`

# 5  
Old 03-29-2012
That's great...thanks so much. Just curious do I need set num_days=4444 or $num_days at the end of the perl code? Won't $ARGV[0] will be my number of days from the user?
# 6  
Old 03-29-2012
I just set num_days in the example to show you how a shell variable is passed into the perl program. Typically you would be doing some sort of calculation or assignment from the script's arguments into num_days.

We then put $num_days on the end of the perl command line and this can be accessed as ARGV[0] from within perl. ARGV[0] here isn't a parameter to the csh script but rather the first parameter passed on the perl command line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in ksh Script to list files older than 365 days from specified directories

Requirement is to list the files older than 365 days from multiple directories and delete them and log the list of files which are deleted to a log file. so 1 script should only list files older than 365 days for each directory separately to a folder The other script should read these files... (7 Replies)
Discussion started by: prasadn
7 Replies

2. Shell Programming and Scripting

Working out days of the week and processing file in 3 working days

Hi guys i need advice on the approach to this one...... I have a file say called Thisfile.20130524.txt i need to work out from the date 20130524 what day of the week that was and then process the file in 3 working days. (so not counting saturday or sunday....(will not worry about bank... (2 Replies)
Discussion started by: twinion
2 Replies

3. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help! (3 Replies)
Discussion started by: kovacsakos
3 Replies

4. Shell Programming and Scripting

Calculate the calendar date since Jan 1, 2000

Does anyone know how to calculate a calendar date since Jan 1, 2000 (this is day 1). I am using CSH with a Solaris system (no GNU products installed). Example: Input from the user (number of days): 4444 Output (dd mmm yy) 02 mar 12 ---------- Post updated at 09:40 PM ----------... (9 Replies)
Discussion started by: thibodc
9 Replies

5. Shell Programming and Scripting

Calculate 30/31 days from today date script

Hi Guys, I was working some time ago n was in need to calculate date 30/31 days from today including Feb (Leap yr stuff). Today date is variable depending on day of execution of script. I tried searching but was not able to get exactly what I needed....So at that I time I implemented by my own... (3 Replies)
Discussion started by: coolgoose85
3 Replies

6. Shell Programming and Scripting

Script to calculate user's last login to check if > 90 days

I need a script to figure out if a user's last login was 90 days or older. OS=AIX 5.3, shell=Korn Here's what I have so far: ==== #!/usr/bin/ksh NOW=`lsuser -a time_last_login root | awk -F= '{ print $2 }'` (( LAST_LOGIN_TIME = 0 )) (( DIFF = $NOW - $LAST_LOGIN_TIME )) lsuser -a... (3 Replies)
Discussion started by: pdtak
3 Replies

7. Shell Programming and Scripting

calculate server uptime in % (99.98), using ksh script

Let me preface by saying, I have looked through many threads that deal with keep the decimal, however I'm not sure that any one resolution meets my needs, ok, ok, they could. So maybe it's just that I am not understanding the resolution - therefore I am posting a new thread. myknowledgebase=at... (2 Replies)
Discussion started by: cml2008
2 Replies

8. Shell Programming and Scripting

ksh script using expr to calculate percentages

Within a ksh script on HP-UX I trying to calculate a percentage of a number (number/100 x percentage) using the below method and expr. TARPERC=`expr 16 / 100 \* 5` TARSUM=`expr 16 + $TARPERC` ZIPSUM=`expr $TARSUM \* 2` If the input is 16 outputs are: TARPERC: 0 TARSUM: 16 ZIPSUM: 32... (6 Replies)
Discussion started by: wurzul
6 Replies

9. Shell Programming and Scripting

set Working day in ksh

Hello guys it´s a pleasure to type with the unix community...I´m new in shell script and I need to insert into a #!/ksh a statment that will check if a file that I´ll receive from another script is arriving in the first working day of each month: let´s say that I´ll reveive the following files... (1 Reply)
Discussion started by: Rafael.Buria
1 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question