-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.sh
executable file
·83 lines (76 loc) · 2.6 KB
/
search.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
83
#!/bin/bash
lastModifiedDir=${LASTMODIFIED_DIR:-"/geoip"}
countryDir=${COUNTRY_DIR:-"${lastModifiedDir}/country"}
subDir=${SUB_DIR:-"${lastModifiedDir}/sub"}
optstring=":c:s:nh"
numberOnly="false"
usage() {
echo "Usage: $(basename $0) [-c -s -n]"
echo " -c A space seperated array of country_code terms to search for in GeoLite2 database."
echo " e.g. -c \"US New-Zealand France\""
echo " -s A space seperated array of sub_code terms to search for in GeoLite2 database."
echo " e.g. -n \"VN-43 West-Virginia:Dallas Berlin\""
echo " -n Script will only display the number of matches for each value, default behaviour lists every matching location."
echo " -h Show usage."
exit 1
}
while getopts ${optstring} arg; do
case "${arg}" in
c) countryCodes=(${OPTARG}) ;;
s) subCodes=(${OPTARG}) ;;
n) echo "Displaying counts only:"
numberOnly="true" ;;
h) usage ;;
:) echo "Error: Missing option argument for -${OPTARG}."
usage ;;
?) echo "Error: Invalid option: - ${OPTARG}."
usage ;;
esac
done
if [[ -z "$countryCodes" && -z "$subCodes" ]]; then
echo "Error: No country_code or sub_code terms provided."
usage
return 2> /dev/null; exit
fi
if ! [ -z "$countryCodes" ]; then
for code in "${countryCodes[@]}"; do
if [ $numberOnly == "false" ]; then
echo ""
fi
placeName=$( grep -hwiF "$code" ${countryDir}/countryList.txt | cut -d, -f2-3 )
if [ -z "${placeName}" ]; then
echo "Country_code "$code" didn't match any entries in the GeoLite2 Country database"
else
placeNameCount=$( echo "${placeName[@]}" | wc -l )
if [ $placeNameCount -eq 1 ]; then
echo "Country_code "$code" matched $placeNameCount country"
else
echo "Country_code "$code" matched $placeNameCount countries"
fi
if [ $numberOnly == "false" ]; then
echo "${placeName[@]}"
fi
fi
done
fi
if ! [ -z "$subCodes" ]; then
for code in "${subCodes[@]}"; do
if [ $numberOnly == "false" ]; then
echo "";
fi
placeName=$( grep -hwiF "$code" ${subDir}/subList.txt | cut -d, -f2-5 | cut -d: -f1 )
if [ -z "${placeName}" ]; then
echo "Sub_code "$code" didn't match any entries in the GeoLite2 City database."
else
placeNameCount=$( echo "${placeName[@]}" | wc -l )
if [ $placeNameCount -eq 1 ]; then
echo "Sub_code "$code" matched $placeNameCount location"
else
echo "Sub_code "$code" matched $placeNameCount locations"
fi
if [ $numberOnly == "false" ]; then
echo "${placeName[@]}"
fi
fi
done
fi