0

I want to send the XML (HTML) content I get from a file to a substitution function so I can fix the wrong HTML entities (and overwrite the file). But I am not able to do it

This is the function based another answer I found

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

I tried

cat $TEMPFILEPATH | htmlEscape > $TEMPFILEPATH 
tee $TEMPFILEPATH <<< htmlEscape "$(cat $TEMPFILEPATH)"

But those do not work. I am really really really new with bash scripts. Does anyone can help me?

distante
  • 6,438
  • 6
  • 48
  • 90

2 Answers2

1

You wrote your function to accept an argument:

htmlEscape "$(cat $TEMPFILEPATH)"

You can move the cat command inside the function, though:

htmlEscape () {
    local s=$(cat "$1")
    s=${1//&/&amp;}
    s=${s//</&lt;}
    s=${s//>/&gt;}
    s=${s//'"'/&quot;}
    echo "$s"
}

htmlEscape "$TEMPFILEPATH"
chepner
  • 497,756
  • 71
  • 530
  • 681
1

I would create a sed script 'htmlEscape.sed'

s/&/&amp;/g
s/</&lt;/g
s/>/&gt;/g
s/"/&quot;/g

which you can then use by piping

cat $TEMPFILEPATH | sed -f htmlEscape.sed

or with the appropriate sed

sed -i -f htmlEscape.sed $TEMPFILEPATH
daniu
  • 14,137
  • 4
  • 32
  • 53