Number of days in the previous month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of days in the previous month
# 8  
Old 08-09-2010
Code:
cal |  awk 'NF{p=$(NF)}END{print p}'

# 9  
Old 08-09-2010
Code:
set - $(cal $(( ($(date +%-m) + 11-1) %12+1)) $(date +%Y))
eval echo \${$#}


Last edited by daPeach; 08-10-2010 at 05:18 AM..
# 10  
Old 08-10-2010
Quote:
Originally Posted by lyoncc
- HPUX POSIX Shell -


This month, the command below is returning an error...

# X=$(cal $(( ( $(date +%m) + 11 - 1 ) %12 + 1 )) $(date +%Y) | grep -v '[A-Za-z]' | wc -w)

sh: 08 + 11 - 1 ) %12 + 1 : The specified number is not valid for this command.

It seemed to work last month....any ideas?

TIA!!!

Some programs interpret a number with a leading zero as octal. So it worked for the previous months, because octal 1..7 represent the same value as decimal 1..7 . But 08 does not exist in octal.

In short: you need to get rid of the leading 0 that the date command produces, then I think t will work as intended.

S.
# 11  
Old 08-10-2010
Quote:
X=$(cal $(( ( $(date +%m) + 11 - 1 ) %12 + 1 )) $(date +%Y) | grep -v '[A-Za-z]' | wc -w)
This does not give a syntax error in HP-UX Posix Shell.
It gives the answer 31 which is correct for the days in July 2010.

It is interesting how it works in a January because it correctly assumes that the days in December are the same this year and last year.
# 12  
Old 08-10-2010
Quote:
Originally Posted by lyoncc
that works on my gentoo system, but it doesn't return anything on the hp

Code:
 X="`cal -3 | sed -n '/29/p' | sed 's/  *$//;s/.*\([0-9][0-9]\)     .*/\1/'`"
# echo $X
 
#

hmm i cant test on hp-ux because i dont have it Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of days in current month

I have a homework assignment: ---------------------------------------- "Display" the number of days in the current month. For example: September 1996 has 30 days ---------------------------------------- I am trying to just display the head of cal to start the sentence. eg. cal | head ... (1 Reply)
Discussion started by: eaafuddy
1 Replies

2. UNIX for Dummies Questions & Answers

Finding days in previous month

#!/bin/ksh day=1 month=1 year=2012 if then then prevmonth=31 elif then prevmonth=30 elif then then prevmonth=29 elif then prevmonth=29 else prevmonth=28 fi (4 Replies)
Discussion started by: vagar11
4 Replies

3. Shell Programming and Scripting

How to Find number of days in a month in mmddyyyy format?

Hi Guru's, I am working on a shell script from past a month and unable to get rid of automating while working with dates,here's what i have. inital_date=11012011 final_date=11302011 expected_output= has to be in below format PFB 11012011 11022011 11032011 * * * 11102011 * *... (9 Replies)
Discussion started by: Gaurav198
9 Replies

4. Shell Programming and Scripting

display number of days in current month

hi all searched google and here, cant find and am begining to suspect there is no options for this. shell = born with either the date or cal command I need to display the number of days in current month. can anyone point me in the right direction? (10 Replies)
Discussion started by: rontopia
10 Replies

5. Programming

Number of days in month from certain parameters, c programming request.

Hi, I have an issue in date processing, the issue is I have a month as an int ( 1 - 12 ), the weekday as int ( 0 - 6 , 0 = Sunday), and the week day in month as int ( 0 - 5, 5 = last ex: first sunday, last monday, third tuesday ... ), now from those three parameters is there a possible way to... (2 Replies)
Discussion started by: modn3
2 Replies

6. Shell Programming and Scripting

Number of days in month from certain parameters

Hi, I have an issue in date processing, the issue is I have a month as an int ( 1 - 12 ), the weekday as int ( 0 - 6 , 0 = Sunday), and the week day in month as int ( 0 - 5, 5 = last ex: first sunday, last monday, third tuesday ... ), now from those three parameters is there a possible way to... (5 Replies)
Discussion started by: modn3
5 Replies

7. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

8. Shell Programming and Scripting

Assigning number of days in the month to a variable

I am writing a script that requires the number of days in any given month. In the shell, I can use the command: cal `date +%m` `date +%Y`| grep -v '' | wc -w to give me the number of days in the month, but when I assign it to a variable: VAR=`cal `date +%m` `date +%Y`| grep -v '' | wc... (3 Replies)
Discussion started by: skaptakalian
3 Replies

9. Shell Programming and Scripting

calculate the number of days left in a month

does any one have any ideas how i would go about calculating the number of days left in the month from a bash script ?. I want to do some operations on a csv file according to the result (8 Replies)
Discussion started by: dunryc
8 Replies

10. Shell Programming and Scripting

Calc number of days in a month

Looking for some help on capturing the number of days in a month to set as a loop counter. Any ideas, please let me know. (3 Replies)
Discussion started by: flounder
3 Replies
Login or Register to Ask a Question