LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2012, 05:40 PM   #1
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Rep: Reputation: 31
Bash script number formatting


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
 
Old 07-23-2012, 06:12 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Use printf for this...

Code:
[root@host]# mynum=1
[root@host]# printf "%02d" ${mynum}
01
 
1 members found this post helpful.
Old 07-23-2012, 07:03 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

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?

Evo2.
 
Old 07-23-2012, 11:52 PM   #4
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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.
 
Old 07-24-2012, 05:54 AM   #5
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
mijohnst
i take it you are getting a number sequence like this and it is driving you "batty"

Code:
1,10,11,12,13,14,15,16,17,18,19,2,20,21, .......
padding the 1 ,2,3,4 with 01,02,03 will help
 
Old 07-24-2012, 11:32 AM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-24-2012, 11:53 AM   #7
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
Thank you guys for the help... Adding the following declaration fixed the problem!

Code:
PWEEK=`expr $WEEK - 3`
PWEEK=$(printf %02d $PWEEK)
 
Old 07-24-2012, 04:18 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 07-24-2012, 04:58 PM   #9
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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.

Thanks!
 
Old 07-25-2012, 11:32 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

This page lists the main coding differences between bash and posix shells.
http://mywiki.wooledge.org/Bashism


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.

arithmetic expressions


Another thing to avoid is the "`..`" (backtick) style of command substitution. All modern shells now support the cleaner and more robust $(..) form.

$(..) is highly recommended over `..`


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
 
1 members found this post helpful.
Old 07-27-2012, 12:33 AM   #11
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 09:59 AM
[SOLVED] Bash script formatting problem New2LinuxMN Linux - Newbie 6 10-02-2010 12:16 PM
Bash script question - formatting a number HarryBoy Linux - Newbie 10 08-31-2010 10:07 PM
BASH: Help with formatting Output from a script SilversleevesX Programming 4 08-23-2010 06:59 PM
bash script to download a number of things linksocc Linux - Software 3 12-10-2003 12:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration