All,
I have a need for a portable shell script for LInux and HPUX. The script has a simple need; Check for local files of a specific name (i.e. filename*), scp them to another system, and archive them to a folder. The script runs via cron.
I first wrote the script in Linux (bash with gnu tools) pretty quickly just to get it to function. Then I was asked to do the same type of process running on HPUX (we have 11.31 and 11.23) and did some quick tweaks to get it to run on HPUX. I'm running into issues with the way I wrote it and timing of files written, sent, and archived (I didn't really have the scope of frequency).
Basically I want to put together a portable script with assurances that files will not get 'missed'.
My thought is:
-
do a listing of the desired files when the script is run
- If no files, exit
- If files exist, drop the listing into an array
-
use the array to send and archive files
Does this sound like the best/easiest way to accomplish this?
The current problem code I have is:
IFS=!
dest=servername
inst=INST2
logdir=/path/to/log/dir
month=`date +%b%Y`
logfile=${logdir}/xfer_${1}_${month}.log
if [ "$1" = "us" ]; then
filedir=/path/to/us/dir
destdir=/path/${inst}/path/us
elif [ "$1" = "eu" ]; then
filedir=/path/to/eu/dir
destdir=/path/${inst}/path/eu
fi
# --------------------------------------------------
# functions
# --------------------------------------------------
usage () {
echo "usage info"
}
header () {
divider===============================
divider=$divider$divider$divider
printf "\n$divider\n" >> $logfile 2>&1
echo "$1 -- `date`" >> $logfile 2>&1
printf "$divider\n" >> $logfile 2>&1
}
# --------------------------------------------------
# main
# --------------------------------------------------
if [ $# -ne 1 ]; then
usage
exit 1
fi
cd $filedir
files=`find $filedir -type f -name "iS*" | xargs -n1 printf %f!`
if [ ${#files[@]} -eq 0 ]; then
header "$1 files to send"
echo "\n No $1 files to transfer" >> $logfile
exit 1
else
header "$1 files to send"
ls -l iS* | awk '{print $9}' >> $logfile
header "Sending $1 files"
cd $filedir; scp iS* $dest:$destdir >> $logfile 2>&1
if [ $? -ne 0 ]; then
echo "\n There are no files to transfer..." >> $logfile 2>&1
header "Complete"
exit 1
else
header "$1 files successfully sent"
header "Moving files and purge 30+ days"
mv iS* ${filedir}/archive >> $logfile 2>&1
find ${filedir}/archive -type f -mtime +30 -ls -exec rm -f -- {} \; >> $logfile 2>&1
header "Finished moving files.."
fi
fi
exit
I know this code is not written very well as there is no checks on archiving only the files that were sent.
Any advise on improving for portability and simplifying is greatly appreciated.
TIA,
Herb