пакетное конвертирование кодировки используя iconv

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# Copyright (c) Alexander Ulyanitsky <asux@yandex.ru>
# License: GNU GPL v2
from="CP1251"
to="UTF-8"
recursive=0
pattern="*"
verbose=0
pretend=0
function function_convert () {
if [[ ${verbose} == 1 ]]; then
echo -e "converting \e[01;32m$1\e[m"
fi
[[ ${pretend} == 0 ]] && iconv --from ${from} --to ${to} --output $1.tmp $1 && rm $1 && mv $1.tmp $1
return 0
}
function usage() {
echo -e "$0 [options] [file1] [file2] ...
where options are:
-f from from encoding
-t to to encoding
-r recursive
-p pattern pattern for find
-P pretend, do not convart really
-h show this help
-v be verbose"
}
OPTERR=0
while getopts 'f:t:rp:Phv' opt; do
case ${opt} in
f) from=${OPTARG};;
t) to=${OPTARG};;
r) recursive=1;;
p) pattern=${OPTARG};;
P) pretend=1;;
v) verbose=1;;
h) usage; exit 0;;
:) echo "Option '-$OPTARG' requires an argument." >&2; exit 1;;
\?) echo "Invalid option: '-$OPTARG'" >&2; exit 1;;
esac
done
[[ ${verbose} == 1 ]] && echo -e " \e[01;32m*\e[m from \e[01;38m$from\e[m
\e[01;32m*\e[m to \e[01;38m$to\e[m
\e[01;32m*\e[m recursive \e[01;38m$recursive\e[m
\e[01;32m*\e[m pattern \e[01;38m$pattern\e[m"
shift $(( ${OPTIND} - 1 ))
files="$*"
if [[ -z "${files}" ]]; then
files="${PWD}"
fi
[[ ${verbose} == 1 ]] && echo -e " \e[01;32m*\e[m for convert \e[01;38m${files}\e[m"
total=0
for file in "${files}"; do
if [[ -d "${file}" ]]; then
if [[ ${recursive} == 1 ]]; then
for item in $(find "${file}" -name "${pattern}" -type f); do
function_convert ${item} && total=$(( ${total} + 1 ))
done
else
[[ ${verbose} == 1 ]] && echo -e "\e[01;33m${file}\e[m is a directory, use '-r' option for recursive"
fi
else
if [[ -f "${file}" ]]; then
function_convert "${file}" && total=$(( ${total} + 1 ))
fi
fi
done
[[ ${verbose} == 1 ]] && echo -e "\e[00;02mTotal\e[m: \e[01;33m${total}\e[m"