Link to home
Start Free TrialLog in
Avatar of Kyroii2
Kyroii2

asked on

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!
Avatar of Kyroii2
Kyroii2

ASKER

Oops... a typo :)

<center><table><tr>
<td>
hi
</td>
<td>
bye
</td>
</tr></table></center>
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
oops, please ignore "count=0" in the script :-)
Avatar of Kyroii2

ASKER

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?
Avatar of Duncan Roe
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>
ASKER CERTIFIED SOLUTION
Avatar of The_LowRider
The_LowRider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial