Shell scripting question

Frank Mueller - emendis GmbH Frank.Mueller at emendis.de
Mon Sep 12 11:37:15 PDT 2005


To get the date in the right format you could simply use

date +%H

Greetz,

Ice

Paul Schmehl schrieb:
> 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/
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to 
> "freebsd-questions-unsubscribe at freebsd.org"

-- 
Frank Mueller
eMail: Frank.Mueller at emendis.de
Mobil: +49.177.6858655
Fax: +49.951.3039342

emendis GmbH
Hofmannstr. 89, 91052 Erlangen, Germany
Fon: +49.9131.817361
Fax: +49.9131.817386

Geschaeftsfuehrer: Gunter Kroeber, Volker Wiesinger
Sitz Erlangen, Amtsgericht Fuerth HRB 10116


More information about the freebsd-questions mailing list