I know the subject is a little verbose, but that is exactly the trouble I am having. I have a script (many thanks to those on LQ who helped me create it) that moves files from one directory to another, based on the numeric date of the file name (i.e., 20091212 would go to the December directory). Now, since this script will be ran at the beginning of the following month (December's files to be tarred and gzipped will be performed the first day of January, and January's in February, etc.), it appears to me that the script will be tricky when it comes time to do the December files in January.
Here's part of the script:
Code:
# Define working directory and target directory.
DIR=/var/log
target=$DIR/month
hostname=`uname -n`
# Switch to working directory.
cd "$DIR"
echo "Entered working directory $DIR"
# Main loop.
for file in 2*.gz; do
echo "-----------------------"
echo "Processing file: $file."
echo
# Test whether file is actually a file.
if [[ ! -f $file ]]; then
echo "$file is not a regular file. Skipping"
continue
fi
case "${file:5:2}" in
01) mkdir -vp $target/January
mv -v --target-directory $target/January $file
;;
02) mkdir -vp $target/February
mv -v --target-directory $target/February $file
#### -----This continues for the remaining months-----
*) echo "File $file does not contain a valid month number."
;;
# Provides feedback for invalid hits.
esac
echo "-----------------------"
done
date=`date +%B`
###THIS IS WHERE I'M RUNNING INTO TROUBLE!!!###
case "${date}" in
January) cd $target/December
tar -czvf * December.`date +%Y.$hostname
;;
February) cd $target/January
tar -czvf * January.`date +%Y.$hostname
;;
esac
echo "Finished"
exit 0
I can't seem to figure out a way to carry the output of the date command to the next command, and the year for the December files will always be wrong. Anyone out there who can make sense of my needs?