Created
January 15, 2025 08:36
-
-
Save dmikushin/4d1119c8182662a48e30da90aec56816 to your computer and use it in GitHub Desktop.
Fix source file character encoding
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
#!/bin/bash | |
set -e | |
set -x | |
# Check if file name is provided | |
if [ -z "$1" ] | |
then | |
echo "No file name provided" | |
exit 1 | |
fi | |
# Determine the file encoding | |
encoding=$(file -bi "$1" | sed -e 's/.*[ ]charset=//') | |
if [ "$encoding" = "unknown-8bit" ] | |
then | |
encoding="ISO-8859-1" | |
fi | |
# Convert the file to UTF-8 | |
if [ "$encoding" != "utf-8" ] | |
then | |
iconv -f "$encoding" -t utf-8 "$1" > "$1".utf8 | |
mv "$1".utf8 "$1" | |
echo "File converted to UTF-8" | |
else | |
echo "File is already UTF-8" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment