Skip to content

Instantly share code, notes, and snippets.

@e3krisztian
Created February 14, 2014 10:36
Show Gist options
  • Save e3krisztian/8999020 to your computer and use it in GitHub Desktop.
Save e3krisztian/8999020 to your computer and use it in GitHub Desktop.
reimplementation of moreutils' sponge
#!/bin/bash
# reimplementation of moreutil's sponge
# moreutil conflicts with GNU parallel on both Ubuntu and Fedora
# http://manned.org/sponge/c673547d
PATH=/bin:/usr/bin
outputfile="$1"
tempfile="$(mktemp)"
[ -f "$tempfile" ] || exit 1
cat > "$tempfile"
if [ -n "$outputfile" ] ; then
# -> output to file specified
[ -f "$outputfile" ] && {
chmod --reference="$outputfile" "$tempfile"
}
mv "$tempfile" "$outputfile"
else
# -> stdout
cat "$tempfile"
rm -f "$tempfile"
fi
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment