Unix for the Rest of Us
Pages: 1, 2
The foundation of the foundation
Underneath the BSD layer (and its flashing Terminal cursor) is the Mach kernel. We won't have much to do with the kernel, although everything that happens on the computer has to deal with it. The kernel is the traffic cop -- it keeps all the various processes running smoothly. The kernel task is the first task started at startup and the last ended at shutdown.
[localhost:~] peterf% sudo kmodstat
Password:
Id Refs Address Size Wired Name (Version) <Linked Against>
1 1 0x0 0x0 0x0 com.apple.kernel (1.3.3)
2 1 0x0 0x0 0x0 com.apple.kernel.bsd (1.0.3)
3 9 0x0 0x0 0x0 com.apple.kernel.iokit (1.0.3)
4 9 0x0 0x0 0x0 com.apple.kernel.libkern (1.0.3)
5 3 0x0 0x0 0x0 com.apple.kernel.mach (1.0.3)
The first five modules dynamically link to the kernel. Some of the others include those shown here.
You get the idea that these kernel modules are providing very fundamental services, without which we wouldn't have access to CD/DVDs, AirPort, FireWire drives, or USB. (I'm impatiently awaiting a module for my Wacom tablet!)
I hope to provide you with more information about how the BSD layer in Mac OS works in future articles. For those with Unix or OpenStep backgrounds, it will be familiar territory. In fact, there are plenty of you out there with more in-depth knowledge about BSD than I have. I hope you'll feel free to correct me and enhance my knowledge as I try to bring an appreciation of this brave (new) world to Mac users of all levels.
Location, location, location
Our first recipe is simple but very useful.
Open your Terminal app (it's in /Applications/Utilities/) and type
locate '/Fonts/'
If locate returns nothing, it's because the "locate DB" script hasn't been initialized yet. If this is the case -- you've recently installed OS X, and the regular cron scripts haven't yet run -- type the following and let the machine churn for a few minutes.
The ampersand at the end of the line puts the process in the background.
sudo /usr/libexec/locate.updatedb &
In the meantime, you can be reading the man page for the locate command by typing
man locate
When the updatedb script has run, try it again. Type
locate '/Fonts/'
You'll probably see a long list of file names flash past. Because the Terminal remembers quite a few lines, you can scroll back and see what's been located to match your request. If you'd like to see this one screen at a time, try
locate '/Fonts/' | less
which "pipes" the output of locate through the "text pager" called less (which, of course, is a program whose ancestor was called more as in "hit space for more").
The first page of the list will appear on the screen. Hit the spacebar for more. You can also hit Command-F for the next page (Forward), hit the down arrow to see the next line, hit Command-B for the previous page (Back), and so forth. The less command is good for a quick, read-only look at any text file on your system. Of course, there's more to the less command than this, but that's for another day.
For now, I'll leave you with a bit more on the locate function. Try this:
locate '/Sys*Fonts'
and then this
locate '/Sys*Fonts/'
and then this
locate '/Sys*Fonts*' | less
or maybe
locate '/Lib*Fonts*' | less
When you read the man page for locate, the synopsis looks like
SYNOPSIS
locate pattern
A pattern is any sequence of characters, with certain special behaviors accorded to the "globbing" characters, as the man page says:
"Shell globbing and quoting characters ("*", "?", "\", "[" and "]") may be used in pattern, although they will have to be escaped from the shell. Preceding any character with a backslash ("\") eliminates any special meaning which it may have. The matching differs in that no characters must be matched explicitly, including slashes ("/")."
As a special case, a pattern containing no globbing characters ("foo") is matched as though it were "*foo*".
Briefly, these are patterns as they are used in a regular expression to create a "regex" that can match a set of different strings. For instance, in our example, /Sys*Fonts* means match any file path name which begins with the characters "/Sys" then has any number of intervening characters followed by Fonts followed by any number of characters out to the end of the line. Books are available (from O'Reilly, of course) on regular expressions. For an very formal presentation, try man re_format and man grep. While shell file name globbing doesn't use exactly the same syntax (in that it's much more limited than the full grep parser), there is plenty of power in the use of a well-placed asterisk or a set of brackets.
I hope this little dip of the toes into the BSD shell gives you a feeling for the depth that awaits you in the next few columns.
Peter Fraterdeus has been webmastering with Linux since 1993 and became hooked on the Mac after purchasing a 128k model in October 1984.
Read more Mac Dev News Roundup columns.
Return to the Mac DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 8 of 8.
-
kmodstat?
2003-04-04 10:34:43 anonymous2 [Reply | View]
I have a Mac OS 10.2.4 and I'm trying to learn Unix with my Terminal, My question is why is it that when I use the commands such as "sudo kmodstat" I get the relpy command not found? I'm very aware of spacing and spelling, yet on alot of commands from my Terminal I get these replies? Help!
I just purchased "Learning the Unix Operating System
5th Ed" I hope to get started on the right path. -
kmodstat?
2003-04-04 12:31:41 peterf@mac.com [Reply | View]
Try
locate kmodstat
However, I don't have that command on my 10.2.4 system either.
"Deprecated: Mac OS X < 10.2.x"
(see http://www.hmug.org/man/8/kmodstat.html)
However, if you are not able to find other commands, be sure that the PATH environment variable includes the path to the command you are interested in
try
setenv
(to see existing env vars)
and
man setenv
(unfortunately, this dumps you into 'man tcsh' and you have to scroll or search to find the setenv description:
setenv [name [value]]
Without arguments, prints the names and values of
all environment variables. Given name, sets the
environment variable name to value or, without
value, to the null string.
so
setenv PATH /path/to/kmodstat:$PATH
Good Luck!
Peter
-------------
Peter Fraterdeus
www.semiotx.com
www.fraterdeus.com
Galena, Illinois
-
Command F and Command B (should use Control Key)
2001-07-11 08:30:53 plumcreek [Reply | View]
When moving forward and backward in 'less', you need to use Control F and Control B. Command F brings up the find dialog (Command B doesn't do anything).
I just thought that should be clarified.
-
File Globbing isn't RegEx - Clarification!
2001-06-21 14:05:46 peterf@mac.com [Reply | View]
A note from an astute reader points out that although the * is used in both "file globbing" and regular expressions, the syntax used for each is actually quite different.
David Thompson writes:
----------------------
Hi Peter,
You are incorrect in your recent Mac article where you
compared shell globbing and regular expressions. You
seemed to imply that globbing was a subset of regular
expression patterns, and this is not true at all.
For example, consider how the asterisk is used in
globbing: it is a wild card for zero or more
characters at the current position. In regular
expressions, an asterisk has an entirely different
meaning: zero or more occurences of the previous
character at the current position.
(Actually, in regular expressions, the asterisk means
zero or more occurences of the previous regular expression, so that [abc]* means any combination of the 3 letters a,b, and c.)
-----------------
Of course, this is perfectly true. My intended meaning was that the building of symbolic expressions is deep and wide!
Thanks!
Peter
-
Bridging the Gap
2001-06-16 09:33:47 johnafriedman [Reply | View]
This article is not only insightful and interesting, it encourages me to dig in and expand my knowledge. Interesting things happen at boundaries, hence the need for a column that focuses on the interface between the Mac neo-cortex and the evolved foundation of the Unix brain.






Small detail to report...., root needs to be enabled in OSX before the "sudo /usr/libexec/locate.updatedb &" will work. Your assumption was probably that like you, many keep root enabled on standalone OSX Macs. I'm Terminal newbie, but tried your tip anyway. It worked fine, and I'm happy that you provided the nudge.
-HiTerm-