Shell script replace variable in HTML document

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

Sample shell script code:

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

The main.html file may look like this:

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

I want to replace the $ name line in the html document between the title tag with the $ name line in the bash script (in this Github example), 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 code of the script 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 than one line in an html document, then this will not work anymore ...

For instance:

<!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 on how to associate lines from a shell script with an HTML document?

+5
source share
5

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

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
+2

$name 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

replace.html. , :

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

. ( awk) : sed .

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>
0

:

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

shtpl . ( , , , , , )

, :

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

:

<!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>

.

0

All Articles