From 87e894e8ca4d76a9fb2746e68896be59395e78bb Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 9 Feb 2024 14:37:15 +0300 Subject: [PATCH] Add scripts to copy/move a file with recoded filename --- bin/cp_recode_fname | 40 ++++++++++++++++++++++++++++++++++++++++ bin/mv_recode_fname | 1 + 2 files changed, 41 insertions(+) create mode 100755 bin/cp_recode_fname create mode 120000 bin/mv_recode_fname diff --git a/bin/cp_recode_fname b/bin/cp_recode_fname new file mode 100755 index 0000000..8cae733 --- /dev/null +++ b/bin/cp_recode_fname @@ -0,0 +1,40 @@ +#! /bin/sh +set -e + +usage() { + echo "Usage: $0 [[from_enc] to_enc] filename" >&2 + exit 1 +} + +if [ $# -eq 1 ]; then + from_enc="`python3 -c \"from m_lib.defenc import default_encoding; print(default_encoding)"`" + if [ "$from_enc" != utf-8 ]; then + to_enc=utf-8 + else + usage + fi + filename="$1" +elif [ $# -eq 2 ]; then + from_enc="`python3 -c \"from m_lib.defenc import default_encoding; print(default_encoding)"`" + to_enc="$1" + filename="$2" +elif [ $# -eq 3 ]; then + from_enc="$1" + to_enc="$2" + filename="$3" +else + usage +fi + +cmd="`basename \"$0\"`" +case "$cmd" in + cp_*) cmd="cp -p" ;; + mv_*) cmd=mv ;; + *) + echo "Uncnown command $0, aborting" >&2 + exit 2 + ;; +esac + +filename_recoded=`echo "$filename" | iconv -f "$from_enc" -t "$to_enc"` +exec $cmd "$filename" "$filename_recoded" diff --git a/bin/mv_recode_fname b/bin/mv_recode_fname new file mode 120000 index 0000000..16052c7 --- /dev/null +++ b/bin/mv_recode_fname @@ -0,0 +1 @@ +cp_recode_fname \ No newline at end of file -- 2.39.5