Using a Command

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:

  • ls /Developer/Tools

    This is the ls command, which lists the names of files and directories. The output of the commanda list of the tools installed in the /Developer/Tools directoryappears, and then a new shell prompt follows ( Figure 2.4 ).

    Figure 2.4. When you type the command line ls /Developer/Tools , this is what you see.
     user-vc8f9gd:~ vanilla$  ls /Developer/Tools  BuildStrings                   RezWack                mdcheckschema CpMac                          SetFile                mdimport DeRez                          SplitForks             momc GetFileInfo                    UnRezWack              packagemaker MergePef                       WSMakeStubs            pbhelpindexer MvMac                          agvtool                pbprojectdump PPCExplain                     cvs-unwrap             uninstall-devtools.pl ResMerger                      cvs-wrap Rez                            firewire user-vc8f9gd:~ vanilla$ 

    The command line you just used consists of two parts : the command ( ls ) and an argument ( /Developer/Tools ).

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 :

  • The command (required)

  • Options (or switches or flags ) (optional)

  • Arguments (optional)

  • Operators and special characters (optional)

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:

  • ls -s /Developer/Tools

    The -s option modifies the output of the ls command, asking for the size of each file. That number (in blocks , which correspond to disk space) is now displayed alongside each filename ( Figure 2.6 ).

    Figure 2.6. When you type this command line, ls -s /Developer/Tools , which has one option, the output lists the size of each file in blocks.
     user-vc8f9gd:~ vanilla$  ls -s /Developer/Tools  total 2144  48 BuildStrings                                    448 WSMakeStubs  56 CpMac                                            32 agvtool 224 DeRez                                             8 cvs-unwrap  40 GetFileInfo                                       8 cvs-wrap 256 MergePef                                          0 firewire  56 MvMac                                            64 mdcheckschema 168 PPCExplain                                        8 mdimport  48 ResMerge                                         72 momc 264 Rez                                               8 packagemaker  48 RezWack                                          40 pbhelpindexer  48 SetFile                                          56 pbprojectdump  48 SplitForks                                       48 uninstall-devtools.pl  48 UnRezWack user-vc8f9gd:~ vanilla$ 

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:

  • ls -s -k /Developer/Tools

    Simply supply both of the options you want.

  • You can combine two or more options:

    ls -sk /Developer/Tools

    This produces the same output as in the first case and saves typing two characters. Figure 2.7 shows the output of both command lines. (Unix commands are short and cryptic because the programmers who invented them wanted to lessen the amount of typing they needed to do.)

    Figure 2.7. This shows that there's no difference between what you get when you use the -s and -k options separately and when you combine them in the -sk option.
     user-vc8f9gd:~ vanilla$  ls -s -k /Developer/Tools  total 1072  24 BuildStrings                              224 WSMakeStubs  28 CpMac                                      16 agvtool 112 DeRez                                       4 cvs-unwrap  20 GetFileInfo                                 4 cvs-wrap 128 MergePef                                    0 firewire  28 MvMac                                      32 mdcheckschema  84 PPCExplain                                  4 mdimport  24 ResMerger                                  36 momc 132 Rez                                         4 packagemaker  24 RezWack                                    20 pbhelpindexer  24 SetFile                                    28 pbprojectdump  24 SplitForks                                 24 uninstall-devtools.pl  24 UnRezWack user-vc8f9gd:~ vanilla$  ls -sk /Developer/Tools  total 1072  24 BuildStrings                              224 WSMakeStubs  28 CpMac                                      16 agvtool 112 DeRez                                       4 cvs-unwrap  20 GetFileInfo                                 4 cvs-wrap 128 MergePef                                    0 firewire  28 MvMac                                      32 mdcheckschema  84 PPCExplain                                  4 mdimport  24 ResMerger                                  36 momc 132 Rez                                         4 packagemaker  24 RezWack                                    20 pbhelpindexer  24 SetFile                                    28 pbprojectdump  24 SplitForks                                 24 uninstall-devtools.pl  24 UnRezWack 

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:

  • ls /Developer /Developer/Tools

    You simply add as many arguments as needed on the command line, separated by spaces. In this example, the ls command gets two arguments and lists the contents of both directories ( Figure 2.8 ).

    Figure 2.8. The ls command gets two arguments, /Developer and /Developer/Tools , and lists the contents of both directories.
     user-vc8f9gd:~ vanilla$  ls /Developer /Developer/Tools  /Developer: ADC Reference Library Extras Applications                         Headers                  Palettes Documentation                        Java                     Private Examples                             Makefiles                Tools /Developer/Tools: BuildStrings                         RezWack                  mdcheckschema CpMac                                SetFile                  mdimport DeRez                                SplitForks               momc GetFileInfo                          UnRezWack                packagemaker MergePef                             WSMakeStubs              pbhelpindexer MvMac                                agvtool                  pbprojectdump PPCExplain                           cvs-unwrap               uninstall-devtools.pl ResMerger                            cvs-wrap Rez                                  firewire user-vc8f9gd:~ vanilla$ 

Tips

  • You can combine multiple options with multiple argumentsfor example,

    ls -sk /Developer /Developer/Tools

  • Remember that the shell expects the parts of a command line to be separated by spaces. If an argument has spaces in it, then you need to protect the embedded space(s) from being interpreted as separators. See "About Spaces in the Command Line," later in the chapter.


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:

  • Press (usually at the lower left of your keyboard) and simultaneously press . This sends what is called an interrupt signal to the command, which should stop running and bring up a shell prompt.

Tip

  • If using doesn't work, as a last resort you can close the Terminal window, overriding the warning that appears. The stuck command will be stopped . It doesn't hurt Unix for you to close the window; it's just annoying for you.


To stop a command using the kill command:

  • kill pid

    You use the kill command to stop other commands if you already have a shell prompt. You need to know the process ID of the command you want to stop. For details on obtaining process ID numbers , see "About Commands, Processes, and Jobs," later in this chapter.

    The kill command doesn't always kill a process. It actually sends a signal asking it to stop. The default signal is hangup .

    Sometimes that isn't strong enough. In those cases you can use signal 9, the kill signal that cannot be ignored:

    kill -9 pid

    Using signal 9 terminates the target with extreme prejudicethe stopped command has no chance to clean up before exiting and may leave temporary files around.

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.



Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net