代码之家  ›  专栏  ›  技术社区  ›  Itai Ganot

如何将特殊字符回显到文件中?

  •  0
  • Itai Ganot  · 技术社区  · 7 年前

    我有一个shell脚本,可以做各种事情。

    其中,它检查/etc/rc中是否存在某一行。本地,如果该行不存在,我想将其添加到文件中。

    grep reboot "/etc/rc.local" || echo -e "sed -i 's/\^reboot//g' $script" >> /etc/rc.local
    

    问题是我无法在文件中插入特殊字符“^”。

    因此,如果我只运行该行,而不重定向到/etc/rc。本地:

    # grep reboot  "/etc/rc.local" || echo -e 'sed -i "s/\^reboot//g" $script' >> /etc/rc.local
    sed -i 's/reboot//g' /root/1.sh
    

    您可以看到,echo'ed行遗漏了“^”。

    我试过使用和不使用echo的“-e”开关,试过使用双引号/单引号。。。什么都不管用——我似乎无法回应这个特殊的字符。

    我做错了什么?

    2 回复  |  直到 7 年前
        1
  •  1
  •   iamauser    7 年前
    $ cat inputfile
    some reboot
    reboot
    some reboot other
    

    删除只有 reboot 没有别的了。

    $ sed '/^reboot$/d' inputfile
    some reboot
    some reboot other
    

    使用 -i 将其就地删除并覆盖 inputfile .

        2
  •  1
  •   Jens    7 年前

    要删除开头有“reboot”的行,请使用

    grep -v '^reboot' /etc/rc.local > result