[NTLUG:Discuss] Using current date in a script

Rick Matthews RedHat.Linux at verizon.net
Fri Feb 7 05:16:33 CST 2003


Minh Duong wrote:
> 
> Does anybody know how to use the current date when
> writing a shell script?  What I am trying to do is
> capture some information and save it as a file with
> the filename as the date.  For example:
> 
> cat file >> 20030205.txt
> 
> The problem is I don't know how I can pass the date in
> that form as text without resorting to using something
> like java.


Start your script with a section defining your variables:

# ------------------------------------------
# Set date format for naming files

DATE=`date +%Y-%m-%d`
YEAR=`date +%Y`
DATETIME=`date +"%a %d %h %Y %T %Z"`
UNIQUEDT=`date +"%Y-%m-%d_%H%M%S"`
BLACKDIR=/usr/local/squidGuard/db/block
BLKDIRADLT=${BLACKDIR}/blacklists
PORN=${BLACKDIR}/blacklists/porn
ADULT=${BLACKDIR}/blacklists/adult


Then you can use them later in the script (sections snipped from a 
script):

# --------------------------------------------
# Create statistics file for porn directory
#

touch ${PORN}/stats/${UNIQUEDT}_stats
echo "Blacklist Line Counts for "${DATETIME} \
	>> ${PORN}/stats/${UNIQUEDT}_stats

# --------------------------------------------------------------
# Download the latest blacklist from the squidguard site
#
# Uses wget (http://wget.sunsite.dk/)
#
# Downloads the current blacklist tar.gz file into the 
# ${BLACKDIR} directory (defined above) and will name the file 
# uniquely with today's date: ${UNIQUEDT}_sg.tar.gz
#

wget --output-document=${BLACKDIR}/${UNIQUEDT}_sg.tar.gz \
	http://ftp.ost.eltele.no/pub/www/proxy/squidGuard/contrib/blacklists.tar.gz

# --------------------------------------------------------------
# Install the new squidguard blacklist
#

gunzip < ${BLACKDIR}/${UNIQUEDT}_sg.tar.gz | (cd ${BLACKDIR}; tar xvf -)
------------------

You get the idea.

Rick







More information about the Discuss mailing list