我想从shell脚本生成一个静态网站。
shell脚本代码的示例是:
author="Github INC."
name="Github"
description="social coding"
text=$(awk '{ print }' main.html)
main.html可能如下所示:
<!DOCTYPE html>
<html>
<head>
<title>$name</title>
</head>
<body>
......
我想用bash脚本中的$ name字符串替换html文档中的$ name字符串(在本例中为Github),所以在这个例子中应该看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>Github</title>
</head>
<body>
......
我可以通过将shell脚本代码更改为:
来完成此操作author="Github INC."
name="Github"
description="social coding"
text="$( sed "s/<title>.*<\/title>/<title>$name<\/title>" main.html )"
但是如果我在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>
如何将Shell脚本中的字符串与HTML文档连接起来?
答案 0 :(得分:2)
使用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
答案 1 :(得分:0)
以下代码段可用于将$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
替换的文件在replacement.html输出。如果要覆盖现有文件,请使用:
awk '{ gsub("\$name","'$title'")}; print $0 }' main.html > replaced.html &&
mv replaced.html test.html
答案 2 :(得分: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>
答案 3 :(得分:0)
:%s/\v"\zs\$\w+\ze"/\={'$author':'Github INC.', '$name':'Github', '$description':'social coding'}[submatch(0)]/g
答案 4 :(得分: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>
就这么简单。