Shell scripting question

Paul Schmehl pauls at utdallas.edu
Mon Sep 12 11:26:19 PDT 2005


I've written a script to check apache to make sure it's running *and* 
logging.  One of the variables I create is named DATEHOUR, and it's created 
by parsing the output of date in such a way that all I get is the hour 
(using awk and cut.)  I'm comparing DATEHOUR to LOGHOUR, which represents 
the the most recent hour that the log was written to

I've run in to a small problem I'm not sure how to solve.  When the hour is 
less than 10, the script generates an arithmetic expression error.

Here's part of the script so you can visualize what I'm trying to do:

PROG=/usr/local/sbin/apachectl
LOG=/var/log/httpd-access.log
PID=`/bin/ps -auxw | grep http | grep root | grep -v grep | awk '{print 
$2}'`
DATE=`date | awk '{print $4}' | cut -d':' -f1,2`
LOGDATE=`ls -lsa ${LOG} | awk '{print $9}'`
DATEHOUR=`echo ${DATE} | cut -d':' -f1`
LOGHOUR=`echo ${LOGDATE} | cut -d':' -f1`
DATEMIN=`echo ${DATE} | cut -d':' -f2`
LOGMIN=`echo ${LOGDATE} | cut -d':' -f2`
LOGGING=1

if [ $((DATEMIN)) -gt $((LOGMIN+15)) ]; then
  LOGGING=0
elif [ $((DATEHOUR)) -ne $((LOGHOUR)) ] && [ $((DATEMIN+60)) -gt 
$((LOGMIN+15)) ]; then
  LOGGING=0
fi

When DATEHOUR is less than 10 (01-09), the script generate an arithmetic 
expression, variable substition error.  I'm pretty certain it's because of 
the leading zero, so I'm trying to figure out how to strip that out.  I 
thought that parameter expansion would do it, but I get some odd (to me) 
results.

Assume DATE is 09.

echo ${DATE:0,0}
09
echo ${DATE:0,1}
9
echo ${DATE:1,1}
9

I would have thought that 0,0 would return only the first character and 1,1 
would return only the second, but that is obviously not the case.

How can I strip the leading character from the string so that I can test to 
see if it's zero?

Paul Schmehl (pauls at utdallas.edu)
Adjunct Information Security Officer
University of Texas at Dallas
AVIEN Founding Member
http://www.utdallas.edu/ir/security/


More information about the freebsd-questions mailing list