Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
while read line
do
echo '<td><font size="1" face="arial">'$line"</font></td>"
wcount=$(expr $wcount + 1)
if [ $wcount -eq *0 ]; then
echo "</tr><tr>"
fi
done < $list
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
echo "This ends with 0 and has at least one number in front of it"
fi
But I'm not at all sure of that and ill definitely look in to modify your solution to do it if no such solution exists.
wcount=0
while read line
do
echo '<td><font size="1" face="arial">'$line"</font></td>"
let wcount++
if [[ "$wcount" = *0 ]]; then
echo "</tr><tr>"
fi
done < $list
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
wcount=0
testcount=$wcount
while [ $wcount -lt 100 ];
do
wcount=$(expr $wcount + 1)
if [ $(expr $wcount / 9) -gt $testcount ]; then
echo $wcount
testcount=$(expr $testcount + 1)
fi
done
@ 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.
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
(( wcount++ ))
if (( wcount % 10 == 0 )); then
echo "$wcount"
fi
done <$list
Here are three more ways to work with incremental values in bash:
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.
Good call. I knew about this, of course, and completely forgot it. Thanks for the reminder
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.