how to get the last word from file

Ralph W. Reid rreid at sunset.net
Wed Mar 30 15:16:53 EST 2005


On Tue, Mar 29, 2005 at 12:17:43PM -0700, Ned wrote:
> Hi list,
> I am using the cal command to get current month. What I need for my shell script is how many days are there in the current month, that is, the last listed date for that month is that very number.
> How can I get that number?
> cal | tail -1
> and then what?
> 
> Many thanks!
> Ned

The above command line will produce a blank line because the last line
produced by `cal` is a blank line.  If you are looking specifically
for days in the current month (as opposed to a more general
last-word-in file search), the following is one of several ways to
produce it:

if [ `cal | grep -c 31` == 1 ];
then
echo 31
elif [ `cal | grep -c 30` == 1 ];
then
echo 30
elif [ `cal | grep -c 29` == 1 ];
then
echo 29
elif [ `cal | grep -c 28` == 1 ];
then
echo 28
else
echo Cal did not produce a calendar.
fi

HTH, and have a _great_ day!

-- 
Ralph.  N6BNO.  Wisdom comes from central processing, not from I/O.
rreid at sunset.net  http://personalweb.sunset.net/~rreid
...passing through The City of Internet at the speed of light!
_PI = 4 * ARCTAN (1)




More information about the Speakup mailing list