Formatting a month's name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Formatting a month's name
# 1  
Old 05-02-2009
Formatting a month's name

The following snippet should output the full month name and the month number:
Code:
#!/bin/sh
echo "Name of the month: " `date +%B` $1

The result is something like:
Name of the month: May 03
whereas I want the argument $1 to be processed and returned, so that it will result in something like:
Name of the month: March 03
How do I provide $1 as input to the date function?
# 2  
Old 05-02-2009
This will give you the expected result

Code:
let mon=$1-1
date "+%B" --date "1970-01-01 UTC $mon months"

# 3  
Old 05-03-2009
Actually I just need the name of the month that was provided as the argument, so 01 will yield January, 02 will yield February and so on.
# 4  
Old 05-04-2009
You should not use the "date" command for that, because most versions of the "date"-command will not accept any input at all. The GNU-date used in Linuxes is a rare exception to this rule.

But as there are not to many months you could use a simple "case". Package it into a function for maximum encapsulation:

Code:
function month_name
{
typeset name=""

case "$1" in
     "01") name="January" ;;
     "02") name="February" ;;
     "03") name="March" ;;
     "04") name="April" ;;

     ....
esac

print - "$name"

return 0
}

I hope this helps.

bakunin
# 5  
Old 05-04-2009
i hope bakunin - solution will help you.

If you want the solution should be implemented with the date command, then the solution which i posted might help you. If you don't understand that, here am rewriting it exactly as:

Code:
#!/bin/sh
let mon=$1-1
echo "Name of the month: " `date "+%B" --date "1970-01-01 UTC $mon months"`

I believe It fully satisfies your requirement as:
Code:
$ sh month.sh 3
Name of the month:  March
$ sh month.sh 10
Name of the month:  October

# 6  
Old 05-05-2009
@bakunin: I was getting concerned that a case statement would be necessary

@thegeek: For the record, this is the error message that I am getting:
Code:
# ./month.sh 10
9
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]
Name of the month: 
# ./month.sh 03
2
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]
Name of the month:

I have tried multiple variations to the script, but none that worked so far. Please advise.

Code:
# uname -a
FreeBSD 7.1-RELEASE FreeBSD 7.1-RELEASE #0: Thu Jan  1 14:37:25 UTC 2009     root@logan.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386

# 7  
Old 05-05-2009
This is what I have at the moment. Not a pretty sight and it does not feel like the UNIX approach:
Code:
#!/bin/sh

case "$1" in
	"01") name="January" ;;
	"02") name="February" ;;
	"03") name="March" ;;
	"04") name="April" ;;
	"05") name="May" ;;
	"06") name="June" ;;
	"07") name="July" ;;
	"08") name="August" ;;
	"09") name="September" ;;
	"10") name="October" ;;
	"11") name="November" ;;
	"12") name="December" ;;
	* ) name="Not a valid month number." ;;
esac
echo "$name"
	
return 0

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

2. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

3. Shell Programming and Scripting

Convert From Month Number to Month Name

Hi, I have a script that accepts an input date from the user in yyyy-mm-dd format. I need to get the mm-dd part and convert it to month name. example: 2011-11-15 I want that to become "Nov 15" I don't have the GNU date, I am using an AIX os. Thanks. (1 Reply)
Discussion started by: erin00
1 Replies

4. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

5. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

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

7. Shell Programming and Scripting

Month name

Hi, Could you pls help me out in finding the name of next month.? For eg: date +"%b" gives the current month ie 'Jul' I want to get the output as 'Aug' Thanks, Jerry (4 Replies)
Discussion started by: jerrynimrod
4 Replies

8. Shell Programming and Scripting

How to match month

Dear all, i need script to ask to enter the month and if the enterd month is not valid prompt " Envalid entry try again " like this. eg - Enter the month : Jun script is proceeding if we enter incorrect month, Enter the month - Junn Invalid... (1 Reply)
Discussion started by: Nayanajith
1 Replies

9. UNIX for Dummies Questions & Answers

Formatting Date (adding a month)

i need to write a script that outputs a file with the following format used for its name BRHYYYYMM.TXT. I have no problems creating this file with the current year and month in the title but i would like the month to be the next period i.e. instead of BRH200601.TXT id like BRH200602.TXT. Im unable... (4 Replies)
Discussion started by: devid
4 Replies

10. UNIX for Dummies Questions & Answers

Days of the month

Hello, Please can someone help me in getitng last 7 days from the current date? for eg: input /default : today (10/18/2004) required output: 10/17/2004 10/16/2004 10/15/2004 10/14/2004 10/13/2004 ... (6 Replies)
Discussion started by: Anamika
6 Replies
Login or Register to Ask a Question