6

I want to generate a static website from a shell script.

A example for the shell script code is:

author="Github INC."
name="Github"
description="social coding"
text=$(awk '{ print }' main.html)

The main.html could look like this:

<!DOCTYPE html> 
<html> 
<head> 
    <title>$name</title> 
</head> 
<body>
......

I want to replace the $name string in the html document between the title tag with the $name string in the bash script (in this example Github) so in this example it should look like this:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Github</title> 
</head> 
<body>
......

I could do this by changing the shell script Code to this:

author="Github INC."
name="Github"
description="social coding"
text="$( sed "s/<title>.*<\/title>/<title>$name<\/title>" main.html )"

But if I use more then one string in the html document, then it won't work anymore...

For example:

<!DOCTYPE html> 
<html> 
<head> 
    <title>$name</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <meta name="robots" content="index, follow" /> 
    <meta name="author" content="$author" /> 
    <meta name="description" content="$description" /> 
    <link rel="shortcut icon" href="favicon.png" /> 
    <link rel="stylesheet" type="text/css" href="style.css" /> 
</head> 
<body> 

Any ideas how to connect the strings from the Shell Script with the HTML Document?

3
  • How should I do this with awk??? Commented Dec 9, 2011 at 21:33
  • not sure what @RobW means. sed 's/1/2/g;s/a/b/g;s/txt/newTxt/g' allows you to change as many lines as you want. Typically for more than a couple of sed cmds (more than my example above), people put all of the sed actions into a seperate file, 1 cmd per line, and then execute like sed -f mySedFixer.sed myTest.html.template > myTest.html Or if your workin in Linux, see if you sed supports the -i option, which saves modifications to the named file(s) without need to redirect into a temporary file, and rename. Good luck. Commented Dec 9, 2011 at 21:55
  • @shellter By "works on a single line only", I was referring to the fact that sed processes input line by line. Commented Dec 9, 2011 at 22:01

5 Answers 5

5

Using awk

[jaypal:~/Temp] cat html.sh 
#!/bin/bash
author="Github INC."
name="Github"
description="social coding"
awk '{sub(/\$name/,name);sub(/\$author/,author);sub(/\$description/,description);}1' name="$name" author="$author" description="$description" inputfile

Using sed

[jaypal:~/Temp] cat html1.sh 
#!/bin/bash
author="Github INC."
name="Github"
description="social coding"
sed -e '/\$name/s//"$name"/' -e '/\$description/s//"$description"/' -e '/\$author/s//"$author"/' inputfile
Sign up to request clarification or add additional context in comments.

Comments

2

see the test (with awk) below: actually sed should work as well.

kent$  cat main.html 
<!DOCTYPE html> 
<html> 
<head> 
    <title>$name</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <meta name="robots" content="index, follow" /> 
    <meta name="author" content="$author" /> 
    <meta name="description" content="$description" /> 
    <link rel="shortcut icon" href="favicon.png" /> 
    <link rel="stylesheet" type="text/css" href="style.css" /> 
</head> 
<body>


kent$  cat doIt.sh 
#!/bin/bash
author="Github INC."
name="Github"
description="social coding"
awk -vauthor="$author" -vname="$name" -vdesc="$description" '{gsub(/\$name/,name);gsub(/\$author/,author);gsub(/\$description/,desc)}1' main.html

kent$  ./doIt.sh  
<!DOCTYPE html> 
<html> 
<head> 
    <title>Github</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <meta name="robots" content="index, follow" /> 
    <meta name="author" content="Github INC." /> 
    <meta name="description" content="social coding" /> 
    <link rel="shortcut icon" href="favicon.png" /> 
    <link rel="stylesheet" type="text/css" href="style.css" /> 
</head> 
<body>

Comments

1

vim:

:%s/\v"\zs\$\w+\ze"/\={'$author':'Github INC.', '$name':'Github', '$description':'social coding'}[submatch(0)]/g

Comments

0

I created shtpl for that purpose. (A very young project of mine, but may you want to give it a try, because i think it solves exactly your needs)

To replace in your last example all placeholders, you can simply execute:

$ name=testname author=testauthor description=mydescription sh -c "$( shtpl example.html.tpl )"

The result would be:

<!DOCTYPE html> 
<html> 
<head> 
    <title>testname</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <meta name="robots" content="index, follow" /> 
    <meta name="author" content="testauthor" /> 
    <meta name="description" content="mydescription" /> 
    <link rel="shortcut icon" href="favicon.png" /> 
    <link rel="stylesheet" type="text/css" href="style.css" /> 
</head> 
<body>

It's that easy.

Comments

-1

The following snippet can be used to replace $name with Github:

# Example: Replace $name in main.html with Github, output in replaced.html
title=Github
awk '{ gsub("\$name","'$title'")}; print $0 }' main.html > replaced.html

The replaced file is output at replaced.html. If you want to overwrite the existing file, use:

awk '{ gsub("\$name","'$title'")}; print $0 }' main.html > replaced.html &&
     mv replaced.html test.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.