Bash script - number testing issue
Heya!
I'm working on a cgi-bin script and bumped in to an issue. I will have around 500-600 objects red with in a while statement that just echoes the name and moves along as long as there is more to read. However i would like to, after 10 entries, to break and use a new line. In the while statement i have a var that +1 for every cycle in the loop, but what would be a good way to match all tens (both 10,20,30.....220,230,240) and break it off? It has crossed my mind to test the var and see if it ends with a "0" and in that case break for new line. But how? I pasted in the code i'm stuck with right now below, which obviously don't work :) Code:
wcount=0 /R |
Why not just increment wcount by 10 instead of 1?
Code:
wcount=$(expr $wcount + 10) |
Hehe :) Thanks for quick reply though!
|
OK - been playing. This will only echo every 10th number. Modify to suit your needs :)
(it only counts to 100 then exits, just for testing. You can remove the while statement) Code:
#!/bin/bash |
Hey Roken!
Thanks for your answer! I tried it and just as you said, every 10 was echoed :) This is however, a rather advanced solution to the problem though. I would have bet that there was a good way to do this from within the if test itself with "built ins". Something along the way of: Code:
if [ $wcount -eq *?0 ]; then Thanks mate! /R |
Code:
wcount=0 |
Worked like a charm, thanks to both of you for the solutions!
/R |
To test for every ten then Cedriks solution is cleaner. If you want to change the frequency (e.g., every 4, every 7, every 9, every 21 etc) then mine is more portable. However, I just found that bash doesn't do floating point, so it can be simplified somewhat (you need to use bc for floating point math).
Code:
#!/bin/bash |
@ Roken: Ah ok i see, well in this case it will only break to a new row when 10 names (10 cols) from the list has filled the width of the html table that it resides in. So for this case, the simplest possible will do fine.
However i love learning new stuff so ill bookmark this for future refs. :) Again thanks for your efforts! br, /R |
Why are using expr here, when there is absolutely no need for it? The shell can do integer math, and you have to use bc or awk for floating point anyway.
To increment an integer variable by one, you can simply use (( i++ )), and by other factors, use (( i+=2 )), etc. (There are many ways you can use arithmetic in the shell...see here for more.) Also, numeric tests in bash should be done with the same ((..)) arithmetic evaluation field, and string tests should be done with the new [[..]] test. Don't use the old [..] test unless you need POSIX-based compatiblity. Anyway, it looks like you need to learn about the "%" (modulo) arithmetic operator. It outputs the remainder after a division operation, for example, "6 % 4 --> 2" and "6 % 3 --> 0". Whenever you get 0, you know you have an even multiple of the divisor. Code:
while read line; do 1) A c-style for loop. 2) brace expansion 3) The seq command |
Quote:
|
All times are GMT -5. The time now is 01:45 AM. |