Skip to content

Instantly share code, notes, and snippets.

@joshbooks
Created August 9, 2021 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbooks/e6642a4d209b6c395d37b22839da0c7b to your computer and use it in GitHub Desktop.
Save joshbooks/e6642a4d209b6c395d37b22839da0c7b to your computer and use it in GitHub Desktop.
#!/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