Short way to escape HTML in Bash?

后端 未结 5 1862
庸人自扰
庸人自扰 2020-12-08 06:24

The box has no Ruby/Python/Perl etc.

Only bash, sed, and awk.

A way is to replace chars by map, but it becomes tedious

相关标签:
5条回答
  • 2020-12-08 07:04

    Pure bash, no external programs:

    function htmlEscape () {
        local s
        s=${1//&/&}
        s=${s//</&lt;}
        s=${s//>/&gt;}
        s=${s//'"'/&quot;}
        printf -- %s "$s"
    }
    

    Just simple string substitution.

    0 讨论(0)
  • 2020-12-08 07:11

    or use xmlstar Escape/Unescape special XML characters:

    $ echo '<abc&def>'| xml esc
    &lt;abc&amp;def&gt;
    
    0 讨论(0)
  • 2020-12-08 07:16

    You can use recode utility:

        echo 'He said: "Not sure that - 2<1"' | recode ascii..html
    

    Output:

        He said: &quot;Not sure that - 2&lt;1&quot;
    
    0 讨论(0)
  • 2020-12-08 07:26

    Escaping HTML really just involves replacing three characters: <, >, and &. For extra points, you can also replace " and '. So, it's not a long sed script:

    sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
    
    0 讨论(0)
  • 2020-12-08 07:26

    The previous sed replacement defaces valid output like

    &lt;
    

    into

    &amp;lt;
    

    Adding a negative loook-ahead so "&" is only changed into "&amp;" if that "&" isn't already followed by "amp;" fixes that:

    sed 's/&(?!amp;)/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
    
    0 讨论(0)
提交回复
热议问题