我的 bash 无法编辑我的文件

my bash can't edit my file

我的bash文件应该打开一个html文件,删除第12行,然后插入这行字符串 $f,以便通过温度传感器的实际值更改数组。但是当我执行我的工作台时,没有任何内容被编辑。

temp.sh

#!/bin/bash
f="myArray_a = ["
for ((i=0 ; 12 - $i ; i++))
  do
  x=$(cat /sys/bus/w1/devices/28-0000075292ed/w1_slave | grep "t=" | awk -F "t=" '{print /1000}')
 if [ $i -eq 11 ]
 then
 x=$(printf %.3f] $x)
 f="$f $x"

 sed '12 d' index.html
 sed -i -e '12i'$f' ''\' index.html

 else
 x=$(printf %.3f, $x)
 f="$f $x"
 fi



 done | column 

index.html

<!DOCTYPE HTML>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <META HTTP-EQUIV="Refresh" CONTENT="10">
  <title> Temperature values</title>
               <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  <style type="text/css">
${demo.css}
              </style>
        <script type="text/javascript">
myArray_a
myArray_a
myArray_a
myArray_a
            var myArray_a = [1, 2, 3, 4, 0, 5, 0, 0, 0, 0, 0, 0, 1];
$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    
                }
            },
            title: {
                text: 'Live sensor  data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'sensor data',
                data: (function () {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;

                    for (i = 0; i <= 12; i += 1) {
                        data.push({
                            x: time + i * 1000,
                            y: myArray_a[i]
                        });
                    }
                    return data;
                }())
            }]
        });
    });
});
  </script>
 </head>
 <body>
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

 </body>
</html>

您可以将两个 sed 命令替换为:

sed -i "12c \
$f 
" index.html

c命令用于"change"(相当于先删除再插入)。

我没有在此处包含 </code> 变量,因为它似乎不是要注入的字符串的一部分。</p> <p><strong>更新:</strong> </p> <p>请注意,如果您想替换现有的 <code>array_a ,您最好不要依赖行号(array_a 在您的示例 html 中不在第 12 行)。

你最好匹配要替换的确切字符串:

sed -i "s/^[ \t]*var myArray_a[^;]*;/$f/" index.html