The following snippet should output the full month name and the month number:
#!/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?
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:
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
}
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:
#!/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:
$ sh month.sh 3
Name of the month: March
$ sh month.sh 10
Name of the month: October
@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:
# ./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.
# 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
@ygor: thanks also for your response, so it seems that the latest version of my script would be the way to go, but it remains to feel like a gap in the breadth of the date function.
Figaro, the only - but important - difference is that i put the logic into a function and (telling from the code you posted) did not. The reason why it will be better to encapsulate it into a function is - encapsulation! You will not have to copy and paste the logic if you need it in another script. You will also be able to extend the functionality later without modifying existing scripts.
Suppose you would want to add different language support: You could introduce a variable and display the months name based on this, for instance.
It is often the simplest solution which is also the most elegant and pleasing one. And, btw., the fastest too, because the case-esac construct will execute really fast whereas the call of an external program is costly in terms of CPU cycles.
@bakunin
Thanks for your response, the script is undergoing many changes and it is the detail that needs to be right and ultimately there will be a function.
I too tried different formats, and not got the error at any case.
This is the explanation of the command, for debugging by yourself.
echo "Name of the month: " `date "+%B" --date "1970-01-01 UTC $mon months"`
date -- date command.
"+%B" - print only the month
--date - argument specifies the next column is th input date
1970-.* - date to be formed from it.
$mon - should have the month number [ i believe this you might be missing ]
months - this is the word which specifies you are specifying month input [ this should be as it is ].
@thegeek
I have tested this snippet again and copying precisely your line as well as prepending with /bin/sh and mon=$1-1. The output is still what was posted here: Formatting a month's name Post: 302313201