Created
February 14, 2014 10:36
-
-
Save e3krisztian/8999020 to your computer and use it in GitHub Desktop.
reimplementation of moreutils' sponge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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