Writing HTML with variables from an array or other means


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing HTML with variables from an array or other means
# 1  
Old 05-09-2013
Writing HTML with variables from an array or other means

Hello!

I wish to create a script that will automate the creation of HTML files...

for example, if my HTML starts out containing:

Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$TITLE</title>
</head>

I want to put that into something that I can read and write later on. My idea was to put that into an array, then call it like so:

Code:
HTML_HEADER=(
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$TITLE</title>
</head>
)

Then later:
Code:
TITLE=1Fish2Fish

for z in "${HTML_HEADER[@]}"
do
echo "z" >> OUTPUT.HTML
done


This doesn't work, as there are quotes and other characters I assume will be mis-interpreted by bash.

I want to be able to write the HTML lines without having to escape quotes and other characters and as my HTML changes, I want to make it simple for me to just drop in the modified HTML code into the array.

Please let me know if there is an easy way to go about this, or if what I am trying to accomplish does not make sense.

Thank you in advance!Smilie

Last edited by Scrutinizer; 05-09-2013 at 03:50 PM.. Reason: code tags
# 2  
Old 05-09-2013
Please use code tags for posting code fragments or data samples.

If awk is an option then you could do something like:
Code:
TITLE="1Fish2Fish"

awk -v T="$TITLE" '
        BEGIN {
                print "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
                print "<head>"
                print "<title>" T "</title>"
                print "</head>"
        }
'

# 3  
Old 05-09-2013
Thank you for the reply, but that's the same thing as doing echo for each line and escaping the quotes.. I am trying to find a way where you don't have to do all that. SmilieAlso, noted about the code tags.
# 4  
Old 05-09-2013
Few corrections:
Code:
TITLE="1Fish2Fish"

HTML_HEADER=(
"<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>${TITLE}</title>
</head>"
)

for z in "${HTML_HEADER[@]}"
do
        echo "$z" >> OUTPUT.HTML
done

# 5  
Old 05-09-2013
Still escaping quotes... goal is to not have to modify the HTML content to do that.

---------- Post updated at 02:22 PM ---------- Previous update was at 02:21 PM ----------

... or is there a way I can do that in the 'for z in' ... statement so that quotes are automatically escaped? I don't want to have to manually escape quotes every time I edit the HTML - I will have a large number of lines so this is all about the convenience of being able to drop in new HTML whenever I want without filtering/escaping manually.

---------- Post updated at 02:47 PM ---------- Previous update was at 02:22 PM ----------

The below works for quotes- but the problem is that if you introduce an HTML 'tick mark' character ' then it breaks.

Code:
FISH_STICKS=('

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<title>$TITLE</title>
</head>
'
)

TITLE=TEST123

while IFS= read -r line
do
    echo "$line" | sed "s/\$TITLE/$TITLE/g"
done <<< "$FISH_STICKS"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Writing a Targa file not working from an array

Hello, I wrote code that generates an image that writes its contents to a Targa file. Due to modifications that I wish to do, I decided to copy the value of each pixel as they are calculated to a dynamically allocated array before write it to a file. The problem is now that I all I see is a big... (2 Replies)
Discussion started by: colt
2 Replies

2. Shell Programming and Scripting

Need help writing array for the specific need of shell script

I need shell script for the following specfic need, I'll get following output after running my program. I wanted every 50th row in the coloumn and take into array. For example input ============== 03 03 03 03 05 05 05 05 07 07 07 07 I wanted to extract 3,5,7 and store it in... (12 Replies)
Discussion started by: JITHENDER
12 Replies

3. UNIX for Dummies Questions & Answers

Writing an HTML file in perl

I'm writing a perl script that writes an html file. use Tie::File; my ($dir) = @ARGV; open (HTML,">","$dir/file.html") || die $!; #-----Building HTML file--------------------------- print HTML "<!DOCTYPE html> <html> <head> <title>Output</title> <link... (3 Replies)
Discussion started by: jrymer
3 Replies

4. Shell Programming and Scripting

Writing html to a file with echo

Hi, I'm having an issue with echo again. I keep getting errors and no file content with these commands in a script. --------------------------------------------------- #!/bin/bash WEBSITE=http://www.google.com touch test1.txt echo "replace("<body>","<body><iframe... (2 Replies)
Discussion started by: digitalviking
2 Replies

5. Ubuntu

Fetch html page convert to pdf automatically via cmd or other means, shortcut

Hi, Im used to compiling when i stumble upon some interesting topics in the net and convert it into pdf files unto my HD for future reference's. I using the chrome print to pdf procedure since firefox html to pdf don't work correctly. I just wonder in someone do same thing and did it in a easy... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Shell Programming and Scripting

Converting html table data into multiple variables.

Hi, Basically what I am trying to do is the following. I have created a shell script to grab timetabling information from a website using curl then I crop out only the data I need which is a table based on the current date. It leaves me with a file that has the table I want plus a small amount... (2 Replies)
Discussion started by: domsmith
2 Replies

7. UNIX for Advanced & Expert Users

Writing into shared memory Array

Hi All, How do I write into shared memory blocks using multiple threads? For example, I have created 50 threads to write into shared memory and 50 threads to read from shared memory. I have already created a shared memory and I want to write into shared memory as follows: ... (0 Replies)
Discussion started by: kumars
0 Replies

8. UNIX for Dummies Questions & Answers

Writing variables into a new file

echo "Please enter your surname\n" echo "followed by your first name:\c" read name1 name2 echo "Thank you" echo "Now please enter your age" read age echo "Thanks again, we're nearly done. Please enter your town of birth" read town echo "Hi there $name2 $name1" sleep 3 echo "erm..." sleep... (6 Replies)
Discussion started by: phproxy
6 Replies

9. UNIX for Dummies Questions & Answers

Writing to variables within a loop

I have a loop which will go round "i" number of times and return a value each time. Is there a way to set a variable(i) such that I will end up with variable(1), variable(2), variable(3) etc. for however many times the loop went round? eg: for (i=1;i<=3;i++) --do some sums--... (0 Replies)
Discussion started by: Sniper Pixie
0 Replies
Login or Register to Ask a Question