Problem with adding date

Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Problem with adding date
# 1  
Old 09-25-2013
Problem with adding date

Hi All,
Please let me know the error in the below code.
m=(0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
M=$(date +%m)
Y=$(date +%Y)
if [ $M == 12 ] ; then 
  M=$M+1)printf "01-%s-%d\n" ${m[$M]} $Y

+ m=(0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
++ date +%m
+ M=09
++ date +%Y
+ Y=2013
+ '[' 09 == 12 ']'
/apps/powercdata/CS/TRAVEL_APPS/Scripts/payroll/ line 174: 09: value too great for base (error token is "09")
+ [[ 0 -ne 0 ]]


Last edited by Franklin52; 09-25-2013 at 09:36 AM.. Reason: fixed code tags
# 2  
Old 09-25-2013
Are you trying to get the date for next month?

If so, then depending on your OS and version, you might be able to use something like this:-
/home/RBATTE1>date +'01-%b-%Y' -d 'next month'

Does this neaten your code, or have I missed the point entirely?

If you cannot get this to run, but I have got the right output then we can work on your code.

I hope that this helps.

# 3  
Old 09-25-2013
Hi Robin,

Thanks for your reply. I found the issue with adding +1 to a variable.

if [ $M == 12 ] ; then
Y=`expr $Y + 1`
M=`expr $M + 1`
# 4  
Old 09-25-2013
Does that mean you have a solution?

By the way, what shell are you scripting in? I know it might seem trivial, but if you are doing lots of calculations, you could save a process by doing internal calculations rather than `expr ....

If you are ksh, for instance, you could:-

... or a few other variations.

# 5  
Old 09-25-2013
Leading zeroes make the shell (at least bash) assume octal values; overcome by prefixing with a base# indicator: 10#09 will be accepted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding days to date

Hi, please can somebody let me know the easiest way to add days to a date. I can do this in perl but would like to able to do it in a shell script. Desired output would be: date +'%Y-%m-%d' + 10 = 2016-05-02 Thank you (8 Replies)
Discussion started by: andy391791
8 Replies

2. Shell Programming and Scripting

Adding one day to a date

Hi All, I want to add one day to a date and store it in a variable. From GUI we are passing value (last day of the month)to $t_date. This $t_date will give me the value like this %Y%m%d 20150531. Now I want to add one day to this value and store it in a variable "datein". datein should... (8 Replies)
Discussion started by: ROCK_PLSQL
8 Replies

3. Shell Programming and Scripting

Adding/ Subtracting from Date

Hi , How can I add/substruct x number of days with date? For example My_Date=`date` Now I need Hope it's clear. (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

4. Shell Programming and Scripting

Adding date to file

I want this: 0.0230769,0.407692,0.307692,0,0.1,1.4,1,0,ADD DATE HERE, im getting this: 11/02/12 0.00192308,0.0269231,0.0192308,0,0.1,1.4,1,0, my script: #!/bin/ksh DIR=/export/home/yani_m/scripts/scrip_out_put/ DIR2=/export/home/yani_m/scripts/scrip_out_put/calc/ Date=$1... (1 Reply)
Discussion started by: LucyYani
1 Replies

5. UNIX for Dummies Questions & Answers

Adding hours and minutes to current date (Only to date not to time)

Hi, I want to add some hours and minutes to the current date. For example, if the current date is "July 16, 2012 15:20", i want to add 5 hours 30 minutes to "July 16, 2012 00:00" not to "July 16, 2012 15:20". Please help. Thanks! (4 Replies)
Discussion started by: manojgarg
4 Replies

6. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

7. UNIX for Dummies Questions & Answers

Adding date when using awk

Hi, I want to print the number of lines of a file along with filename and today's date. Ex: XXX|07-22-2010|8 I am using as wc -c -l file.txt | awk '{print "XXX|",date +"%m-%d-%Y","|",$1}' But this one prints as AAA| 0 | 8 Can anyone please help me on this for printing the date? ... (3 Replies)
Discussion started by: aeroticman
3 Replies

8. Shell Programming and Scripting

adding a date to the last of each file name

i have some files in my directory....wit names say aaa, bbb, ccc,,,,, i want to make loop so that for each file name,,,,,,a sysdate should be appended in the end.........and the files should look like aaa_20100331 i knw date can be appended as `date +"%Y%m%d"` but m nt able to make a loop. ... (2 Replies)
Discussion started by: amitpta
2 Replies

9. Shell Programming and Scripting

problem with displaying date and adding time

Hi, I have a log file with contents like - - - - - - - - I need an output like this 14/Sep/2008 Time duration: 00:06:00 15/Sep/2008 Time duration: 00:00:01 Please anyone suggest a script for this.... (1 Reply)
Discussion started by: FuncMx
1 Replies

10. UNIX for Dummies Questions & Answers

Adding a date as a first column

I want to add a date to a record which is appended to a file that gets its data from an external source. An explanation: 1. Getting external data: curl 2. Getting the required record: | grep mylocation 3. Appending to file: >> mytemperatures.txt ... (2 Replies)
Discussion started by: figaro
2 Replies
Login or Register to Ask a Question