Created
August 16, 2012 15:47
-
-
Save rsvp/3371208 to your computer and use it in GitHub Desktop.
telcountry.sh : look-up country / telephone code. | Linux bash self-contained database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# bash 4.1.5(1) Linux Ubuntu 10.04 Date : 2012-08-11 | |
# | |
# _______________| telcountry : look-up country / telephone code. | |
# | |
# Usage: telcountry [term] | |
# ^telephone code, | |
# or case-sensitive partial country name. | |
# | |
# Examples: % telcountry United | |
# 1; United States | |
# 44; United Kingdom | |
# 971; United Arab Emirates UAE | |
# | |
# % telcountry 97 | |
# 970; Palestine | |
# 971; United Arab Emirates UAE | |
# 972; Israel | |
# 973; Bahrain | |
# 974; Qatar | |
# 975; Bhutan | |
# 976; Mongolia | |
# 977; Nepal | |
# | |
# Dependencies: awk | |
# CHANGE LOG get LATEST version from https://bitbucket.org/rsvp/gists/src | |
# | |
# 2012-08-10 First version. Telephone data massaged from: | |
# http://en.wikipedia.org/wiki/List_of_country_calling_codes | |
# | |
# CCITT, the predecessor of the ITU Telecommunication Standardization Sector (ITU-T), | |
# developed the first formal list of telephone country codes. That list was included | |
# in the 1964 CCITT Blue Book, which would eventually become ITU-T recommendation | |
# E.164, also called IDD (International Direct Dialling) or | |
# ISD (International Subscriber Dialling) codes. | |
# See http://en.wikipedia.org/wiki/E.164 | |
# _____ Prelims | |
set -u | |
# ^ unbound (i.e. unassigned) variables shall be errors. | |
# Example of default assignment: arg1=${1:-'foo'} | |
set -e | |
# ^ error checking :: Highly Recommended (caveat: you can't check $? later). | |
# | |
# _______________ :: BEGIN Script :::::::::::::::::::::::::::::::::::::::: | |
term=${*:-'NULL'} | |
# ^designed for using this script in a "while" loop. | |
outf='/dev/shm/88_countrytel-out.tmp' | |
tmpf='/dev/shm/88_countrytel.tmp' | |
# Write to TMP FILE data on 283 entries where: | |
# Zone 1 – North American Numbering Plan Area | |
# Zone 2 – Mostly Africa | |
# Zone 3 – Europe | |
# Zone 4 – Europe | |
# Zone 5 – Mostly Latin America | |
# Zone 6 – Southeast Asia and Oceania | |
# Zone 7 – Eurasia (former Soviet Union) | |
# Zone 8 – East Asia and special services | |
# Zone 9 – Central, South and Western Asia; Middle East | |
# Unicode characters have been converted to ASCII equivalent. | |
cat - > $tmpf <<EOHereDoc | |
0@NULL improper argument | |
1@United States USA | |
1@Canada | |
1242@Bahamas | |
1246@Barbados | |
1264@Anguilla | |
1268@Antigua | |
1268@Barbuda | |
1268@Barbuda | |
1284@British Virgin Islands | |
1340@US Virgin Islands | |
1345@Cayman Islands | |
1441@Bermuda | |
1473@Grenada | |
1649@Turks and Caicos Islands | |
1664@Montserrat | |
1670@Northern Mariana Islands | |
1671@Guam | |
1684@American Samoa | |
1721@Sint Maarten | |
1758@Saint Lucia | |
1767@Dominica | |
1784@Saint Vincent and Grenadines | |
1787@Puerto Rico | |
1808@Midway Island | |
1808@Wake Island USA | |
1809@Dominican Republic | |
1829@Dominican Republic | |
1849@Dominican Republic | |
1868@Trinidad and Tobago | |
1869@Nevis | |
1869@Saint Kitts and Nevis | |
1876@Jamaica | |
1939@Puerto Rico | |
20@Egypt | |
211@South Sudan | |
212@Morocco | |
213@Algeria | |
216@Tunisia | |
218@Libya | |
220@Gambia | |
221@Senegal | |
222@Mauritania | |
223@Mali | |
224@Guinea | |
225@Cote d'Ivoire | |
226@Burkina Faso | |
227@Niger | |
228@Togo | |
229@Benin | |
230@Mauritius | |
231@Liberia | |
232@Sierra Leone | |
233@Ghana | |
234@Nigeria | |
235@Chad | |
236@Central African Republic | |
237@Cameroon | |
238@Cape Verde | |
239@Sao Tome and Principe | |
240@Equatorial Guinea | |
241@Gabon | |
242@Congo | |
243@Congo | |
244@Angola | |
245@Guinea-Bissau | |
246@British Indian Ocean Territory | |
246@Diego Garcia | |
247@Ascension | |
248@Seychelles | |
249@Sudan | |
250@Rwanda | |
251@Ethiopia | |
252@Somalia | |
253@Djibouti | |
254@Kenya | |
255@Tanzania | |
255@Zanzibar | |
256@Uganda | |
257@Burundi | |
258@Mozambique | |
260@Zambia | |
261@Madagascar | |
262@Mayotte | |
262@Reunion | |
263@Zimbabwe | |
264@Namibia | |
265@Malawi | |
266@Lesotho | |
267@Botswana | |
268@Swaziland | |
269@Comoros | |
27@South Africa | |
290@Saint Helena | |
290@Tristan da Cunha | |
291@Eritrea | |
297@Aruba | |
298@Faroe Islands | |
299@Greenland | |
30@Greece | |
31@Netherlands | |
32@Belgium | |
33@France | |
34@Spain | |
350@Gibraltar | |
351@Portugal | |
352@Luxembourg | |
353@Ireland | |
354@Iceland | |
355@Albania | |
356@Malta | |
357@Cyprus | |
358@Aland Islands | |
358@Finland | |
359@Bulgaria | |
36@Hungary | |
370@Lithuania | |
371@Latvia | |
372@Estonia | |
373@Moldova | |
374@Armenia | |
375@Belarus | |
376@Andorra | |
377@Monaco | |
378@San Marino | |
379@Vatican | |
380@Ukraine | |
381@Serbia | |
382@Montenegro | |
385@Croatia | |
386@Slovenia | |
387@Bosnia and Herzegovina | |
389@Macedonia | |
39066@Vatican Holy See | |
39@Italy | |
40@Romania | |
41@Switzerland | |
420@Czech Republic | |
421@Slovakia | |
423@Liechtenstein | |
43@Austria | |
44@Guernsey | |
44@Isle of Man | |
44@Jersey | |
44@United Kingdom UK | |
45@Denmark | |
46@Sweden | |
47@Norway | |
47@Svalbard and Jan Mayen Islands | |
48@Poland | |
49@Germany | |
500@Falkland Islands | |
500@South Georgia and South Sandwich Islands | |
501@Belize | |
502@Guatemala | |
503@El Salvador | |
504@Honduras | |
505@Nicaragua | |
506@Costa Rica | |
507@Panama | |
508@Saint Pierre and Miquelon | |
509@Haiti | |
51@Peru | |
52@Mexico | |
5399@Guantanamo Bay Cuba | |
53@Cuba | |
54@Argentina | |
55@Brazil | |
56@Chile | |
56@Easter Island | |
57@Colombia | |
58@Venezuela | |
590@Guadeloupe | |
590@Saint Barthelemy | |
590@Saint Martin FR | |
591@Bolivia | |
592@Guyana | |
593@Ecuador | |
594@French Guiana | |
595@Paraguay | |
596@French Antilles | |
596@Martinique | |
597@Suriname | |
598@Uruguay | |
5993@Caribbean Netherlands | |
5993@Sint Eustatius | |
5994@Caribbean Netherlands | |
5994@Saba | |
5997@Bonaire | |
5997@Caribbean Netherlands | |
5999@Curacao | |
60@Malaysia | |
61@Australia | |
61@Christmas Island | |
61@Cocos Keeling Islands | |
62@Indonesia | |
63@Philippines | |
64@Chatham Island New Zealand | |
64@New Zealand | |
64@Pitcairn Islands | |
65@Singapore | |
66@Thailand | |
670@East Timor | |
672@Australian territories | |
672@Norfolk Island | |
673@Brunei Darussalam | |
674@Nauru | |
675@Papua New Guinea | |
676@Tonga | |
677@Solomon Islands | |
678@Vanuatu | |
679@Fiji | |
680@Palau | |
681@Wallis and Futuna | |
682@Cook Islands | |
683@Niue | |
685@Samoa | |
686@Kiribati | |
687@New Caledonia | |
688@Tuvalu | |
689@French Polynesia | |
690@Tokelau | |
691@Micronesia | |
692@Marshall Islands | |
7@Russia | |
76@Kazakhstan | |
77@Kazakhstan | |
800@International Freephone | |
808@International Shared Cost ISCS | |
81@Japan | |
82@South Korea | |
84@Vietnam | |
850@North Korea | |
852@Hong Kong | |
853@Macao | |
855@Cambodia | |
856@Laos | |
86@China | |
870@Inmarsat Satellite SNAC | |
878@UPT Universal Personal Telecommunications | |
880@Bangladesh | |
8810@ICO Global Satellite | |
8811@ICO Global Satellite | |
8812@Ellipso Satellite | |
8813@Ellipso Satellite | |
8816@Iridium Satellite | |
8817@Iridium Satellite | |
8818@Globalstar Satellite | |
8819@Globalstar Satellite | |
881@GMSS Global Mobile Satellite | |
88213@EMSAT Satellite | |
88216@Thuraya Satellite | |
886@Taiwan | |
90@Turkey | |
91@India | |
92@Pakistan | |
93@Afghanistan | |
94@Sri Lanka | |
95@Burma | |
960@Maldives | |
961@Lebanon | |
962@Jordan | |
963@Syria | |
964@Iraq | |
965@Kuwait | |
966@Saudi Arabia | |
967@Yemen | |
968@Oman | |
970@Palestine | |
971@United Arab Emirates UAE | |
972@Israel | |
973@Bahrain | |
974@Qatar | |
975@Bhutan | |
976@Mongolia | |
977@Nepal | |
98@Iran | |
992@Tajikistan | |
993@Turkmenistan | |
994@Azerbaijan | |
99534@South Ossetia | |
995@Georgia | |
996@Kyrgyzstan | |
998@Uzbekistan | |
EOHereDoc | |
# Type of first argument term determines appropriate action; | |
# To TEST whether term is an INTEGER, we could use: | |
# [[ $term =~ ^-?[0-9]+$ ]] | |
# but double brackets is a bashism, | |
# and =~ is supported only for bash 3.1 onwards. | |
# | |
# awk matches, then PRINTS the appropriate FIELDS. | |
# -F@ specifies field separator as @ in the phone data above. | |
# | |
printsep='"; "' | |
# Be assured: semi-colon will not appear within country field, | |
# which is useful to know for post-processing. | |
# | |
if [ "${term//[0-9]*}" = "" ]; then | |
# yes, natural number -- so print country name: | |
awk -F@ "\$1 ~ /^$term/ {print \$1 $printsep \$2}" $tmpf > $outf | |
else | |
# no, has non-numerical characters -- so print telephone code: | |
awk -F@ "\$2 ~ /$term/ {print \$1 $printsep \$2}" $tmpf > $outf | |
fi | |
# display OUTPUT if non-zero size, otherwise generate error message: | |
# remember to clean-up... | |
if [ -s $outf ] ; then | |
cat $outf | |
rm -f $tmpf $outf | |
else | |
echo " !! ${0##*/}: NULL response; check arguments and data." 1>&2 | |
rm -f $tmpf $outf | |
exit 113 | |
fi | |
exit 0 | |
# _______________ EOS :: END of Script :::::::::::::::::::::::::::::::::::::::: | |
# StackOverflow: Test whether bash string is valid as an integer: | |
# http://stackoverflow.com/questions/2210349/ | |
# vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=sh : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment