Skip to content

Instantly share code, notes, and snippets.

@jstrassburg
Created April 23, 2014 20:17
Show Gist options
  • Save jstrassburg/11230788 to your computer and use it in GitHub Desktop.
Save jstrassburg/11230788 to your computer and use it in GitHub Desktop.
The worst code I ever wrote
# This was part of a larger bash script.
# I decided to use sed to replace a password in an XML file after prompting the person installing.
# Since it was XML I had to first use sed to encode for XML.
# Then, since I was using sed I had to encode for sed.
# There is some shell encoding there too because of the backticks.
# A lot of damn back slashes.
# encode for XML
# & => &amp; < => &lt; > => &gt; " => &quot; ' => &apos;
ENCODED_SQL_PASSWORD=`echo $SQL_PASSWORD | sed -e "s/&/\&amp;/g" -e "s/</\&lt;/g" -e "s/>/\&gt;/g" -e "s/\"/\&quot;/g" -e "s/'/\&apos;/g"`
# now encode for sed
# \ => \\ " => \" & => \& / => \/
ENCODED_SQL_PASSWORD=`echo $ENCODED_SQL_PASSWORD | sed -e "s/\\\\\/\\\\\\\\\\\\\/g" -e "s/\"/\\\\\\\\\"/g" -e "s/&/\\\\\&/g" -e "s_/_\\\\\/_g"`
echo "Using $ENCODED_SQL_PASSWORD"
sed -i "s/{PutPasswordHere}/${ENCODED_SQL_PASSWORD}/g" $MY_XML_CONFIG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment