Bash Script - quick question
Author
Discussion

TheExcession

Original Poster:

11,669 posts

280 months

Thursday 24th February 2005
quotequote all
declare -i STARTYEAR=2005
declare -i STARTMONTH=2
declare -i STARTDAY=1
declare -i STARTHOUR=4
declare -i STARTMINUTE=8

PRESTATEFILENAME="$STARTYEAR$STARTMONTH/$STARTDAY/$STARTYEAR$STARTMONTH$STARTDAY-$STARTHOUR$STARTMINUTE"


How to get
200502/01/20050201-0408
and not
20052/1/200521-48.

ie. how to quickly pad these numbers to 2 chars?

many thanks
Ex

Plotloss

67,280 posts

300 months

Thursday 24th February 2005
quotequote all
Can you not declare STARTMONTH as 02 rather than 2?

I take it that if not its assuming STARTMONTH to be numeric and its stripping the leading zeroes?

Is that what the -i operator is doing? Declaring it as an integer?

Or can you in the string do "0"&&$STARTMONTH$ or similar?

>> Edited by Plotloss on Thursday 24th February 11:10

zaktoo

1,401 posts

270 months

Thursday 24th February 2005
quotequote all
printf "%02d" VARIABLE

HTH

Ciao

Zak

TheExcession

Original Poster:

11,669 posts

280 months

Thursday 24th February 2005
quotequote all
Plotloss said:
Can you not declare STARTMONTH as 02 rather than 2?


the numbers are passed in fro manother program so cant go there.

Plotloss said:

I take it that if not its assuming STARTMONTH to be numeric and its stripping the leading zeroes?

correct

Plotloss said:

Or can you in the string do "0"&&$STARTMONTH$ or similar?


will that work if the number is 12 - wouldn't I end up with 012?

Where is sprintf when you need it?

best
Ex

jeremyc

28,176 posts

314 months

Thursday 24th February 2005
quotequote all
I'm no expert, and I haven't tested this, but what about:

declare -i HOUR=4
declare -i STARTHOUR=’printf%02d $HOUR’

TheExcession

Original Poster:

11,669 posts

280 months

Thursday 24th February 2005
quotequote all
zaktoo said:
printf "%02d" VARIABLE

HTH

Ciao

Zak


thanks Zak, thought it would be something like that - so I just do
STARTMONTH= printf "%02d" STARTMONTH ?

(I'm very new at this shell scripting nonesense if you hadn't noticed!)

best
Ex

zumbruk

7,848 posts

290 months

Thursday 24th February 2005
quotequote all
The correct answer to this is, as usual, learn Perl.

But here's a hint; "bash" has the "printf" function, very similar to 'C'.

zumbruk

7,848 posts

290 months

Thursday 24th February 2005
quotequote all
TheExcession said:

zaktoo said:
printf "%02d" VARIABLE

HTH

Ciao

Zak



thanks Zak, thought it would be something like that - so I just do
STARTMONTH= printf "%02d" STARTMONTH ?


Yes.


TheExcession said:
(I'm very new at this shell scripting nonesense if you hadn't noticed!)

best
Ex


Learn Perl! You won't regret it!

zaktoo

1,401 posts

270 months

Thursday 24th February 2005
quotequote all
TheExcession said:

zaktoo said:
printf "%02d" VARIABLE

HTH

Ciao

Zak



thanks Zak, thought it would be something like that - so I just do
STARTMONTH= printf "%02d" STARTMONTH

STARTMONTH=`printf "%02d" STARTMONTH1`
should do it - note backticks, not single quotes.

Depending on what you're trying to acheive, there may be an even easier way. What does your script do in its entirety?

Ciao

Zak

rebelstar

1,146 posts

274 months

Thursday 24th February 2005
quotequote all
zumbruk said:
Learn Perl! You won't regret it!

Yes you will. Perl should be taken out and shot.

TheExcession

Original Poster:

11,669 posts

280 months

Thursday 24th February 2005
quotequote all
zaktoo said:

note backticks, not single quotes.

Depending on what you're trying to acheive, there may be an even easier way. What does your script do in its entirety?

Ciao

Zak

Duh - muppets mistake - thanks for that!

As for the script -
we have a set of directory built based on year and month

200501/
200502/ etc

inside each of these are daily directories
so...
200502/01
..
200502/28

inside these daily directories we have some state files usually built on a 15 minute interval
e.g.
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0000_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0015_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0030_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0045_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0100_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0115_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0130_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0145_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0200_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0215_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0230_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0245_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0300_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0315_00000.post_state
etc...

my script takes a start and stop date and time
./search.sh 200502240923 200502241023
and needs to build an array of candidate state files on which we will later do some searching.

the script is finished but I've now found that the files don't always lie on the 15 minute boundaries, so now I need to build the list based on the actual filenames that exist.

Chopping up the file names checking their times and then if they fall inside the boundaries storing them into an array that I can process later i nthe script.

If anyone knows a quick clean method for doing this I'll owe em a beer!

best
Ex


zaktoo

1,401 posts

270 months

Thursday 24th February 2005
quotequote all
TheExcession said:

As for the script -
we have a set of directory built based on year and month

200501/
200502/ etc

inside each of these are daily directories
so...
200502/01
..
200502/28

inside these daily directories we have some state files usually built on a 15 minute interval
e.g.
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0000_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0015_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0030_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0045_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0100_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0115_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0130_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0145_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0200_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0215_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0230_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0245_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0300_00000.post_state
/export/LTCT/Spot1/daily/200502/23/burum_20050223-0315_00000.post_state
etc...

my script takes a start and stop date and time
./search.sh 200502240923 200502241023
and needs to build an array of candidate state files on which we will later do some searching.

the script is finished but I've now found that the files don't always lie on the 15 minute boundaries, so now I need to build the list based on the actual filenames that exist.

Chopping up the file names checking their times and then if they fall inside the boundaries storing them into an array that I can process later i nthe script.



Do you have any control over how those files are named in the first place?

Ciao

Zak

TheExcession

Original Poster:

11,669 posts

280 months

Thursday 24th February 2005
quotequote all
zaktoo said:

Do you have any control over how those files are named in the first place?

Ciao
Zak

Not at this stage - I can't really get that changed - we have a lot of archive data in this format.

zaktoo

1,401 posts

270 months

Thursday 24th February 2005
quotequote all
ok this should get you on the right track I think:

for i in `find . -type f` ; do if [ ${i:5:8} -gt 20050227 ]; then echo $i ; fi ; done

You will have to adjust the ${i:5:8} to spit out the first bit - you're looking for the 20050227 bit of the filename there. You'll have to add a second if to grab the 0215 bit also, and compare to your time slots. It is quite a juicy one-liner, but should work fine with some fiddling for your system. If you're finding it tough to finish off, just shout, I hope I'll see it ;-) PM me if necessary...

HTH

Ciao

Zak

zaktoo

1,401 posts

270 months

Thursday 24th February 2005
quotequote all
oops. Should have mentioned that the ${i:5:8} bit is a substring operator. We're basically sticking the filenames in $i (for i in....) and then testing the portion of that filename against your chosen year month day range. So the 8 (substring length) is going to be correct. You'll need to change the start point (5) to suit your specific filename system.

Hope that clears it up a tad.

Ciao

Zak

TheExcession

Original Poster:

11,669 posts

280 months

Thursday 24th February 2005
quotequote all
thanks Zak - that's good stuff I'll take a play with it later!

cheers
Ex

TheExcession

Original Poster:

11,669 posts

280 months

Friday 25th February 2005
quotequote all
Hi all, back again with more stupid questions!
[Mr Garrison]
There are no stupid questions, just stupid people
[/Mr Garrison]

Back OT, I'm having some succcess with this but am stuck allocating the results to an array...

e.g.
declare -a CANDIDATE_FILE_LIST
CANDIDATE_FILE_LIST=`for i in `find . -name "*.post_state" -type f`; do echo $i; done`

having the nested commands in the back ticks seems to be screwing things up, any pointers as to how to correct the syntax.

Many thanks
Ex

TheExcession

Original Poster:

11,669 posts

280 months

Friday 25th February 2005
quotequote all
ahhh getting somewhere:

CANDIDATE_FILE_LIST=$(for i in `find . -name "*.post_state" -type f`; do echo $i; done)