Last active
October 13, 2022 09:07
-
-
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)
This file contains hidden or 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
#!/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