ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I sort my security logs on a weekly basis in folders like week-01, week-02,..., week-52 so that it's easier to audit. I've written a little script to compress the content of weeks that are at least 3 weeks behind the current week, but I'm having a number formatting problem I'm hoping someone can help me with.
Code:
ARCHPATH=/tmp
HOSTNAME=`/bin/hostname`
WEEK=`date +%U`
YEAR=`date +%Y`
DATE=`date +%N-%F`
PWEEK=`expr $WEEK - 3`
if [ "$WEEK" -eq 01 -a -d "$ARCHPATH/$HOSTNAME/week-50" ] ; then
cd $ARCHPATH/$HOSTNAME/week-50
/usr/bin/zip -rm $HOSTNAME-50-$YEAR.zip . -x '*.zip'
elif [ "$WEEK" -eq 02 -a -d $ARCHPATH/$HOSTNAME/week-51 ] ; then
cd $ARCHPATH/$HOSTNAME/week-51
/usr/bin/zip -rm $HOSTNAME-51-$YEAR.zip . -x '*.zip'
elif [ "$WEEK" -eq 03 -a -d $ARCHPATH/$HOSTNAME/week-52 ] ; then
cd $ARCHPATH/$HOSTNAME/week-52
/usr/bin/zip -rm $HOSTNAME-52-$YEAR.zip . -x '*.zip'
elif [ -d $ARCHPATH/$HOSTNAME/week-$PWEEK -a $PWEEK -ne 50 -a $PWEEK -ne 51 -a $PWEEK -ne 52 ] ; then
cd $ARCHPATH/$HOSTNAME/week-$PWEEK
/usr/bin/zip -rm $HOSTNAME-$PWEEK-$YEAR.zip . -x '*.zip'
else
exit 0
fi
My problem is that if its weeks 01-09, it for formats my search to look at "week-1" instead of "week-01". Is there something I can do to format the first 9 numbers so they come out correct" As always, thanks for the help
slightly off topic regarding the zero padding of ints, but instead of reinventing the wheel with you script, is there any reason you are not using a standard tool like logrotate?
Thank you for the responses. I'll look more into the printf command and how I can tweak my script a little to make it work.
I actually don't use logrotate because of the nature of our environment. Not all our UNIX machines are Linux and so I use something like this that work across different platforms. I do like logrotate for standalone machines.
I'm concerned about a couple things:
1. You're underquoting your variables. You should always have "" around your variables unless there's a *very* specific reason you're not
2. Is this actually bash, or really bourne shell? if it's actually bash, you should use [[ ]] or (( )) rather than [ ] for tests and $(( )) instead of expr for math.
Thanks for the reply Tuxdev. As you can tell I'm a script scrapper. I've added the "" to my variables and that seems to work fine, but when I added [[ ]] to my statements it's breaks saying:
Code:
./audit_compress.sh: line 28: syntax error in conditional expression
./audit_compress.sh: line 28: syntax error near `-a'
So for the sake of teaching me something, why does adding the double brackets break my script? I just don't understand the difference between [ ] and [[ ]] I guess.
I'll also play around with the $(()) stuff too...but expr did what I needed so that's what I used it.
Your script interpreter is obviously not bash (or another advanced shell like ksh or zsh) then. "[[" is an upgraded form of test available only to those shells. It's possible that your system uses dash as the underlying sh script interpreter, for example, in which case you need to limit your script to POSIX-defined script syntax.
If, however, all of the machines on your system are guaranteed to have bash installed, then be sure to use "#!/bin/bash" as the first, "shebang" line, to explicitly tell the system to use that as the interpreter. Only use "#!/bin/sh" if you absolutely need it to be portable to multiple shells. And if so, again, use POSIX code only.
Except in the case of truly ancient shells, integer arithmetic will be built-in. Using expr in such an environment is just a waste of a process. Every POSIX-supporting shell will accept the $((..)) arithmetic form at the very least.
Next, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.
Finally, the bash version of printf has a -v option that allows setting variables directly. So your solution could be reduced to this:
Code:
printf -v pweek '%02d' "$( week - 3 )"
PS: Be sure to only do your zero-padding after any arithmetic functions are finished. Your shell will treat any numbers with leading zeroes as octal values, and you'll end up with errors and mistaken calculations.
Last edited by David the H.; 07-25-2012 at 11:36 AM.
Reason: addendum
David the H, thank you so much for taking the time to write this up. I really feel like I've learned something in this thread and I know it will help others as well. Even though my script works the way I want it too, I'm changing it so that it's done properly. Thanks again!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.