1824976 Members
3848 Online
109678 Solutions
New Discussion юеВ

Fiscal Date Shell Script

 
SOLVED
Go to solution
Monte Heeren
Frequent Advisor

Fiscal Date Shell Script

Trying to get the fiscal date from the Unix date command, but need the fiscal date in the format : 2006-07.

Have the following script:
echo "This is a date test"
yearlong=$(date +%Y)
yearshort=$(date +%y)
echo "Year short --> " $yearshort
yearshort=$(($yearshort+1))
#if [ $yearshort -lt 10 ]
#then
# echo "less than 10 " $yearshort
# yearshort =$(string "0"+$yearshort)
# exit
#fi
echo "The fiscal date is $yearlong-$yearshort"
~


The fiscal date ends up as 2006-7.
How can I get the extra zero in the fiscal date? Trying to get the fiscal year to feed a
dbaccess query thru a script.
Monte.
6 REPLIES 6
Jeff_Traigle
Honored Contributor
Solution

Re: Fiscal Date Shell Script

Lots of ways to do this, no doubt, but here's a quick one off the top of my head:

yearshort=$(echo $(($yearlong+1)) | cut -c 3-)
--
Jeff Traigle
James R. Ferguson
Acclaimed Contributor

Re: Fiscal Date Shell Script

Hi Monte:

The easy way is to use formatted printing:

# printf "The fiscal date is %d-%02d\n" $yearlong $yearshort

Regards!

...JRF...
Yang Qin_1
Honored Contributor

Re: Fiscal Date Shell Script

echo "This is a date test"
yearlong=$(date +%Y)
yearshort=$(date +%y)
echo "Year short --> " $yearshort
yearshort=$(($yearshort+1))
if [ $yearshort -lt 10 ]
then
echo "The fiscal date is $yearlong-0$yearshort"
else
echo "The fiscal date is $yearlong-$yearshort"
fi
exit

Yang
Bill Hassell
Honored Contributor

Re: Fiscal Date Shell Script

Leading zeros are much simpler to implement using typeset:

typeset -Z2 yearshort
yearshort=$(date +%y)
echo $yearshort

typeset has a *lot* of formatting features for variables (UPPERCASE, lowercase, leading zeros, leading spaces, trailing spaces, etc)


Bill Hassell, sysadmin
john korterman
Honored Contributor

Re: Fiscal Date Shell Script

Hi Monte,

based on Mr. Hassel's suggestion:

$ eval typeset -Z2 X=$(($(date +%y ) + 1)); date +%Y-${X}

regards,
John K.
it would be nice if you always got a second chance
Monte Heeren
Frequent Advisor

Re: Fiscal Date Shell Script

Thank you to all that responded to this thread. The easyest way to solve this was to use Jeff Traigle's example.

Could not get the typeset example to work.

Please close this thread!

This is definitely a valueable resource center!

Monte.