LPIC-1 Linux Professional Institute Certification Study Guide. Richard Blum
Чтение книги онлайн.

Читать онлайн книгу LPIC-1 Linux Professional Institute Certification Study Guide - Richard Blum страница 16

Название: LPIC-1 Linux Professional Institute Certification Study Guide

Автор: Richard Blum

Издательство: John Wiley & Sons Limited

Жанр: ОС и Сети

Серия:

isbn: 9781119021193

isbn:

СКАЧАТЬ the type command. Just enter the command type before the name of the command you wish to check:

      Some of these internal commands are duplicated by external commands that do the same thing. But those external commands aren't always installed on all systems. You can see if there are internal commands with installed duplicate external commands by using the -a option on the type command:

      You can see that on this system, there is no external cd command installed. However, it does have an external pwd command installed.

      Keep in mind that even when external commands are installed, the internal command takes precedence. To access the external command, you must provide the complete external command path, as in typing /usr/bin/time rather than time.

      Confusion over Internal and External Commands

      When duplicate internal and external commands exist, they sometimes produce subtly different results or accept different options. These differences may occasionally cause problems if you are unaware of them. For example, the time built-in command returns slightly different results than the /usr/bin/time external command:

      As you can see, bash's internal time shows the time to execute the pwd command in a very nice format, while the external time command /usr/bin/time is not only a little sloppy in appearance, it also provides additional details. Be mindful of the potential behavior differences between internal and external commands.

      When you type a command that's not recognized by the shell as one of its internal commands, the shell checks its path to find a program by that name to execute it. The path is a list of directories in which commands can be found. It's defined by the $PATH environment variable, as described shortly in “Using Environment Variables.” A typical user account has about half a dozen or so directories in its path. You can add and remove directories to the shell's path by changing the $PATH environment variable in a shell configuration file, as described in “Exploring Shell Configuration” later in this chapter.

      You can run programs that aren't on the path by providing a complete path name on the command line. For instance, typing ./myprog runs the myprog program in the current directory. Typing /home/arthur/thisprog runs the thisprog program in the /home/arthur directory.

      The root account should normally have a shorter path than ordinary user accounts. Typically, you'll omit directories that store GUI and other user-oriented programs from root's path in order to discourage use of the root account for routine operations. This minimizes the risk of security breaches related to buggy or compromised binaries being run by root. Most important, root's path should never include the current directory (./). Placing this directory in root's path makes it possible for a local troublemaker to trick root into running replacements for common programs. Omitting the current directory from ordinary user paths is also generally a good idea. If this directory must be part of the ordinary user path, it should appear at the end of the path so that the standard programs take precedence over any replacement programs in the current directory.

      Whether you need to enter the path or not for a command, the program file must be marked as executable. This is done via the execute bit that's stored with the file. Standard programs are marked as executable when they're installed, but if you need to adjust a program's executable status, you can do so with the chmod command, as described in Chapter 4, “Managing Files.”

      Performing Some Shell Command Tricks

      Many users find typing commands to be tedious and error-prone. This is particularly true of slow or sloppy typists. For this reason, Linux shells include various tools that can help speed up operations. The first of these is command completion: Type part of a command or a filename (as an option to the command), and then press the Tab key. The shell tries to fill in the rest of the command or the filename. If just one command or filename matches the characters you've typed so far, the shell fills the rest of the command (or filename) for you and adds a space after it.

      If the characters you've typed don't uniquely identify a command (or filename), the shell fills in what it can and then stops. Depending on the shell and its configuration, it may beep. If you press the Tab key again, the system responds by displaying the possible completions. You can then type another character or two and, if you haven't completed the command (or filename), press the Tab key again to have the process repeat.

      The most fundamental Linux commands have fairly short names —mv, ls, set, and so on. However, some other commands are much longer, such as traceroute or service – status-all. Filenames can also be quite lengthy – up to 255 characters on many filesystems. Thus command completion can save a lot of time when you're typing. It can also help you avoid typos.

      The most popular Linux shells, including bash and tcsh, support command and filename completion. Some older shells, though, don't support this helpful feature.

      Another useful shell shortcut is history. The shell history keeps a record of every command you type. If you've typed a long command recently and want to use it again or use a minor variant of it, you can pull the command out of the history.

      There are several rather easy methods to retrieve commands. It comes down to determining the method you like best:

      Retrieve a Command

      The simplest way to do this is to press the Up arrow key on your keyboard; this brings up the previous command. Pressing the Up arrow key repeatedly moves through multiple commands so you can find the one you want. If you overshoot, press the Down arrow key to move down the history. The Ctrl+P and Ctrl+N keystrokes double for the Up and Down arrow keys, respectively.

      Search for a Command

      Press Ctrl+R to begin a backward (reverse) search, and begin typing characters that should be unique to the command you want to find. The characters you type need not be the ones that begin the command; they can exist anywhere in the command. You can either keep typing until you find the correct command or, after you've typed a few characters, press Ctrl+R repeatedly until you find the one you want.

      The Ctrl+S keystroke is used to search forward in the command history. You can press the Ctrl+S keystroke while using the backward search. This reverses the history search from backward to forward. If you used a backward search and have passed by what you need, then this keystroke is useful.

      If the Ctrl+S keystroke causes your terminal to hang, press Ctrl+Q to resume terminal operations. To keep your terminal from hanging when Ctrl+S is used, type stty – ixon at the command line.

      In either event, if you can't find the command you want or if you change your mind and want to terminate the search, press Ctrl+G to do so.

      Frequently, after finding a command in the history, СКАЧАТЬ