?
Solved

script to make HTMl tables

Posted on 2005-03-24
6
Medium Priority
?
226 Views
Last Modified: 2012-05-05
Hi,

I'd like to make a simple script that writes HTML tables, so I thought something like this:

function tabla()
{
  echo "<center><table style=\"width: $1%; height: ${2}px\"><tr>"
  for I in `seq 1 $3`
  do
    echo "<td>"
    echo "</td>"
  done
  echo "</tr></table></center>"
}

$1 = Width in %
$2 = Height in px
$3 = Nº of columns

#Nº Rows always = 1

But I also want this function to take the content in columns by parameters. For ex:

tabla 90 30 2 "hi" "bye"

<center><table><tr>
<td>
hi
</td>
<td>
bye
</bye>
</tr></table></center>

But I don't now how to do it

Can u guys help me?

Thk u all!
0
Comment
Question by:Kyroii2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:Kyroii2
ID: 13624899
Oops... a typo :)

<center><table><tr>
<td>
hi
</td>
<td>
bye
</td>
</tr></table></center>
0
 
LVL 1

Expert Comment

by:The_LowRider
ID: 13626136
Hi, you could put your entries in a file and redirect from standard input:

--- start of script.sh ---

#!/bin/bash
#

count=0
echo "<center><table style=\"width: $1%; height: ${2}px\"><tr>"
while read name
do
  echo "<td>"$name"</td>"
done
echo "</tr></table></center>"

--- end of script.sh ---

use it like this:

$ ./script.sh < data.txt

data.txt is a simple ASCII file that holds the entries you want. Every entry on a separate line...
like this:

hi
bye

Nicolas
0
 
LVL 1

Expert Comment

by:The_LowRider
ID: 13626141
oops, please ignore "count=0" in the script :-)
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

Author Comment

by:Kyroii2
ID: 13626679
Ok thks 4 your answer. That should work but i'd prefer a solution without using any file, any ideas?

By the way

#########
#! /bin/bash

VAR=0
function FUNC()
{
    let VAR++
}

#main
printf "`FUNC`$VAR"
###########

Do you know why VAR isn't updated in the function FUNC? How to solve this?
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 13627522
There's no guarantee as to the order that printf will pick up its args. So it could have got the value of VAR before it called FUNC.
There's no need to backtick FUNC because it doesn't produce any output.
Also it's good practice to always put a format in the format (first) argument - you never know when data might contain printf control characters. This works:
#! /bin/bash

VAR=0
function FUNC()
{
    let VAR++
}

#main
FUNC
printf "%s\n" "$VAR"

To answer your original question, you can use a Here Document, as in:
#!/bin/bash
#

echo "<center><table style=\"width: $1%; height: ${2}px\"><tr>"
while read name
do
  echo "<td>"$name"</td>"
done<<EOF
hi
bye
EOF
echo "</tr></table></center>"

Try a run:
13:25:19$ ./html.sh
<center><table style="width: %; height: px"><tr>
<td>hi</td>
<td>bye</td>
</tr></table></center>

Here Documents can contain shell variables, so you can get a bit fancier:
#!/bin/bash
#

echo "<center><table style=\"width: $1%; height: ${2}px\"><tr>"
while read name
do
  echo "<td>"$name"</td>"
done<<EOF
hi $1
bye $1
EOF
echo "</tr></table></center>"

Run with argument "fred"
13:28:20$ ./html.sh fred
<center><table style="width: fred%; height: px"><tr>
<td>hi fred</td>
<td>bye fred</td>
</tr></table></center>
0
 
LVL 1

Accepted Solution

by:
The_LowRider earned 2000 total points
ID: 13628884
You could also use the "shift" command to read the input as arguments:

--- start of script.sh ---

#!/bin/bash
#

count=0
echo "<center><table style=\"width: $1%; height: ${2}px\"><tr>"
until [ -z "$3" ]
do
  echo "<td>$3</td>"
  shift
done
echo "</tr></table></center>"

--- end of script.sh --

Use it like this:

[lowrider@c3p0 ~]$ ./script.sh 100 100 hi bye hello blahblah somemore ...
<center><table style="width: 100%; height: 100px"><tr>
<td>hi</td>
<td>bye</td>
<td>hello</td>
<td>blahblah</td>
<td>somemore</td>
<td>...</td>
</tr></table></center>

Concerning your other question, I must agree with duncan_roe.

Hope it helps.

Nicolas.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question