Shell script to rotate & delete/housekeep away highest version logs

I have the following logs in /opt/avscan that's being created by a Shell script's redirection ie >  command)

update.log  (being created daily)

Would like to add extra lines of Shell script into the current script such that prior to running
the command that do > redirect, add the following task:
  if file exists, rm -f update9.log.gz
  if file exists, mv update8.log.gz update9.log.gz
  if file exists, mv update7.log.gz update8.log.gz
  if file exists, mv update6.log.gz update7.log.gz
  if file exists, mv update5.log.gz update6.log.gz
  if file exists, mv update4.log.gz update5.log.gz
  if file exists, mv update3.log.gz update4.log.gz
  if file exists, mv update2.log.gz update3.log.gz
  if file exists, mv update1.log.gz update2.log.gz
  if file exists, mv update.log update1.log
  gzip -9 update1.log
  ...  the rest of the existing Shell script codes ...
sunhuxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Here you go:

[[ -e update9.log.gz ]] && echo rm update9.log.gz
for n in 8 7 6 5 4 3 2 1 ; do
    [[ -e update${n}.log.gz ]] && echo mv update${n}.log.gz update$((n+1)).log.gz
done
[[ -e update.log ]] && echo mv update.log update1.log
echo gzip -9 update1.log

Again, please note "echo". The commands will be displayed but not executed. Remove "echo" to really get the task performed.
MikeOM_DBACommented:
Why re-invent the wheel, most Unix and Linux flavors have a logrotate utility.
Even Apache has one.
If not present, ask the sysadmin to install it.
It may even be possible to install it yourself.
TintinCommented:
Agree with Mike that you should use logrotate.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

sunhuxAuthor Commented:
Thanks chaps;  I still prefer Shell scripts because I'm not at liberty to install
any packages in our tenants' (Solaris) VMs.


WoolMilkPorc,
Is that limited to 9 versions or we can rotate to more versions where needed?
woolmilkporcCommented:
We can make the number of versions variable, if you wish.
woolmilkporcCommented:
Here is a snippet with almost everything made "variable":
Base name ("update"),  extension (".log"),  gzip suffix (".gz"),  high version ("15"),  low version ("1").

I omitted the first "rm" of the oldest version because "mv" will overwrite it anyway if appropriate.
Next, I added a line which skips all processing if there's no new "update.log", because
otherwise at each run without a new "update.log" being present you would loose one version.

Finally, please note that the "seq" utility is required here.
It is installed by default in most Linux'es, but If you don't have it please let me know.
We can go without it.

BASE="update"
EXT=".log"
ZIPPED=".gz"
VERSIONH=15
VERSIONL=1
if [[ -e ${BASE}${EXT} ]] ; then
  for VER in $(seq $((VERSIONH-1)) ${VERSIONL}) ; do
    [[ -e ${BASE}${VER}${EXT}${ZIPPED} ]] && mv ${BASE}${VER}${EXT}${ZIPPED} ${BASE}$((VER+1))${EXT}${ZIPPED}
  done
  mv ${BASE}${EXT} ${BASE}${VERSIONL}${EXT} && gzip ${BASE}${VERSIONL}${EXT}
fi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.