LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2014, 10:41 PM   #1
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
if [[ endodrowcount < 2 ]]; then


In this code snippet from a much larger file, the logic of counting to 2 seems to have vanish from my brain.
Code:
image_body="
<td align=\"center\"><p style=\"margin-top: 0; margin-bottom: 0\"><img border=\"0\" src=\"$image_name\"></p> <p style=\"margin-top: 0; margin-bottom: 0\">\"$image_type\"</p> </td>"
echo $image_body >> $project;
####code below
	if [[ endodrowcount < 2 ]]; then
		endofrowcount=endofrowcount+1
	else
		echo "</tr><tr>" >> $project;
		endofrowcount=0
	fi
####code above
done
I want to print 2 row of 'table data' and then 1 row of 'table row'. This is what I am getting.
Code:
</tr><tr>
<td align="center"><p style="margin-top: 0; margin-bottom: 0"><img border="0" src="Well Dressed Fisherman for Bitterroot April 2011 7x5 72.jpg"></p> <p style="margin-top: 0; margin-bottom: 0">"Well Dressed Fisherman for Bitterroot April 2011 7x5 72"</p> </td>
</tr><tr>
<td align="center"><p style="margin-top: 0; margin-bottom: 0"><img border="0" src="Nilo catching a ladyfish on the fly 1.JPG"></p> <p style="margin-top: 0; margin-bottom: 0">"Nilo catching a ladyfish on the fly 1"</p> </td>
</tr><tr>
<td align="center"><p style="margin-top: 0; margin-bottom: 0"><img border="0" src="Wead about to hit the fast water.JPG"></p> <p style="margin-top: 0; margin-bottom: 0">"Wead about to hit the fast water"</p> </td>
</tr><tr>
<td align="center"><p style="margin-top: 0; margin-bottom: 0"><img border="0" src="ffflogo.gif"></p> <p style="margin-top: 0; margin-bottom: 0">"ffflogo"</p> </td>
Someone please tell me what I am doing wrong before I have a chocolate ice cream attack.

Thanks for you help
Dave
 
Old 09-10-2014, 11:13 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Code:
if [ $endofrowcount -lt 2 ]; then
1. endodrowcount -> endofrowcount
2. endofrowcount -> $endofrowcount
3. Remove bashism "[[ ]]" -> "[ ]" (you didn't say what shell)
4. Use numerical comparison "<" -> "-lt"



Evo2.
 
1 members found this post helpful.
Old 09-11-2014, 12:14 AM   #3
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Hi Dave!

Quote:
Originally Posted by Dafydd View Post
I want to print 2 row of 'table data' and then 1 row of 'table row'.
IF by that you mean that you want to have 2 table cells per table row, then I suspect you might want something more like this:


Contents of file 2td_per_tr.data is as follows:
Code:
Well Dressed Fisherman for Bitterroot April 2011 7x5 72.jpg
Well Dressed Fisherman for Bitterroot April 2011 7x5 72
Nilo catching a ladyfish on the fly 1.JPG
Nilo catching a ladyfish on the fly 1
Wead about to hit the fast water.JPG
Wead about to hit the fast water
ffflogo.gif
ffflogo

Contents of file 2td_per_tr.bash is as follows:
Code:
#!/bin/bash

set -x

function generate_table_cell()
{
    cat  >>  $project_file  <<EOTD
        <td align="center">
            <p style="margin-top: 0px ;  margin-bottom: 0px ;">
                <img border="0px ;"  src="$image_file_name">
            </p>
            <p style="margin-top: 0px; margin-bottom: 0px ;">
                $image_description
            </p>
        </td>
EOTD
}

project_file='./test_output.html'
table_cell_count=0

>  $project_file

(

    read  -t 10  image_file_name
    read  -t 10  image_description
    result=$?

    while  [[  $result  -eq  0  ]]
        do

            if [[  $(( table_cell_count  %  2  ))  -eq  0  ]]
                then
                    echo  -e  '\t<tr>'  >>  $project_file
            fi


            generate_table_cell

            table_cell_count=$table_cell_count+1


            if [[  $(( table_cell_count  %  2  ))  -eq  0  ]]
                then
                    echo  -e  '\t</tr>'  >>  $project_file
            fi

            read  -t 10  image_file_name
            read  -t 10  image_description
            result=$?

        done

)  <  2td_per_tr.data

With those, when I run 2td_per_tr.bash I get this output in test_output.html :
Code:
    <tr>
        <td align="center">
            <p style="margin-top: 0px ;  margin-bottom: 0px ;">
                <img border="0px ;"  src="Well Dressed Fisherman for Bitterroot April 2011 7x5 72.jpg">
            </p>
            <p style="margin-top: 0px; margin-bottom: 0px ;">
                Well Dressed Fisherman for Bitterroot April 2011 7x5 72
            </p>
        </td>
        <td align="center">
            <p style="margin-top: 0px ;  margin-bottom: 0px ;">
                <img border="0px ;"  src="Nilo catching a ladyfish on the fly 1.JPG">
            </p>
            <p style="margin-top: 0px; margin-bottom: 0px ;">
                Nilo catching a ladyfish on the fly 1
            </p>
        </td>
    </tr>
    <tr>
        <td align="center">
            <p style="margin-top: 0px ;  margin-bottom: 0px ;">
                <img border="0px ;"  src="Wead about to hit the fast water.JPG">
            </p>
            <p style="margin-top: 0px; margin-bottom: 0px ;">
                Wead about to hit the fast water
            </p>
        </td>
        <td align="center">
            <p style="margin-top: 0px ;  margin-bottom: 0px ;">
                <img border="0px ;"  src="ffflogo.gif">
            </p>
            <p style="margin-top: 0px; margin-bottom: 0px ;">
                ffflogo
            </p>
        </td>
    </tr>

N.B. The presumption here is that you have enough data in the data file, for an even number of table cells. Otherwise there would need to be a little additional logic to generate the final closing table row end tag.

Apart from the indenting and such, if that's not at all the type of output you want, maybe you could give us some additional details of what you're trying to accomplish.

HTH.

Last edited by rigor; 09-11-2014 at 12:20 AM.
 
1 members found this post helpful.
Old 09-11-2014, 12:18 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
I did not understand the question, but if this was a copy/paste then you seem to have a typo in the code (otherwise a typo in the post):

Code:
####code below
	if [[ endodrowcount < 2 ]]; then
		endofrowcount=endofrowcount+1
	else
		echo "</tr><tr>" >> $project;
		endofrowcount=0
	fi
####code above
 
1 members found this post helpful.
Old 09-11-2014, 12:33 AM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

oh and
Code:
endofrowcount=endofrowcount+1
is not going to increment the variable in any bourne style shell that I know of.

If this is bash you can use
Code:
((endofrowcount++))
If straight bourne shell:
Code:
endofrowcount=`expr $endofrowcount + 1`
HTH,

Evo2.
 
1 members found this post helpful.
Old 09-11-2014, 05:52 PM   #6
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Hi Dave,

Just so we're not working at cross purposes here, as evo2 effectively said here:
Quote:
Originally Posted by evo2 View Post

If this is bash you can use
Code:
((endofrowcount++))
something like
Code:
table_cell_count=$table_cell_count+1
does not actually increment the value of table_cell_count.

Since as has been pointed out by other folks, you had made some typing errors, I was hoping your use of the "+1" was just a different sort of mistake. I was hoping you are using bash, and are very familiar with bash.

If you are using bash, but are not familiar with it in detail, then please note the following.

What I did actually concatenates a '+1' onto the end of the existing string value of the table_cell_count. I was cheating and taking advantage of a side effect of the later execution of $(( table_cell_count % 2 )) which evaluates a string something like 0+1+1+1+1+1 in an arithmetic context.

I really had trouble trying to figure out exactly what you were trying to do. I guessed that maybe for some reason you were going in the direction of using $(()) later in your code, but just hadn't gotten there yet. I thought if what you were doing was just a "one off", just for a single use, not something that would be processing a lot of data on an ongoing basis, it wasn't too bad.

In general, what evo2 showed you ((endofrowcount++)) should be used because it's more efficient.
 
1 members found this post helpful.
Old 09-11-2014, 09:03 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Also, within [[ ... ]] variables need to be introduced with "$"
Code:
if [[ $endofrowcount < 2 ]]; then
The lack of "$" does not result in an error message because the "<" operator is performing a string comparison, so you are actually comparing the string "endofrowcount" with the string "2".

Last edited by rknichols; 09-11-2014 at 09:14 PM.
 
1 members found this post helpful.
Old 09-14-2014, 04:31 PM   #8
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
A great big thanks to all who contributed to this thread.

Here is the complete project. I know it is messy coding, but it serves my needs and I am the only one I need to please in this case.

Again, THANK YOU.

Code:
#! /bin/bash
## Write HTML table code for a large number of images

project=$(date +"%m-%d-%Y")
endorrowcount=0
endofrow="</tr><tr>"
TitlePlace="<!-- Page title and commentary text go here. -->"
template="
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\>
<html><head></head>;

<body bgcolor=\"#efb321\" text=\"#f8c99f\" link=\"#006666\" vlink=\"#003300\">;

<p align=\"center\">< style=\"line-height: 100%; margin-top: 0; margin-bottom: 0\">" 
echo $template >> $project;
echo $TitlePlace >> $project;
tabletemplate="<table border=\"1\" width=\"90%\" bordercolor=\"#f8c99f\" bordercolorlight=\"#efb321\" bordercolordark=\"#f8c99f\" bgcolor=\"#a95205\" \ id=\"table1\">"
echo $tabletemplate >> $project;

for image_name in *.jpeg *.jpg *.JPG *.gif *.GIF *.png *.PNG; do
	    [ -e "$image_name" ] || continue
		image_type=${image_name%.*};

image_body="
<td align=\"center\"><p style=\"margin-top: 0; margin-bottom: 0\"><img border=\"0\" src=\"$image_name\"></p> <p style=\"margin-top: 0; margin-bottom: 0\">\"$image_type\"</p> </td>"
echo $image_body >> $project;
 
	if [[ $endofrowcount < 1 ]]; then
		((endofrowcount++))
		##break;
	else
		echo "$endofrow" >> $project;
		endofrowcount=0
	fi

done

echo "</body></html>" >> $project
 
  


Reply



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



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 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
Open Source Consulting | Domain Registration