LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   need to manipulate dates (http://www.linuxquestions.org/questions/programming-9/need-to-manipulate-dates-82291/)

clsonnt 08-15-2003 12:00 PM

need to manipulate dates
 
I'm trying to run a script in Bourne shell (solaris8) that executes on the first day of the month and passes the numeric representation of the previous month.

Getting the current month numeric is not a problem.
MONTH=`date +%m``

I just can't seem to remember how to add/subtract variables....
:confused:

kev82 08-15-2003 12:22 PM

echo $(( 5 + 3 ))

clsonnt 08-15-2003 12:33 PM

lost something in the translation....

# month=`date +%m`
# echo $month
08
# echo $((month -1))
syntax error: `(' unexpected

kev82 08-15-2003 01:16 PM

sorry, my fault, just checked the sh on solaris and it doesnt work, strange though, it works on my sh. anyway, the old way is to use expr.

$ month=`date +%m`
$ echo $month
08
$ echo `expr $month - 1`
7

clsonnt 08-19-2003 10:00 AM

THANKS...Works like a champ.
If you are interested....here's the code

#!/bin/sh
#
# Script to run Wusage 7.0 stats
# This script assumes that the conf files are in
# the same directory as the wusage executable
# Usage: run_stats.sh conf_File_name

# Define values
WUSAGE_DIR="/data/projects/doedir/serversII/wusage7.0"
CONF_FILE="$WUSAGE_DIR/1"
CURRENT_MONTH=`date +%m`
CURRENT_YEAR=`date +%y`
MONTH=`expr $CURRENT_MONTH - 1`
BEGIN_DATE=`echo "$MONTH/1/$CURRENT_YEAR"`
END_DAY=`cal $MONTH $CURRENT_YEAR | awk '/./{day=$NF};END {print day}'`
END_DATE=`echo "$MONTH/$END_DAY/$CURRENT_YEAR"`

# Run stats
cd $WUSAGE_DIR
./wusage -c $CONF_FILE -b $BEGIN_DATE -e $END_DATE

exit 0

clsonnt 08-19-2003 10:07 AM

Ooopps ...fond an error. Here's the revised code.

#!/bin/sh
#
# Script to run Wusage 7.0 stats
# This script assumes that the conf files are in
# the same directory as the wusage executable
# Usage: run_stats.sh conf_File_name

# Define values
WUSAGE_DIR="/data/projects/doedir/serversII/wusage7.0"
CONF_FILE="$WUSAGE_DIR/$1"
CURRENT_MONTH=`date +%m`
CURRENT_YEAR=`date +%y`
MONTH=`expr $CURRENT_MONTH - 1`
BEGIN_DATE=`echo "$MONTH/1/$CURRENT_YEAR"`
END_DAY=`cal $MONTH $CURRENT_YEAR | awk '/./{day=$NF};END {print day}'`
END_DATE=`echo "$MONTH/$END_DAY/$CURRENT_YEAR"`

# Run stats
cd $WUSAGE_DIR
./wusage -c $CONF_FILE -b $BEGIN_DATE -e $END_DATE

exit 0


All times are GMT -5. The time now is 11:37 PM.