LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-30-2011, 05:39 PM   #1
ravedog
Member
 
Registered: Jun 2009
Distribution: Ubuntu, Sabayon
Posts: 38

Rep: Reputation: 0
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
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
big thanks in advance!

/R
 
Old 12-30-2011, 05:56 PM   #2
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 258

Rep: Reputation: 41
Why not just increment wcount by 10 instead of 1?

Code:
wcount=$(expr $wcount + 10)
Scratch that - just realised

Last edited by Roken; 12-30-2011 at 05:57 PM.
 
Old 12-30-2011, 06:03 PM   #3
ravedog
Member
 
Registered: Jun 2009
Distribution: Ubuntu, Sabayon
Posts: 38

Original Poster
Rep: Reputation: 0
Hehe Thanks for quick reply though!
 
Old 12-30-2011, 06:41 PM   #4
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 258

Rep: Reputation: 41
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

wcount=0
testcount=$wcount
while [ $wcount -lt 100 ];
do
  wcount=$(expr $wcount + 1)
  test=$(expr $wcount / 10)
  if [ `echo $test | sed 's/^.*\.//'` -gt $testcount  ]; then
    echo $wcount
    testcount=$(expr $testcount + 1)
  fi
done
Note: You can substitute "/ 10" in line 8 for "/ whatever number" to change the tested increment.

Last edited by Roken; 12-30-2011 at 06:44 PM.
 
1 members found this post helpful.
Old 12-30-2011, 06:52 PM   #5
ravedog
Member
 
Registered: Jun 2009
Distribution: Ubuntu, Sabayon
Posts: 38

Original Poster
Rep: Reputation: 0
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
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.

Thanks mate!

/R
 
Old 12-30-2011, 06:59 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Code:
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
 
1 members found this post helpful.
Old 12-30-2011, 07:02 PM   #7
ravedog
Member
 
Registered: Jun 2009
Distribution: Ubuntu, Sabayon
Posts: 38

Original Poster
Rep: Reputation: 0
Worked like a charm, thanks to both of you for the solutions!

/R
 
Old 12-30-2011, 07:05 PM   #8
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 258

Rep: Reputation: 41
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
(this one does every 9th ) EDIT: simpler still

Last edited by Roken; 12-30-2011 at 07:08 PM.
 
Old 12-30-2011, 07:09 PM   #9
ravedog
Member
 
Registered: Jun 2009
Distribution: Ubuntu, Sabayon
Posts: 38

Original Poster
Rep: Reputation: 0
@ 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
 
Old 12-31-2011, 11:44 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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:

1) A c-style for loop.

2) brace expansion

3) The seq command
 
1 members found this post helpful.
Old 12-31-2011, 12:13 PM   #11
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 258

Rep: Reputation: 41
Thumbs up

Quote:
Originally Posted by David the H. View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 10:59 AM
BASH script optimization for testing large number of files instag Programming 24 09-27-2010 12:40 AM
Bash script question - formatting a number HarryBoy Linux - Newbie 10 08-31-2010 11:07 PM
Testing for enter in bash script bwysocki Programming 8 06-16-2005 10:11 AM
bash script to download a number of things linksocc Linux - Software 3 12-10-2003 01:18 PM


All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration