Operating System - HP-UX
1832143 Members
3074 Online
110038 Solutions
New Discussion

Re: inputting date into a file

 
JOHN TURNER_2
Frequent Advisor

inputting date into a file

Hi

i need to copy a file to another file, and also put the month end onto the end of the file. This needs to be scheduled automatically to run through cron each month

Any help would be appreicated on this issue
GUI's are for wimps!
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: inputting date into a file

You need to create a script that is called by cron. You script should look something like this:

-------------------------------------
#!/usr/bin/sh

export PATH=${PATH}:/usr/bin

typeset PROG=${0##*/}
typeset INFILE=/aaa/bbb/file1
typeset OUTFILE=/ccc/ddd/file1
typeset -i STAT=0

cp -p ${INFILE} ${OUTFILE}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
date >> ${OUTFILE}
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "${PROG}: date command failed; status ${STAT}." >&2
fi
else
echo "${PROG}: cp failed; status ${STAT}." >&2
fi
exit ${STAT}
If it ain't broke, I can fix that.
OldSchool
Honored Contributor

Re: inputting date into a file

and....

as for scheduling, the easiest way would be to stick it in cron at 00:01 on the first day of the new month. Otherwise, your going to have to schedule it over a span of a few days and determine if it is truly the last day of the month.

if that is acceptable, then the cron entry would look something like this:

01 00 01 * * /myscript > /dev/null &2> /dev/null
A. Clay Stephenson
Acclaimed Contributor

Re: inputting date into a file

Oh, and if you do need to determine if today is the last day of the month, then this works quite well:

if [[ $(caljd.sh -M) -ne $(caljd.sh -n 1 -M) ]]
then
echo "It's that last day of the month."
else
echo "It ain't."
fi

You should be able to find a copy of caljd.sh with a simple search. The latest version is 2.3 s. Make sure that the directory which contains caljd.sh in in your current PATH.
If it ain't broke, I can fix that.