• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

script to make HTMl tables

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
Kyroii2
Asked:
Kyroii2
  • 3
  • 2
1 Solution
 
Kyroii2Author Commented:
Oops... a typo :)

<center><table><tr>
<td>
hi
</td>
<td>
bye
</td>
</tr></table></center>
0
 
The_LowRiderCommented:
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
 
The_LowRiderCommented:
oops, please ignore "count=0" in the script :-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Kyroii2Author Commented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
The_LowRiderCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now