Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
I need little bash help
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
IvanZD
Apprentice
Apprentice


Joined: 04 Jul 2005
Posts: 166

PostPosted: Thu Apr 02, 2015 2:19 pm    Post subject: I need little bash help Reply with quote

Hello,

how to make this for loop nicer?

Code:
for TIME in 00 06 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108
 do
  echo $TIME
done


What I want is replace somehow those 00 06 ... with something like this {START..END..INCREMENT} where START is "00", END is "108" and INCREMENT is 6. However, numbers below 10 must be with preceeding zero (00, not 0; 06, not 6) and those larger than 9 shoud not have preceeding zero (12, 18,... ; not 012, 018,...).

Tried something like this but doesn't work correctly:
Code:
for TIME in {00..108..6}
 do
  echo $TIME
done

Output:
Code:
./test.sh
000
006
012
018
024
...


It should be
Code:
./test.sh
00
06
12
18
24
...


Also when I fix that preceeding zeros, I will need another fix. So, I also need to be able to control START, END and INCREMENT with variables. However, when I try:

Code:
START=00
END=108
INCREMENT=6

for TIME in {${START}..${END}..${INCREMENT}}
 do
  echo $TIME
done


All breaks:
Code:
./test.sh
{00..108..6}



Thanks for any help!
Ivan
_________________
http://www.meteoadriatic.net/
Back to top
View user's profile Send private message
Naib
Watchman
Watchman


Joined: 21 May 2004
Posts: 5775
Location: Removed by Neddy

PostPosted: Thu Apr 02, 2015 2:25 pm    Post subject: Reply with quote

Code:
for i in {0..108..6}
do
  echo "$i"
done

_________________
The best argument against democracy is a five-minute conversation with the average voter
Great Britain is a republic, with a hereditary president, while the United States is a monarchy with an elective king
Back to top
View user's profile Send private message
IvanZD
Apprentice
Apprentice


Joined: 04 Jul 2005
Posts: 166

PostPosted: Thu Apr 02, 2015 2:32 pm    Post subject: Reply with quote

Hi Naib,

this also doesn't work also because there are no preceeding zeros for 0 or 6.

Ivan
_________________
http://www.meteoadriatic.net/
Back to top
View user's profile Send private message
Dr.Willy
Guru
Guru


Joined: 15 Jul 2007
Posts: 504
Location: NRW, Germany

PostPosted: Thu Apr 02, 2015 3:02 pm    Post subject: Reply with quote

IvanZD wrote:
this also doesn't work also because there are no preceeding zeros for 0 or 6.

printf should do the trick
Back to top
View user's profile Send private message
Naib
Watchman
Watchman


Joined: 21 May 2004
Posts: 5775
Location: Removed by Neddy

PostPosted: Thu Apr 02, 2015 3:04 pm    Post subject: Reply with quote

Code:

START=0
STOP=108
STEP=6

for ((i=$START;i<=$STOP;i+=$STEP))
do
  printf "%02d\n" "$i"
done

_________________
The best argument against democracy is a five-minute conversation with the average voter
Great Britain is a republic, with a hereditary president, while the United States is a monarchy with an elective king
Back to top
View user's profile Send private message
IvanZD
Apprentice
Apprentice


Joined: 04 Jul 2005
Posts: 166

PostPosted: Thu Apr 02, 2015 3:42 pm    Post subject: Reply with quote

Thanks a lot, now this is exactly right!
_________________
http://www.meteoadriatic.net/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum