Skip to content

Instantly share code, notes, and snippets.

@borestad
Last active October 13, 2022 09:07
Show Gist options
  • Save borestad/957083cf6a30fb64a09b218d0b22bce2 to your computer and use it in GitHub Desktop.
Save borestad/957083cf6a30fb64a09b218d0b22bce2 to your computer and use it in GitHub Desktop.
sponge - soak up standard input and write to a file (poor mans version)
#!/usr/bin/env bash
set -euo pipefail
#
# sponge - soak up standard input and write to a file
#
# NOTE: Poor mans versions of sponge
# ... to avoid having to 'apt-get install moreutils' and perl dependencies) in CI pipeline
# Example Usage:
# cat file.txt | grep -v "foobar" | sponge file.txt
target="${1}"
tempfile="/tmp/sponge-$RANDOM-$RANDOM-$RANDOM-$RANDOM-$RANDOM"
cat > $tempfile
chmod --reference="$target" $tempfile
mv -f $tempfile "$target"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment