Created
August 9, 2021 22:03
-
-
Save joshbooks/e6642a4d209b6c395d37b22839da0c7b to your computer and use it in GitHub Desktop.
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 | |
# script to collect input until the end then overwrite an existing file | |
# with the provided input. Intended to simplify this pattern: | |
# cat file | grep | awk | sed > temp | |
# rm file | |
# mv temp file | |
# into this pattern: | |
# cat file | grep | awk | sed | rewrite file | |
# need the file to overwrite | |
overwriteFile=$1 | |
if [[ -z "$overwriteFile" ]] | |
then | |
echo "must provide a file to rewrite" | |
exit 1 | |
fi | |
if [[ ! -f "$overwriteFile" ]] | |
then | |
echo "$overwriteFile does not exist, either there's a typo or you're using the wrong command" | |
exit 2 | |
fi | |
tmpFile=$(mktemp /tmp/rewrite-script.XXXXXX) | |
cat > $tmpFile | |
rm $overwriteFile | |
mv $tmpFile $overwriteFile | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment