<<

. 5
( 69 .)



>>




9
UNIX FOR MAC

SHELL COMMAND CONCEPTS
First, the shell interprets the command line you have typed

S
hell commands in Unix are often cryptic sets of letters
and breaks it down into its constituent parts. Second, the
strung together, apparently at random. To the
shell uses those parts to determine whether a built-in
uninitiated, they can appear arbitrary, but there are
command or an executable program should be run, and
some general concepts that hold them together.
then passes the appropriate parameters to the command or
executable program.
Shell commands execute as soon as you type them and
press the Return key. The execution consists of two steps:

ANATOMY OF A SHELL COMMAND

Each shell command is composed of specific parts, including grep -i -C2 --line-number "Mac OS X"
the command name, options, and arguments. These parts Documents/to-do.txt
tell the shell which command to execute, how that
This command illustrates all of the components that
command should be executed, and what other information
comprise a shell command, and illustrates how to use
on your computer it should use in the execution of the
multiword arguments by employing quotes.
command. You can better understand how Unix reads shell
commands by looking at the following example:

Simple Command Name Arguments
The shell breaks down your command into words After the options in the command line, you can add
separated by spaces, and then interprets the commands arguments in the form of filenames or text. The
one word at a time. The first word in the above shell command passes these arguments along to the
command is grep, and so, the shell knows to execute command that you are executing, and they give
the grep command. the command such information as what file or
information to look for, or where to store the results of
To save on typing, Unix commands have traditionally the command when it is done. In the example above,
been very short words. Some are not even the grep command has two arguments: "Mac OS X"
pronounceable words in English, such as the ls, cd, rm, and Documents/to-do.txt. The first argument to the
or pwd commands. The word grep can be pronounced, grep command, "Mac OS X", is a specific piece of text
but it seems to have no meaning in English. This is that you want to find in a file, and the second argument,
because it is actually an acronym for global regular Documents/to-do.txt, is the name of that file.
expression print. That may not help you much, but for
now, it is enough to say that the grep command lets Escaping Arguments
you search for text in a file.
The quotation marks around the three words "Mac OS
X" tell the shell to treat them as one argument rather
Options
than three. If the quotation marks are not used, the
You can type a number of options after the command
command line tells the grep command to look for the
name; these are strings starting with a hyphen or dash,
word Mac in files named OS, X, and Documents/to-
for example -i. You use these options to force Unix
do.txt. You can use quotation marks whenever you
commands to change their behavior. Each command has
need to include multiword arguments or filenames in
a unique set of options that you can use with it, and the
your Unix commands. Or, you can type a backslash (\)
meanings of these options vary from command to
before each space; this tells the shell not to treat the
command. For example, with grep the option -i means
following space as a word separator.
to match text without regard for letter case, while with
rm ” a command that deletes files ” the option -i tells
the shell to ask the user to confirm each file deletion.
Many options are single letters, but some are a single
letter and then a value, such as -C2, which stands for
the option -C and the numeric value 2. Longer options
are often set off with two hyphens, as in --line-number.


10
1
GET STARTED WITH UNIX



USING SHELL COMMANDS


Executable Files
Most commands that you enter on the command line are The shell locates the executable by searching through a
requests to run a program of some kind. The command set of locations on your hard drive called your path. The
may be a compiled program originally written in a path is a list of folders, also called directories. If you type
language such as C++ and now saved on disk as a binary the name of an executable within one of those folders
file. It may be a script written in Perl, or it may be a shell and press Return, you are running that file as a shell
script. To be run, a file must be executable. Executable application. By default, your path contains a reasonable
refers to both the contents and the attributes of a file. set of directories, but you can add other file locations.

Built-In Commands which Command
Some of the commands you type are built directly into You use the which command to determine whether a
the shell application and do not exist as separate files. command is built-in or is an executable file.
These functions include starting and stopping
commands, setting shell variables, repeating commands,
TYPE THIS:
and moving through the file structure.
[darwin:˜] user% which w
You can get a full list of the built-in shell commands by
typing the command builtins at the shell prompt. One
built-in command that is quite useful is the which
command. The which command tells you whether a
command is a built-in command or an executable file; RESULT:
if it is an executable file, the shell lists the location of
/usr/bin/w
that file.
You can see from this output that w is an
executable file stored in the location /usr/bin/w.



TYPE THIS:
[darwin:˜] user% which which




RESULT:
which: shell built-in command.

This output tells you that the which command is
built directly into the shell application.




11
UNIX FOR MAC




READ THE UNIX MANUAL
in several sections, and you can select from among them by

Y
ou can read the Unix manual pages by using the man
including the section number before the term. After the
command. These pages give you a basic summary of
name of the command, a synopsis of the syntax for the
how to use a specific Unix command as well as
command appears. Parameters in brackets are optional, but
detailed information on the types of parameters you can
if you use them, you must replace the underlined words
use with that command.
with an appropriate value or filename when you type the
command.
The simplest way to get information about a given
command is to type man commandname. If there is such a
A manual page also includes a description of the command
command and if there is a manual page for that command,
and often provides examples of how you can use it. Related
the shell displays the manual page. Because most manual
commands also appear along with section numbers, and
pages are long, only the first screen of information displays
any files that relate to the command, such as configuration
in your Terminal window; to continue on to the next screen,
files, are shown. Extra bits of trivia, such as the history of
you can press the Spacebar. You can continue pressing the
the command or known bugs, may also appear.
Spacebar for each screen of information until you have read
the entire manual page.

A manual page first lists the name of the command, and in
parentheses, the section number of the manual where you
can find that command. Some commands or terms appear


READ THE UNIX MANUAL




– The shell displays the first – This area displays the
¤ Type the name of a
TYPE THE MAN COMMAND
command and press Return. screen of the manual page. Command syntax summary.
„ At the Terminal window
– This area displays the – This area displays the
prompt, type man and a
space. name and section. Description of the command.
‹ Press the Spacebar.
12
1
GET STARTED WITH UNIX


You can become familiar with the man
It is quite common for Unix computers to ship without a
command by trying some of the commands.
printed manual explaining the Unix shell commands,
and your Mac OS X computer is no exception. Decades
Example:
of Unix users have relied upon the man command for
[darwin:˜] user% man man
instructions on the proper use of Unix commands. As
[darwin:˜] user% man apropos
heir to their legacy, you may sometimes find yourself
[darwin:˜] user% man whatis
consulting the manual pages to decipher obscure error
[darwin:˜] user% man manpath
messages. Therefore, you should be familiar with how to
[darwin:˜] user% man tcshb
use the man command.
Some of the resulting man pages can be
You will find that only reading a manual page is not
very opaque; so do not worry about
enough; you also need to understand what it is trying to
anything you cannot immediately
tell you. This can be difficult, as most manual pages
understand. For example, the man page
seem complex at first glance, having been written for a
for tcsh is extremely long.
very technical audience of Unix system administrators.
The key to understanding manual pages is to read
enough of them so that you begin to absorb the
concepts through familiarity. Use keyword searches and
look up related commands and terms that you do not
understand. Reading manual pages requires practice,
but it is worth the effort.




– The second screen of – You have now read the – The shell lists commands
SEARCH THE MANUAL
BY KEYWORD
information appears. entire manual page for that related to the keyword.
ˇ Type man -k and a space.
term.
› Press the Spacebar and ‡ Press the Spacebar to read
Á Type a keyword and press
read each screen until you the list until you see the shell
see the shell prompt. prompt.
Return.

13
UNIX FOR MAC




EXIT THE TERMINAL WINDOW
hen you are done typing Unix shell commands, you For this reason, it is best to use the exit command to end

W can exit your shell and close the Terminal window, a shell in a Terminal window. The exit command ends the
or exit the Terminal application. To do this, you can shell, but leaves the Terminal open. After you quit the shell,
simply click the red Close button in the upper left-hand you can close the window manually by clicking the red
corner of each Terminal window. Alternatively, you can Close button in the upper left-hand corner of the Terminal
choose Close Window from the Window menu or Quit window. You can change this behavior in the Window
Terminal from the Terminal menu. However, these are not settings for Terminal by setting the windows to close
necessarily the best ways to shut down a shell window. automatically when the shell ends.

This is because Unix shell commands can often continue to If you close a Terminal window without exiting the shell,
run in a Terminal window even if they do not appear on the you probably do not do any serious harm if no shell
screen, or seem to be doing nothing. When you close the commands are running. The Terminal application tries to
window, you also terminate the shell application that prevent problems by warning you if you close a window
started those commands, and as a result, you often that contains a running command. You can choose to
terminate the commands as well. continue closing the window, or cancel the window closure.

EXIT THE TERMINAL WINDOW



<<

. 5
( 69 .)



>>