-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcharsetdet.sh
50 lines (46 loc) · 1.07 KB
/
charsetdet.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
#!/bin/bash
# install the python packages
pip install chardet >/dev/null
# check flags
inplace=0
while test $# -gt 0;
do
case "$1" in
-h|--help)
echo "detect the python files recursively inside the directory"
echo ""
echo "charsetdet [-i] directory"
echo ""
echo "options:"
echo "-h, --help show help message"
echo "-i, convert to utf-8 with no nom inplace"
echo ""
shift
;;
-i)
inplace=1
shift
;;
*)
break
;;
esac
done
# set the file directory
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
# for each the files inside the directory
for file in $(find $1 -type f -name "*.py")
do
result=`chardet3 $file`
if [[ $result =~ .+ascii|.+utf-8 ]]; then
echo -e "\e[0;32m$result"
else
if $inplace -gt 0; then
iconv -t $file
fi
echo -e "\e[0;31m$result"
fi
done