Last active
January 27, 2021 13:54
-
-
Save sportebois/db81d74a47fefd78aef38ac65d2c076f to your computer and use it in GitHub Desktop.
Remove BOM from UTF8 files
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
#!/usr/bin/env bash | |
set -eu | |
# find-bom searches for nonbinary files starting with a BOM marker | |
# Path of BOM-encoded files is print to stdout | |
# Exit status is 0 if no BOM files has been found | |
# Exit status is 1 if at least one BOM file has been found | |
readonly TARGET_ROOT=${1:-$PWD} | |
echo "Searching for BOM in $TARGET_ROOT" | |
bomFiles=$(grep -rlI $'\xEF\xBB\xBF' "$TARGET_ROOT" --exclude-dir={.tox,.git,.venv,.hypothesis,.mypy_cache} || echo "") | |
if [ -z "$bomFiles" ]; then | |
exit 0 | |
fi | |
for f in $bomFiles; do | |
echo "$f" | |
done | |
exit 1 |
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
#!/usr/bin/env bash | |
set -eu | |
# fix-bom search for BOM marker in non-binary files and removes them | |
# You can either call the script as-is, to run it in the current working folder, and all its children recursively | |
# Or you can pass a target folder as the command line argument | |
# | |
# Sample use | |
# ./fix-bom.sh | |
# ./fix-bom.sh ./only/this/specific/tree/ | |
readonly TARGET_ROOT=${1:-$PWD} | |
echo "Searching for BOM in $TARGET_ROOT" | |
bomFiles=$(grep -rlI $'\xEF\xBB\xBF' "$TARGET_ROOT" --exclude-dir={.tox,.git,.venv,.hypothesis,.mypy_cache} || echo "") | |
for f in $bomFiles; do | |
echo "Removing BOM from $f" | |
sed -i '1s/^\xEF\xBB\xBF//' "$f" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment