Bash Script - quick question
Discussion
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
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
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
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
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
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!
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
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
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
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
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
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
Hope that clears it up a tad.
Ciao
Zak
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
[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
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



I'm no expert, and I haven't tested this, but what about: