Skip to content

Instantly share code, notes, and snippets.

@pfrozi
Last active February 12, 2021 21:51
Show Gist options
  • Save pfrozi/f11529786c85eb70f24a7c67c63512e5 to your computer and use it in GitHub Desktop.
Save pfrozi/f11529786c85eb70f24a7c67c63512e5 to your computer and use it in GitHub Desktop.
Change the charset of a file
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f] -i INPUT_FILE -e OUTPUT_CHARSET -o OUTPUT_FILE
Given an INPUT_FILE, create an OUTPUT_FILE with charset OUTPUT_CHARSET.
Example: $(basename "${BASH_SOURCE[0]}") -i test.sql -e utf-8 -o out.sql
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-s, --silent Silent mode
-i, --input Input file
-e, --output-chs Charset used in output file
-o, --output Output file
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
if [ $silent == 0 ]; then
echo >&2 -e "${1-}"
fi
}
step(){
msg "${BLUE}${1-}...${NOFORMAT}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "${RED}$msg"
exit "$code"
}
parse_params() {
# default values of variables set from params
input=''
output_chs=''
output=''
silent=0
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-color) NO_COLOR=1 ;;
-s | --silent) silent=1 ;;
-i | --input) input=${2-} && shift;;
-e | --output-chs) output_chs=${2-} && shift;;
-o | --output) output=${2-} && shift;;
-?*) die "Unknown option: ${1-}" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${input}" ]] && die "Missing required parameter: input"
[[ -z "${output_chs}" ]] && die "Missing required parameter: output-chs"
[[ -z "${output}" ]] && die "Missing required parameter: output"
return 0
}
setup_colors
parse_params "$@"
# script logic here
if [ -f "$input" ]; then
msg "${BLUE}$input exists."
else
die "$input does not exist."
fi
step "Reading parameters"
msg "- input: ${input}"
msg "- output-chs: ${output_chs}"
msg "- output: ${output}"
step "Checking input charset"
file_info=`file -i ${input}`
charset_in=''
if [[ $file_info =~ .+charset=(.+) ]]; then
charset_in=${BASH_REMATCH[1]}
msg "charset=${charset_in}"
else
die "Cannot define charset of ${input}. Result ${file_info}"
fi
step "Creating output file"
if [ $charset_in == $output_chs ]; then
msg "The charset of input is equal to charset of output."
exit 0
fi
iconv -f $charset_in -t $output_chs "$input" -o "$output" && msg "Done!" || die "Bad use of iconv."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment