Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Update code files in /usr/share/misc UNIX
As previously mentioned on this site, there are many useful text files in /usr -> share -> misc, containing such useful information as zip codes, airport codes, area codes and more. Problem is, the versions shipped with OS X are very out-of-date. Here's a little shell script that will update them.

#!/bin/sh

# update_codes.sh
#
# very simple script to update airport, phone and zip codes kept
# in /usr/share/misc from source data at the openbsd site.  

base_url='http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/share/misc'
code_list='airport inter.phone na.phone zipcodes'

for code_type in $code_list; do
   echo "downloading $code_type..."
   src="$base_url/$code_type"
   dest="/usr/share/misc/$code_type"
   mv $dest ${dest}.bak
   curl $src > $dest
done
As previously mentioned here, you may use the very cool Code Finder utility to view and search these files.

[robg adds: This script works as described, though you'll need to remember to make it executable (chmod 755 update_codes) and run it as root (sudo update_codes). Replace update_codes with whatever you named the script.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[7,033 views]  

Update code files in /usr/share/misc | 28 comments | Create New Account
Click here to return to the 'Update code files in /usr/share/misc' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Update code files in /usr/share/misc
Authored by: tandemrepeat on Aug 08, '03 11:17:03AM

great hint - hadn't even looked in the directory...thanks!



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: rgbrock1 on Aug 08, '03 11:32:42AM

Don't forget that if you are using the default shell, that before running the update_codes command you need to enter the command as follows: sudo sh update_codes

---
Richard G. Brock
rgbrock1@mac.com



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: notmatt on Aug 08, '03 12:42:46PM

That sounds odd to me - that's what the #!/bin/sh is supposed to take care of, and similarly-constructed scripts have worked for me forever without changing from the default shell.



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: bdm on Aug 08, '03 08:39:42PM

Quite so, Richard is mistaken. In fact, your default shell is not used even if there is no #! construction at the front. In that case the Bourne shell /bin/sh is used (which these days is often bash masquerading as sh). This is a convention that goes back to the earliest days of Unix.

Brendan.



[ Reply to This | # ]
even updated files are out of date
Authored by: Anonymous on Aug 08, '03 12:31:16PM

Just FYI:

Even the updated code files are way out of date, though. For example, I live in Fayetteville, Arkansas USA. Our area code is still listed as 501 even though it changed to 479 over a year ago. Also, our Northwest Regional Airport, which is almost five years old isn't listed in the airport code list (it's XNA, btw).



[ Reply to This | # ]
even updated files are out of date
Authored by: satcomer on Aug 08, '03 02:32:36PM

I concur that even the update files are a couple years out of date.



[ Reply to This | # ]
even updated files are out of date
Authored by: greenplasticcup on Aug 08, '03 07:29:30PM

Ha! 3 Months ago, I just moved away from Siloam Springs just west of you.

Indeed, a newer codes resource should be available.



[ Reply to This | # ]
even updated files are out of date
Authored by: jbravo on Aug 11, '03 11:04:53AM

These files are kept "current" by the developers of the OpenBSD operating system. Being an open source project I imagine that they're primarily just volunteers who probably spend most of their efforts keeping the operating system in good order. My guess is that updates are made to these reference files only as someone volunteers the effort, and from looking at the version history updates are made to only specific parts of the file. So while the file may have been updated a month ago, it doesn't mean that all of the info is up-to-date as of a month ago.

If you find an error or omission, you can, 1. edit your local file, and/or 2. get involved with OpenBSD and submit your edits via CVS for the community's benefit. Keeping these files accurate is a group effort, the larger the group, the more accurate the files will be.



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: harttj on Aug 08, '03 07:14:39PM

Hi!

Pure newbie here where it comes to UNIX. That does not mean that I won't test the waters, but I just would not go in without a lifeguard, so to speak. Rob mentions using sudo and some other terminology, but I just don't know where it goes.

If possible, could someone please write out the complete script so I could type it out or copy it to my terminal?

Many thanks in advance.


T.J. Hart



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: bluehz on Aug 08, '03 07:21:15PM
1. Copy the code above and paste into a text document. Save it as text only if using TextEdit. For our example, lets say you save the text file to your desktop as "update_codes". 2. Open the terminal and enter (hit return or enter after each command):
cd ~/desktop
chmod 755 update_codes
sudo ./update_codes
Thats it! The first command (cd) changes to your desktop directory. The 2nd command (chmod) makes the file executable, and the 3rd command (sudo) actually executes the command. You will be asked for your password.

[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: tstewart777 on Aug 08, '03 07:26:10PM

You can also copy the text from the hint and create it in the terminal in emacs:

emacs update_codes

(paste in text from the hint)

ctrl-x ctrl-s to save
ctrl-x ctrl-c to exit

That's it, then do the chmod to make it executable. Emacs is easy to deal with for stuff like this, IMHO, the learning curve is slight compared to vi.

Tim



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: GaelicWizard on Aug 09, '03 05:25:06AM

emacs, vi, ... they're all hugely massive and incredibly incomprehensable to a newbie, i'd recommend pico if you're just beginning. once you've got the terminal basics down, then you can tackel emacs or vi or whatever.

---
Pell



[ Reply to This | # ]
The SUID Bit
Authored by: jyncroft on Aug 09, '03 03:23:07AM

Alternatively, you could set the owner of the shell script to root and set the uid (SUID) bit. Then you could run the script w/o having to type sudo (the SUID bit causes the script to run with the rights of the owner).

A note of caution here: setting the uid bit on a script could compromise your system. Be sure you know what the script does (everything it does) before doing so. Think twice before setting the SUID bit for scripts (owned by root) that take arguments at the command line. Since you never know what parameters a malicious user may pass to your script. Since the script would run as root it could do great damage if misused.

So, here's what you'd do:

% sudo chown root update_codes.sh
% sudo chmod 4755 update_codes.sh

or
% sudo chmod u+s update_codes.sh

Now when you want to run the script, just type the name of the script (if it's in your path) or ./update_codes.sh when you're in the same directory.

Jennifer



[ Reply to This | # ]
The SUID Bit
Authored by: GaelicWizard on Aug 09, '03 05:26:19AM

I'm not sure, but I don't believe the SUID bit works on shell scripts...

---
Pell



[ Reply to This | # ]
The SUID Bit
Authored by: jyncroft on Aug 09, '03 11:29:18AM

It does... try it. I have a few scripts set up this way, works great

Jennifer



[ Reply to This | # ]
The SUID Bit
Authored by: Crawdad on Dec 11, '03 03:48:49PM
Think twice before setting the SUID bit for scripts (owned by root) that take arguments at the command line. Since you never know what parameters a malicious user may pass to your script. Since the script would run as root it could do great damage if misused.
Not enough. The invoker could set an environment variable which causes the script to be parsed differently by the shell. Take $IFS for example, normally containing SP TAB NEWLINE. Add a well-chosen letter to that and the script does something the author never dreamed of. Setuid shell scripts are Bad Juju. If you think you must have one, write a setuid C wrapper that cleans the environment, then does setreuid() and runs the script.

[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: merlyn on Aug 10, '03 04:46:35PM
It'd be easier to do this by rsync...
sudo rsync -avz rsync://mirror.cs.wisc.edu/openbsd/src/share/misc/ /usr/share/misc/

This pulls in some extra stuff, but it's good stuff.

[ Reply to This | # ]

Update code files in /usr/share/misc
Authored by: MaxMouseroom on Aug 10, '03 06:17:16PM

I'm the author of the update script, and just wanted to say that rsync is really cool! If I had known about it, I would have probably skipped writting this script. Thanks for sharing.

However, I will say that when I used rsync as you specified, I got a bunch of junk I really don't want.

Anyway, thanks, seems like there is always more to learn with Unix.



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: MaxMouseroom on Aug 10, '03 06:22:55PM

I also noticed that when using rsync, the owner and group codes got all pucked up. Of course that is easy enough to fix.

For Unix newbies:

sudo chown -R root:wheel *



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: easco on Aug 10, '03 09:05:29PM

Don't use that "sudo chown" command in the previous posting unless you have changed your current directory to the /usr/share/misc directory first.

Otherwise you will change the ownership of all the files in whatever directory happens to be your current directory and all of it's subdirectories!!!!



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: MaxMouseroom on Aug 11, '03 02:27:05AM

Good safety tip, Egon! My bad! Doh!



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: _merlin on Aug 10, '03 06:40:41PM

The files in /usr/share/misc might be more useful if they weren't so USA-centric. There is a world out there...



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: MaxMouseroom on Aug 11, '03 02:43:17AM
I was bored and feeling really geeky, so I found a web site with more complete (and International) airport codes, and then wrote a one-liner to convert the web page into the correct format for /usr/share/misc/airport. In case anyone cares, here it is. I wrote it to all go on one (long) line.
sudo curl -s http://www.mapping.com/airportcodes.html | \
awk '/A00/,/9Z9/' | perl -ne '$/=";"; s#  ?##g; s#\n##g; \
s#.*$##i; s###g; s#^\s+##; s#^\s*$##; \
s#\s+#:#; print "$_\n"' > /usr/share/misc/airport

Of course you might want to backup your old file first, etc.

Warning: this file is over 21K lines, compared to 1.9K for the file at the BSD site, and only 153 for the original OS X version of the file. It causes Code Finder to start a little slower.

[robg adds: I edited this comment to restore the missing backslashes...]

[ Reply to This | # ]

Update code files in /usr/share/misc
Authored by: MaxMouseroom on Aug 11, '03 03:37:23PM

Some of the regular expressions I used in the one-liner above got hozed in the translation when I posted this. If anyone wants the working version, I'll have to email it to them. Reach me here: sswell at yahoo.com



[ Reply to This | # ]
Should be OK now...
Authored by: robg on Aug 11, '03 04:28:39PM

I edited the comment to replace the missing characters (Geeklog killed them).

-rob.



[ Reply to This | # ]
Update code files in /usr/share/misc
Authored by: MaxMouseroom on Aug 12, '03 04:31:08PM
Sorry, the code was still garbled by Geeklog. Let me try it again. Put this all on one line:

sudo curl -s http://www.mapping.com/airportcodes.html | 
awk '/A00/,/9Z9/' | perl -ne '$/=";"; s#  ?##g; s#\n##g; 
s#<.><..>.*$##i; s#<.*?>##g; s#^\s+##; s#^\s*$##; s#\s+#:#;
print "$_\n"' > /usr/share/misc/airport
[\code]


[ Reply to This | # ]
repair permissions
Authored by: LouieNet on Aug 22, '03 04:10:30AM

You should run Disk Utility to repair your file permissions after updating the files with any of the methods previously mentioned.

---
G4 cube, 1152M RAM, OS X Server 10.2.6



[ Reply to This | # ]
curl on multiple files
Authored by: ilovja on Aug 29, '03 08:05:39AM

Just for your information. Curl can do multiple files.

In ~/temp;

curl -O -O -O -O http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/share/misc/{airport,inter.phone,na.phone,zipcodes}

The command above should be entered in one line. After that, you could;

sudo cp * /usr/share/misc

Curl also supports brackets such as 'ftp://ftp.letters.com/file[a-z].txt'. man curl for more info.



[ Reply to This | # ]