Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)

Using a Command

To use commands, you type them into the shell at the prompt. The shell executes the command line and displays output (if any), and then gives you another shell prompt. When the shell prompt comes up again, even if there's no other output, your shell is ready to accept another command.

Many command lines (but not all) produce output before returning a new shell prompt. It is quite common in Unix for a command to produce no visible output if it is successful (if it fails, a command should always produce output). In Unix, silence implies success.

To run a command:

The parts of a command line

The parts of a command line are separated by spaces. Basic command lines have up to four kinds of components :

Figure 2.5 shows the different parts of a typical command line (you'll recognize this as the command from Figure 1.4 in Chapter 1, "What Is Unix, and Why Is It Good?" that searched for all instances of the word success in a particular directory).

Figure 2.5. The parts of a command line are separated by spaces, and basic command lines have up to four kinds of items in them. This shows the separate parts of the command line.

Each command may have multiple options and multiple arguments.

About the "command" part of the command line

When you enter a command line, the shell assumes that the first item on the line is a command.

There are two types of commands: those that are built into the shell you are using and those that are separate files somewhere on your disk. For example, if you run the cd command from the bash shell, you will execute the separate program /usr/bin/cd ; the tcsh shell has a built-in cd command.

The overwhelming majority of Unix commands are separate programsthat is, Unix commands are usually individual files that are actually small (or not-so-small) programs that perform a specific function, such as listing the contents of a directory (that would be the /bin/ls command).

Unix Commands vs. Mac Applications

Traditional Macintosh applications tend to have a great many features that allow you to accomplish complete projects all from within one application. For example, you can create and manipulate complex documents in a page-layout program. Unix takes a different approach.

In Unix, commands are often focused on specific steps you use in a variety of different tasks. For example, where the Mac has a single application (the Finder) for performing many tasks involving files, Unix uses a collection of separate "applications": the ls command lists the contents of a directory, the cd command switches from one directory to another, the cp command copies files, the mv command renames files, and so on.

This difference in approach shows a key difference in philosophy between the traditional Mac and Unix ways of thinking. In Unix, you are expected to combine commands in various ways to accomplish your work; in traditional Mac applications, the program's author is expected to have anticipated every kind of task you might want to accomplish and provided a way of doing that.

Unix provides a collection of smaller, "sharper" tools and expects you to decide how to put them together to accomplish your goals.

Your PATHhow the shell finds commands

When the shell sees a command, it evaluates whether it is a built-in commandthat is, one that is part of the shell itself. If the command is not built in, then the shell assumes the command is an actual file on the disk and looks for it.

If the command does not contain any / (slash) characters, then the shell searches in a list of places known as your PATH for a file whose name matches the command name (see the description of the PATH environment variable in Chapter 7). If the command contains any / characters, then the shell assumes you are telling it not to search your PATH but instead to interpret the command as a relative or absolute path to the command file. Relative and absolute paths are two ways of specifying Unix filenames on the command line, and we explain relative and absolute paths in Chapter 5.

About command options

Options (also called switches or flags ) modify the way a command behaves. Most commands have at least a few options available, and many commands have a large number of options. As we noted when we talked about Unix's flexibility in Chapter 1, options frequently can be combined.

See Chapter 3 to learn how to ascertain the available options for each command.

To use one option with a command:

Here's a case where we want to combine two options in a command. The -s option gave us file sizes, using a unit of measurement (blocks) that might not always have the same value (blocks are usually 512 bytes but might vary, depending on a setting called BLOCKSIZE ). If we add the -k option, the sizes are shown in kilobytes, regardless of how BLOCKSIZE is set. ( BLOCKSIZE is an environment variable ; see Chapter 7.)

To use multiple options with a command:

About command arguments

Most commands accept one or more arguments . An argument is a piece of information the command acts upon, such as the name of a file to display. It's similar to the object of a sentence .

You used a command with a single argument in the tasks above. The single argument was /Developer/Tools , the folder whose contents you wanted to list. A command line can contain multiple arguments.

To use multiple arguments with a command:

Tips

Operators and special characters in the command line

A number of special characters often appear in command lines, most frequently the > , , and & characters.

These special characters are used for a variety of powerful features that manipulate the output of commands. The most common of these operators make it easy to save the output of a command to a file, feed the output of one command into another command, use the output of one command as an argument to another command, and run a command line "in the background" (that is, letting you get a shell prompt back even if the command takes an hour to run).

The use of these powerful features is covered later in this chapter (see "Creating Pipelines of Commands").

Table 2.1 summarizes the most frequently used command-line operators and special characters, with examples of their use.

Table 2.1. Operators and Special Characters

S YMBOL

E XAMPLE AND M EANING

>

command > filename

Redirect output to file .

>>

command >> filename

Redirect output, appending to file .

<

command < filename

command gets input from file .

cmdA cmdB (sometimes called the pipe character)

Pipe output of cmdA into cmdB .

&

command &

Run command in background, returning to shell prompt at once.

` `

cmdA `cmdB` (often called backtick characters)

Execute cmdB first, then use output as an argument to cmdA .

Stopping commands

Some commands run for a long time, and sometimes they can get "stuck" (perhaps because a command is waiting for some other process to finish, or because of a network problem, or for any number of other reasons) and neither give output nor return you to a shell prompt. In those cases, you need a way to stop a command once you have started it. Here are two ways to stop a command.

If you are waiting for the shell prompt to appear, then you use to stop the command.

To stop a command with Control-C:

Tip

To stop a command using the kill command:

Getting help for a command

Most commands have associated documentation in the Unix help system. Unfortunately, Unix help is almost always written for the experienced programmer, not for the novice user, so we have devoted all of Chapter 3, "Getting Help and Using the Unix Manual," to clarifying it.

You can skip ahead to Chapter 3 and come back if you like, but here is the bare minimum you need to at least begin to explore the help available for commands.

To read the Unix manual for a command:

1.

man command

This displays the Unix manual for a command. Figure 2.9 shows the first screen of the manual for the ls command, displayed by typing man ls .

Figure 2.9. When you request help from the Unix manual by typing in a command like man ls , you get a page with an explanation of that entry (this shows only part of an output).

The man command displays the Unix manual entry for the named command, one screen at a time.

2.

To move forward one screen, press the once.

3.

To move backward one screen, press once.

4.

To quit from the man command and return to a shell prompt: .

You should be back at the shell prompt.

Категории