Skip to content

Instantly share code, notes, and snippets.

@n-st
Created September 13, 2017 17:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save n-st/048facd0c12f105ac122030fb58b962f to your computer and use it in GitHub Desktop.
Converts umlauts (äöüß) to "Code page 437" encoding for PXELINUX boot menus
#!/usr/bin/env bash
## fix-umlauts.sh
# Converts umlauts (äöüß) in files to "Code page 437" encoding, so they will
# display correctly in PXELINUX boot menus.
# Expects one or more file paths as arguments, e.g.:
# $ ./fix-umlaut.sh "pxelinux.cfg/menus/main.menu" "pxelinux.cfg/menus/admin.submenu"
# The files are overwritten in place (using a temp file).
# -------
# These variables contain the umlauts A O U a o u sz in the respective encoding:
plain=(A O U a o u sz)
utf8=($'\xc3\x84' $'\xc3\x96' $'\xc3\x9c' $'\xc3\xa4' $'\xc3\xb6' $'\xc3\xbc' $'\xc3\x9f')
#utf8=$'\xc3\x84\xc3\x96\xc3\x9c\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f'
latin1=($'\xc4' $'\xd6' $'\xdc' $'\xe4' $'\xf6' $'\xfc' $'\xdf')
#latin1=$'\xc4\xd6\xdc\xe4\xf6\xfc\xdf'
cp437=($'\x83' $'\x99' $'\x9a' $'\x84' $'\x94' $'\x81' $'\xe1')
#cp437=$'\x83\x99\x9a\x84\x94\x81\xe1'
sed_cmd=""
for (( i=0; i<${#plain[@]}; i++ ))
do
# Replace existing UTF-8 and Latin-1 in one go:
from1="${utf8[i]}"
from2="${latin1[i]}"
to="${cp437[i]}"
sed_cmd="${sed_cmd}s/${from1}/${to}/g;s/${from2}/${to}/g;"
done
printf 'I know %d different characters that I will replace.\n' $((i))
cleanup() {
if [ -n "$tmpfile" ]
then
rm -f "$tmpfile"
fi
}
trap cleanup EXIT INT TERM
tmpfile=$(mktemp)
for file in "$@"
do
if [ -f "$file" ]
then
if sed "$sed_cmd" < "$file" > "$tmpfile" && cat "$tmpfile" > "$file"
then
printf '"%s" OK\n' "$file"
else
printf '"%s" ERROR\n' "$file" 1>&2
fi
else
printf '"%s" does not exist or is not a file\n' "$file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment