-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.sh
executable file
·82 lines (69 loc) · 1.8 KB
/
convert.sh
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
79
80
81
82
#!/bin/bash
# TODO: allow for passing in directory to save to
print_help() {
echo "Usage: $0 [-o output] [-f input_encoding] [-h] <input_file>"
echo "Convert a file to UTF-8 encoding"
echo
echo "Options:"
echo " -o output Specify the output file name (without extension)"
echo " -f, --from encoding Specify the input file encoding (skips auto-detection)"
echo " -h, --help Display this help message"
exit 1
}
output_name=""
input_encoding=""
while [[ $# -gt 0 ]]; do
case $1 in
-o)
output_name="$2"
shift 2
;;
-f | --from)
input_encoding="$2"
shift 2
;;
-h | --help)
print_help
;;
*)
break
;;
esac
done
# Shift the options so that $1 is the input file
shift $((OPTIND - 1))
# Check if a file is provided as an argument
if [ $# -eq 0 ]; then
print_help
fi
input_file="$1"
if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found."
exit 1
fi
if [ -z "$input_encoding" ]; then
if ! command -v uchardet &>/dev/null; then
echo "Error: uchardet is not installed. Please install it or use the -f flag to specify the input encoding."
exit 1
fi
detected_encoding=$(uchardet "$input_file")
if [ $? -ne 0 ]; then
echo "Error: Unable to detect encoding."
exit 1
fi
else
detected_encoding="$input_encoding"
fi
file_extension="${input_file##*.}"
if [ -n "$output_name" ]; then
output_file="${output_name}.${file_extension}"
else
output_file="${input_file%.*}_utf8.${file_extension}"
fi
iconv -f "$detected_encoding" -t UTF-8 "$input_file" >"$output_file"
if [ $? -eq 0 ]; then
echo "Conversion successful. Output file: $output_file"
else
echo "Error: Conversion failed."
exit 1
fi