Update ffn

This commit is contained in:
cckuan 2021-01-12 21:54:58 +08:00 committed by GitHub
parent 0ccf4b95b8
commit c8cd9efb94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@
# * while given '-d' , do not rename file, only print new # * while given '-d' , do not rename file, only print new
# file name # file name
# Dependency: # Dependency:
# * nil # * err.sh
# Author: # Author:
# * Chang, Chu-Kuan <cckuan@changchukuan.name> # * Chang, Chu-Kuan <cckuan@changchukuan.name>
@ -15,11 +15,26 @@
# Characters which will be replaced by '_', other [:punct:] not presented here # Characters which will be replaced by '_', other [:punct:] not presented here
# will be deleted # will be deleted
readonly sep='[]._-[:blank:]' readonly sep='[]()._-[:blank:]'
. err.sh
help_msg()
{
cat << EOF
Usage:
${progname} [-hd] file... - format filename
Options:
-d Perform a 'dry run'. Do not actually rename file.
-h Show this message and exit.
EOF
}
process_file() process_file()
{ {
file="$(readlink -f "$1")" file="$1"
dir="$(dirname "${file}")" dir="$(dirname "${file}")"
base="$(basename "${file}")" base="$(basename "${file}")"
@ -37,10 +52,10 @@ process_file()
# process file name # process file name
new_base="$(printf '%s' "${base%.*}" \ new_base="$(printf '%s' "${base%.*}" \
| iconv --to-code=utf-8 \ | iconv --to-code=utf-8 \
| tr "${sep}" '[:space:]' \ | tr "${sep}" ' ' \
| tr -d '[:punct:]' \ | tr -d '[:punct:]' \
| tr '[:upper:]' '[:lower:]' \ | tr '[:upper:]' '[:lower:]' \
| tr -s '[:space:]' '_' \ | tr -s ' ' '_' \
| sed 's/^_//; s/_$//' | sed 's/^_//; s/_$//'
).${extension}" ).${extension}"
;; ;;
@ -48,10 +63,10 @@ process_file()
*) *)
new_base="$(printf '%s' "${base}" \ new_base="$(printf '%s' "${base}" \
| iconv --to-code=utf-8 \ | iconv --to-code=utf-8 \
| tr "${sep}" '[:space:]' \ | tr "${sep}" ' ' \
| tr -d '[:punct:]' \ | tr -d '[:punct:]' \
| tr '[:upper:]' '[:lower:]' \ | tr '[:upper:]' '[:lower:]' \
| tr -s '[:space:]' '_' \ | tr -s ' ' '_' \
| sed 's/^_//; s/_$//' | sed 's/^_//; s/_$//'
)" )"
;; ;;
@ -64,7 +79,7 @@ process_file()
return 0 return 0
fi fi
case "${dry_run}" in case "${d_flag}" in
'1') '1')
# While given '-d', do not rename file, only # While given '-d', do not rename file, only
# print new file name # print new file name
@ -79,7 +94,7 @@ process_file()
} }
readonly progname="$(basename "$0")" readonly progname="$(basename "$0")"
dry_run='0' d_flag='0'
total_file_cnt='0' total_file_cnt='0'
success_file_cnt='0' success_file_cnt='0'
clean_file_cnt='0' clean_file_cnt='0'
@ -87,12 +102,15 @@ clean_file_cnt='0'
while getopts 'dh' opt; do while getopts 'dh' opt; do
case "${opt}" in case "${opt}" in
'd') 'd')
readonly dry_run='1' readonly d_flag='1'
;; ;;
'h') 'h')
printf 'Usage:\t./%s [-hd] <path/to/file_name>...\n' "${progname}" help_msg
exit 0 exit 0
;; ;;
*)
err '1' 'invalid option\n'
;;
esac esac
done done
@ -107,4 +125,3 @@ done
printf '\n%s: renamed %d, clean %d, total %d/%d\n' "${progname}" \ printf '\n%s: renamed %d, clean %d, total %d/%d\n' "${progname}" \
"${success_file_cnt}" "${clean_file_cnt}" \ "${success_file_cnt}" "${clean_file_cnt}" \
"$((success_file_cnt + clean_file_cnt))" "${total_file_cnt}" "$((success_file_cnt + clean_file_cnt))" "${total_file_cnt}"