Help with incrementing the date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with incrementing the date
# 1  
Old 05-07-2012
Help with incrementing the date

I have a date variable like 2012-12-31 ( YYYY -MM -DD ) in flat file and it has to be incremtented by 1 every time i run the script

Example : i tried the below script after data modifcation but this does not seem to work

Code:
 
expr `20121231 +%Y%m%d` + 1


Note : Mine is not a GNU compliant OS
# 2  
Old 05-09-2012
Dates are a pain. I called the file inc.pl, so this is output. use the inc_date function
Code:
$ ./inc.pl 1999-12-31
2000-01-01
jim@jim-HP ~
$ ./inc.pl 2011-12-31
2012-01-01
jim@jim-HP ~
$ ./inc.pl 2010-02-28
2010-03-01
jim@jim-HP ~
$ ./inc.pl 2012-02-28
2012-02-29

example code:
Code:
#!/bin/bash
# inc.pl - from a perl script....

inc_date()   # add 86400 seconds to time in seconds from date to get the next day
{
  declare -a arr=( $(echo "$1" | tr -s '-' ' ') )
 
  perl -e  '     
         use Time::Local;            
         $time = timelocal(0,0,0, $ARGV[0], $ARGV[1] -1, $ARGV[2] );
         $time+=86400;  # one day = 86400 seconds
         ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($time);
         $year+=1900;
         $mon+=1;         
         printf "%d-%02d-%02d\n", $year, $mon, $mday;
         ' ${arr[2]} ${arr[1]} ${arr[0]}
}

inc_date "$1"

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-10-2012
Code:
perl -e 'printf "%s\n", scalar localtime(time() + 86400)'

# 4  
Old 05-12-2012
Hi Jim

I tried using your code but it seems to be giving some syntax error. Since i am novice with the usage of arrays in UNix could you please help me to resolve the same .

Code:
 
data/test>inc.pl 2012-12-12

P.S : I have uploaded the script that i have beeen using. But it results in the below error :

Code:
 
# inc.pl - from a perl script....
inc_date()   # add 86400 seconds to time in seconds from date to get the next day
{
  declare -a arr=( ./inc.pl[2]: syntax error at line 8 : `(' unexpected

# 5  
Old 05-12-2012
Quote:
Originally Posted by royalibrahim
Code:
perl -e 'printf "%s\n", scalar localtime(time() + 86400)'


Hi Ibrahim

thanks for the help, I am currenlty looking to increment the user defined dates and not the current date

---------- Post updated at 02:03 AM ---------- Previous update was at 02:02 AM ----------

Hi Jim

This code you have provided seem to have a syntax erro could you please help to correct the same
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Incrementing the date depending on the Counter (parameter) passed to the script

Hello Everyone, I have been trying to complete a shell script where, I need to increment the date depending on the file (depending on the date) availability on the remote server. i.e. Basically, I will be passing a counter (like parameter 1 or 2 or 3 or 4). First I will check for the... (1 Reply)
Discussion started by: filter
1 Replies

2. UNIX for Dummies Questions & Answers

Incrementing variable in for

Hi, want to increment a variable in a for loop like this: for (( c=$total-1; c>=0; c-- )) do if ; then maximo=$valores fi done But it gives the error: No such file or directory How can i do this only incrementing the c variable? Thanks (8 Replies)
Discussion started by: limadario
8 Replies

3. Shell Programming and Scripting

Incrementing with a twist - please help

I'm currently trying to write a ksh or csh script that would change the name of a file found in directories and attach to the name an incrementing three digit number. I know how to write a script that will go: 000, 001, 002, 003, etc The twist is I need more increments then allowed by a 3... (11 Replies)
Discussion started by: Rust
11 Replies

4. Programming

incrementing variables in C++

Hello, what is the result of the below, and how does it work? int i = 5; cout << i++ * ++i << endl; cout << i << endl; (12 Replies)
Discussion started by: milhan
12 Replies

5. Shell Programming and Scripting

Incrementing in while loop

echo "Enter Starting id:" echo "" read rvst_strt_idxx echo "" echo "Enter Closing id:" echo "" read rvst_clsn_idxx FIELD1=$rvst_strt_idxx FIELD2="USER" FIELD3="TEST" FIELD4="12345" FIELD5="00000" echo "" echo "INSERT INTO TABLE( FIELD1, FIELD2, FIELD3, FIELD4, ... (7 Replies)
Discussion started by: ultimatix
7 Replies

6. Linux

Incrementing the date stored in the variable

Hi all, I have a variable with date as 20080831 . Now I want to increment it as 20080901 and so on.Is there any command for this. Please help me. thanks rameez (1 Reply)
Discussion started by: rameezrajas
1 Replies

7. Shell Programming and Scripting

Need help with incrementing date in while loop.

I need to execute a KornShell (SunOS 5.9) script against a range of dates: endDate=20080804 extractDate=20080401 while ; do batch < scripts/myshellscript.sh $extractDate ## add 1 day to extractDate ## done My question is how do I increment the extractDate variable and still have it... (3 Replies)
Discussion started by: Robert W.Mills
3 Replies

8. Post Here to Contact Site Administrators and Moderators

No. post not incrementing

Hi Admin, i just noticed that when I do postings, the number does not increment. eg : Post A -Total Posts 312 Post B - Total Posts 312 Post C - Total Posts 313 Post D - Total Posts 313 Why is this so? Can you kindly check this out? Thank you. (5 Replies)
Discussion started by: incredible
5 Replies

9. Shell Programming and Scripting

Port incrementing using one file

Hi I wrote this script with the help of you guyz.My next challenge is to increment the port using single file.So far iam using this code to increment hp1 and hp2 .To increment port numbers, iam using two different files (.default_port_hp1 and .default_port_hp2).The challenge for me is to use... (0 Replies)
Discussion started by: coolkid
0 Replies

10. Shell Programming and Scripting

incrementing a for loop

I have, LIST="a b c d e" for word in $LIST do echo $word done would give me a b c d e With the first iteration of the for loop, I get "a" as the result. Is it possible that I get both "a" and "b" in only the first iteration. In the next iteration I get "c" and "d" and so on.... (2 Replies)
Discussion started by: run_time_error
2 Replies
Login or Register to Ask a Question