【问题标题】:Shell script to replace a variable in a HTML document用于替换 HTML 文档中的变量的 Shell 脚本
【发布时间】:2011-12-09 21:25:00
【问题描述】:

我想从 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>
......

我想将 html 文档中标题标签之间的 $name 字符串替换为 bash 脚本中的 $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 文档连接起来?

【问题讨论】:

  • 我应该如何用 awk 做到这一点???
  • 不确定@RobW 是什么意思。 sed 's/1/2/g;s/a/b/g;s/txt/newTxt/g' 允许您更改任意数量的行。通常对于多个 sed cmd(比我上面的示例更多),人们将所有 sed 操作放入一个单独的文件中,每行 1 个 cmd,然后像 sed -f mySedFixer.sed myTest.html.template &gt; myTest.html 一样执行或者如果你在 Linux 中工作,看看是否你 sed 支持 -i 选项,它保存对命名文件的修改,而无需重定向到临时文件并重命名。祝你好运。
  • @shellter “仅在单行上工作”是指sed 逐行处理输入。

标签: html templates shell sed awk


【解决方案1】:

使用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】:

    请参阅下面的测试(使用 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】:

      vim:

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

      【讨论】:

        【解决方案4】:

        下面的sn-p可以用Github代替$name

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

        被替换的文件在replaced.html 中输出。如果要覆盖现有文件,请使用:

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

        【讨论】:

          【解决方案5】:

          我为此创建了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>
          

          就这么简单。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-10-21
            相关资源
            最近更新 更多