Shell Scripting Help

Ghodmode ghodmode at ghodmode.com
Mon May 14 22:26:30 UTC 2007


Hi Chris,
    I believe you've already seen two solutions, but I'd still like to offer
my preferred alternative...

#!/bin/bash
## ^ ksh isn't available in Ubuntu by default :)

YEAR=`date +%Y`
MONTH=`date +%m`
DAY=$(( `date +%d` )) - 1

# Let the shell do the basic math instead of calling an external program

if [ $DAY -eq 0 ]; then
    MONTH=$(( MONTH - 1 ))
    [ $MONTH -eq 0 ] && $MONTH = 12
    DAY=`cal $MONTH | tr -d "\b" | fmt -1 | tail -1`
fi

# This shortcut to an if/then statement is just as effective, but nicer to
read
# It's the same as:
# if [ $DAY -lt 10 ]
# then
#     DAY="0$DAY"
# fi
[ $DAY -lt 10 ]   && DAY="0$DAY"
#[ $MONTH -lt 10 ] && MONTH="0$MONTH"

echo "Yesterday was $MONTH $DAY $YEAR



On 5/15/07, Nils Kassube <kassube at gmx.net> wrote:
>
> chrisl at xp.etowns.net wrote:
> > I have the following script:
> >
> ...
>


>
> > The value DAY6 would be 13 if run today. If it was run on the 10th it
> > would be 9.
> >
> > I need to have DAY6 displayed as 09 and not just 9, is this possible?



> How about this one?


echo "1 day ago was: $(date -d yesterday '+%m %d %Y')"


Thanks, Nils.. I didn't know date had that argument :)


-- Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20070515/b9a3a9e0/attachment.html>


More information about the ubuntu-users mailing list