Skip to content

Instantly share code, notes, and snippets.

@mprentice
Created August 10, 2012 21:11
Show Gist options
  • Save mprentice/3318015 to your computer and use it in GitHub Desktop.
Save mprentice/3318015 to your computer and use it in GitHub Desktop.
Fix license text from xsl:comment to !--
#!/bin/bash
# Replace erroneous <xsl:comment> license tags with xml <!--> comments
bak="$HOME/tmp/xslbak"
mkdir -p "$bak"
find /usr/local/tusk/tusk-4_0_0 -name '*.xsl' -print0 | while IFS='' read -r -d '' fold ; do
lic=`awk '/<xsl:comment>/,/<\\/xsl:comment>/' "$fold" | grep 'licenses/ecl1\.php'`
if [ "" != "$lic" ] ; then
fdir="${fold%/*}"
fdir="${fdir#/usr/local/tusk/tusk-4_0_0/}"
fn="${fold##*/}"
fnew="$bak/$fdir/$fn"
mkdir -p "$bak/$fdir"
cp "$fold" "$fnew"
cat > "$fold" <<EOF
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2012 Tufts University
Licensed under the Educational Community License, Version 1.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.opensource.org/licenses/ecl1.php
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
EOF
awk '/<xsl:stylesheet/,/<\/xsl:stylesheet>/' "$fnew" >> "$fold"
fi
done
@charles-dyfis-net
Copy link

Notes:

  • Generally speaking, use of all-caps variable non-environment names is discouraged (/msg greybot !varcap on freenode)
  • basename and dirname should be replaced with parameter expansion expressions (/msg greybot basename and /msg greybot dirname, as appropriate)
  • Re-opening the NEW file every time you want to append a program's output to it is silly. Instead, you can open it once and reuse the file descriptor:
    exec 4>"$NEW"; one_command >&4; another_command >&4
  • Piping multiple awk statements together is somewhat silly -- awk is almost as powerful a language as perl; you could have a single awk statement doing the work of all of them, and sed, and so on, but the #awk channel, not the #bash one, is appropriate for that.

@charles-dyfis-net
Copy link

Change:

find /usr/local/tusk/tusk-4_0_0 -name '*.xsl' | while read -r f ; do

to

find /usr/local/tusk/tusk-4_0_0 -name '*.xsl' -print0 | while IFS='' read -r -d '' f ; do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment