Skip to content

Instantly share code, notes, and snippets.

@pix0r
Created August 21, 2013 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pix0r/6299935 to your computer and use it in GitHub Desktop.
Save pix0r/6299935 to your computer and use it in GitHub Desktop.
in-place cleanjson
#!/bin/sh
if [ "$1" = "" ]; then
echo "Usage: $0 <file.json> [file2.json...]"
exit -1
fi
CLEANJSON=`which cleanjson`
if [ "$CLEANJSON" = "" ]; then
echo "Please install cleanjson and ensure it's on your PATH"
echo "E.g. npm install -g cleanjson"
exit -1
fi
TEMPDIR=`mktemp -d -t cleanjson`
if [ ! -d $TEMPDIR ]; then
echo "Error creating temp directory"
exit -1
fi
for file in $*; do
if [ ! -f $file ]; then
echo "File not found: $file"
rm -rf $TEMPDIR
exit -1
fi
dir=`dirname $file`
mkdir -p $TEMPDIR/$dir
cat $file | $CLEANJSON > $TEMPDIR/$file
if [ $? -ne 0 ]; then
echo "Error cleaning $file"
rm -rf $TEMPDIR
exit -1
fi
mv $TEMPDIR/$file $file
if [ $? -ne 0 ]; then
echo "Error replacing $file"
rm -rf $TEMPDIR
exit -1
fi
done
rm -rf $TEMPDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment