date addition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date addition
# 1  
Old 10-13-2008
date addition

i have a script called date_add.sh written in k_shell

My shell script requirement is that it accepts a date from the user in the format YYYY-MM-DD and then it shows all the 15 days later day availaible in the current year

if date accepted from user is 2008-10-13,then the o/p sholud be
2008-10-28
2008-11-12
2008-11-27
2008-12-12
2008-12-27
-----------------------------------------------------------------------

How to put this logic in a loop? Also how to convert Epoch days into format YYYY-MM-DD

Current_day=`perl -e 'print int(time/86400);'`--------------------16145

but the below command is not working

perl -e '@d=localtime ((stat(shift))[9]); printf "%4d-%02d-%02d\n", $d[5]+1900,$d[4]+1,$d[3]' Current_day

Last edited by ali560045; 10-13-2008 at 07:44 AM..
# 2  
Old 10-13-2008
help in date script

below my script . Actually in my script i m taking the current date and then adding 15 days to it.
--------------------------------------------------------------------------

#!/bin/ksh

Current_day=`perl -e 'print int(time);'`
echo $Current_day

count=0
count=`expr $Current_day + 1296000`
echo $count

perl -le 'print scalar localtime('"$count"');'

perl -e '@d=localtime ((stat(shift))[9]); printf "%4d-%02d-%02d\n", $d[5]+1900,$d[4]+1,$d[3]' '"$count"'
--------------------------------------------------------------------------

the last command that converts the $count value into format YYYY-MM-DD is not working . plz help me in this?
# 3  
Old 10-13-2008
stat works on files, not epoch seconds.
# 4  
Old 10-13-2008
ok. so how to convert the value of the below command into the formay YYYY-MM-DD so i should get the o/p as 2008-10-28

perl -le 'print scalar localtime('"$count"');'
# 5  
Old 10-13-2008
Code:
#!/bin/ksh
tictock()
{ 
perl -e '
 $now = time;
 $increment = 1296000;
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now);
 $year += 1900;
 
 for($i=0, $thisyear = $year ; $thisyear==$year ; $i++)
 {
    printf ("%4d-%02d-%02d ", $year, $mon, $mday);
 	$incr=($increment * $i) + $now;
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($incr);
    $year += 1900;
    $mon += 1;
  
 }
 '
}

set -A arr $(tictock )
let i=0
while [[ $i -lt ${#arr[*]} ]]
do 
    print ${arr[i]}
    i=$(( i + 1))
done

# 6  
Old 10-13-2008
Quote:
Originally Posted by ali560045
i have a script called date_add.sh written in k_shell

My shell script requirement is that it accepts a date from the user in the format YYYY-MM-DD and then it shows all the 15 days later day availaible in the current year

if date accepted from user is 2008-10-13,then the o/p sholud be
2008-10-28
2008-11-12
2008-11-27
2008-12-12
2008-12-27
-----------------------------------------------------------------------


Code:
. date-funcs
year=$( date +%Y )

_DATESHIFT=$1
while :
do
  _dateshift $_DATESHIFT 15
  [ ${_DATESHIFT%%-*} -ne $year ] && break
  printf "%s\n" "$_DATESHIFT"
done

The date-funcs library of shell functions is available at The Dating Game.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Addition of new line

Hi I have a file whose contents are as follows: sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424; sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1; sorce1 LEN ... (8 Replies)
Discussion started by: sa@@
8 Replies

2. Shell Programming and Scripting

Addition to Julian date

Need assistance . Below code gets me julian date . I wanted to add hour/24 to julian date and output it. Is there a way to do the calculation? use Time::Local; use POSIX qw(strftime); my $time=timelocal(1,2,3,9,11,2013); printf strftime "%j", localtime($time); 343 (3 Replies)
Discussion started by: ajayram_arya
3 Replies

3. UNIX for Dummies Questions & Answers

Limitation in addition

whats wrong with this addition? Whats the maximum number of digits can be handled? pandeeswaran@ubuntu:~/Downloads$ const=201234454654768979799999 pandeeswaran@ubuntu:~/Downloads$ let new+=const pandeeswaran@ubuntu:~/Downloads$ echo $new -2152890657037557890 pandeeswaran@ubuntu:~/Downloads$ (4 Replies)
Discussion started by: pandeesh
4 Replies

4. Shell Programming and Scripting

addition of two numbers

by the script, two files Q1 and Q2 will be generated on the system. Q1 will contain an integer number and Q2 also contain an integer number. i would like to add those numbers and put into new file. excerpt from my script 22 subcount=`echo $dir/Q$qid.txt` + `echo $dir/Q$qid.txt` 23 echo... (1 Reply)
Discussion started by: lookinginfo
1 Replies

5. Shell Programming and Scripting

Awk addition

How would I print out the total amount through awk? I tried using print "Total Amount: " $4+$4; Would I have to do a for loop to get through everything? (2 Replies)
Discussion started by: Boltftw
2 Replies

6. Shell Programming and Scripting

Substitution/Addition

Hi I need to do the following substitution I have to look for line starting with ABC and add 4 ":" before the first occurence of "+"in that line Input ABC:12:Lambert:C278472:1357:0:0:0:0:2:N::::N:9045123:NAP::+DEF output ABC:12:Lambert:C278472:1357.00:0.00:0:0:0:2:N::::N:9045123:NAP::::::+DEF... (5 Replies)
Discussion started by: mad_man12
5 Replies

7. Shell Programming and Scripting

Addition Of New Fields

Hi, I want to add 3 new fields in the existing file.Please find the example below. input: UID: ABCD UNAME: XYZ Desired Output Tmiestamp: 20101208 UID: ABCD UNAME: XYZ DEPTNO:40 ModifyTImestamp:20101209 If you see the above i have added the 3 columns manually in the output section... (2 Replies)
Discussion started by: Nani7574
2 Replies

8. Shell Programming and Scripting

Addition

Hi all, I am very new to shell programming and trying to learn out the basics. I tried this: $ echo `expr 20 + 30` and it worked. But when i tried this,it does not work. $ a=20 $ b=30 $ echo `expr a + b` The error is: expr: non-numeric argument I cant understand why its... (3 Replies)
Discussion started by: gautamshaw
3 Replies

9. Shell Programming and Scripting

Help with addition

Hi all, I am getting following output by using commands like sort, uniq and awk to the standard output. 110 d 40 a 59 c 9 b 3 e Now at the end I would like to add all the numbers in column 1 and display the count of all numbers i.e. (110 + 40 + 59 + 9 + 3). Also the output may... (3 Replies)
Discussion started by: tenderfoot
3 Replies

10. Shell Programming and Scripting

addition

Hi all, I am new to perl. I need help adding bunch of numbers. I have a file look like this: 1 1 2 1 2 3 1 2 3 4 1 (2 Replies)
Discussion started by: email-lalit
2 Replies
Login or Register to Ask a Question