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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unable to convert date into no. using date -d +%s syntax in ksh shell
# 1  
Old 06-06-2012
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
Code:
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.

Last edited by Franklin52; 06-06-2012 at 07:24 AM.. Reason: Please use code tags
# 2  
Old 06-06-2012
date is a unix command not a Shell command. Only the GNU date command has conversion features. There is a FAQ on this.
What Operating System and version do you have?
# 3  
Old 06-06-2012
Hi Methyl,
Operating System details
SunOS PRTWLS23 5.8 Generic_117350-51 sun4u sparc SUNW,Sun-Fire-480R

---------- Post updated at 06:05 AM ---------- Previous update was at 05:58 AM ----------

Hi Methyl,
can you please post link of that FAQ.

thanks
jcpratap
# 4  
Old 06-06-2012
Link to FAQ about date arithmentic.

https://www.unix.com/answers-frequent...rithmetic.html

Ps. in you failed command, what was the expectation?
# 5  
Old 06-06-2012
Hi Methyl,

I wanted to convert date into a number so that two diffrent dates can be compared.
but in SunOS "v3" variable is converted to a value %s.

wheras in
Linux gwl09072appw017 2.6.18-128.el5 #1 SMP Wed Jan 21 08:45:05 EST 2009 x86_64 x86_64 x86_64 GNU/Linux

same syntax is giving v3 as 1338981734(a number).
# 6  
Old 06-06-2012
There is a perl solution in the FAQ.

Anybody with Solaris 5.8 cracked this problem before?
# 7  
Old 06-06-2012
Hi All,
I have made a script to compare dates of files with todays date
as below


Code:
datecalc()
{
if [ $1 = "Jan" ]
then
return 1;
elif  [ $1 = "Feb" ]
then
return 2;
elif  [ $1 = "Mar" ]
then
return 3;
elif  [ $1 = "Apr" ]
then
return 4;
elif [ $1 = "May" ]
then
return 5;
elif  [ $1 = "Jun" ]
then
return 6;
elif  [ $1 = "Jul" ]
then
return 7;
elif  [ $1 = "Aug" ]
then
return 8;
elif [ $1 = "Sep"
then
return 9;
elif  [ $1 = "Oct" ]
then
return 10;
elif  [ $1 = "Nov" ]
then
return 11;
elif  [ $1 = "Dec" ]
then
return 12;
fi
}
cd /CP2_logs/wls21/SAS/MDCR/Applicationlogs/WLS13
v1=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep  "gz$" |  egrep "MDCR.*" | wc -l`
v2=`date | awk '{print $2}'`
v3=`date | awk '{print $3}'`
datecalc $v2
v4=$?
i=1
while [ $i -le $v1 ]
do
A[$i]=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep  "gz$" |  egrep "MDCR.*" | awk '{print $1}' | head -$i | tail -1`
B[$i]=`ls -lrt | awk '{print $6,$7,$8,$9}'| egrep  "gz$" |  egrep "MDCR.*" | awk '{print $2}' | head -$i | tail -1`
C[$i]=`ls -lrt | awk '{print $9}'| egrep  "gz$" |  egrep "MDCR.*" | awk '{print $1}' | head -$i | tail -1`
echo ${A[$i]}
echo ${B[$i]}
echo ${C[$i]}
i=`expr $i + 1`
done

but while running it on solaris 5.8 with kornshell its giving error
related to arrays as

Code:
A[1]=May: not found
B[1]=5: not found
C[1]=MDCRClient_Refactored.log.1.06-05-12.gz: not found
bad substitution

while same script is working on linux with bash

please suggest with this

Last edited by Scrutinizer; 06-06-2012 at 11:16 AM.. Reason: code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

3. Shell Programming and Scripting

Convert date into days using shell script

Hi, I have date format in an excel sheet as 07/03/2014 11:21. I need to check this date with current date and need to find whether it is more than 30 days or not. I know how to current date into days, but someone please help me to change the date in the excel sheet to days, so that i can check... (2 Replies)
Discussion started by: Arasu123
2 Replies

4. Shell Programming and Scripting

Convert date arg to sql date

Doing a bcp load to sybase and need to convert datearg which comes in as 20130501 to 2013-05-01 which is the best way to do this (4 Replies)
Discussion started by: tasmac
4 Replies

5. Shell Programming and Scripting

How to convert string(variable) into date( epoch) in ksh on HPUX machine?

Hi all, I have used a bash script which ultimately converts a string into date using date --date option: DATE=$DATE" "$TIME" "`date +%Y` //concatenating 2 strings TMRW_DATE=`date --date="$DATE" +"%s"` //applying date command on string and getting the unixtime Please use code tags... (7 Replies)
Discussion started by: Rashu123
7 Replies

6. UNIX for Dummies Questions & Answers

Unable to add date to current date.

Hi, I am trying to display future date from the current date but unable to do so in UNIX (not in PERL). For eg: if today is March 5 then I want a variable wherein I can store Mar 7 date, but unable to get the future date from the current date. I have tried many possible ways as mentioned below... (11 Replies)
Discussion started by: amit.mathur08
11 Replies

7. 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

8. Shell Programming and Scripting

convert Julian date to calender date

Hi, I have script in unix which creates a julian date like 126 or 127 I want convert this julian date into calender date ex : input 127 output 07/may/2007 or 07/05/2007 or 07/05/07 rgds srikanth (6 Replies)
Discussion started by: srikanthus2002
6 Replies

9. Shell Programming and Scripting

Newbie convert date ksh

If I have start = 02282006; end = 03152006; How do I get startdate = 02/28/2006; enddate = 03/15/2006; Thanks, (3 Replies)
Discussion started by: britney
3 Replies
Login or Register to Ask a Question