LightNovesOnl.com

LPI Linux Certification in a Nutshell Part 10

LPI Linux Certification in a Nutshell - LightNovelsOnl.com

You're reading novel online at LightNovelsOnl.com. Please use the follow button to get notifications about your favorite novels and its latest chapters so you can come back anytime and won't miss anything.

For that reason, you should try not to a.s.sign positive nice numbers to foreground jobs on your terminal. If the system gets busy, your terminal could hang awaiting CPU time, which has been sacrificed by nice nice.

Changing nice numbers on running processes The nice nice command works to change the nice number for new processes only at the time that they're started. To modify a running program, use the command works to change the nice number for new processes only at the time that they're started. To modify a running program, use the renice renice command. command.

Name renice Syntax renice[+|-]nicenumber[option]targets Description Alter the nicenumber nicenumber to set the scheduling priority of one or more running to set the scheduling priority of one or more running target target processes. By default, processes. By default, renice renice a.s.sumes that the a.s.sumes that the targets targets are numeric PIDs. One or more are numeric PIDs. One or more option options may also be used to interpret targets targets as processes owned by specific users. as processes owned by specific users.

Frequently used options -u Interpret targets targets as usernames, affecting all processes owned by those users. as usernames, affecting all processes owned by those users.

-p Interpret targets targets as PIDs (the default). as PIDs (the default).

Examples This command will lower the priority of the process with PID 501 by increasing its nice number to the maximum: $renice20501 The following command can be used to increase the priority of all of user adamh adamh's processes as well as the process with PID 501: #renice-10-uadamh-p501 In this command, -10 -10 indicates a nice value of negative 10, thus giving PID 501 a higher priority on the system. A dash isn't used for the nice value, because the dash could be confused for an option, such as indicates a nice value of negative 10, thus giving PID 501 a higher priority on the system. A dash isn't used for the nice value, because the dash could be confused for an option, such as -u -u.

On the ExamBe sure to know the range and meaning of nice numbers and how to change them for new and existing processes. Also note that nice nice and and renice renice specify their numbers differently. With specify their numbers differently. With nice nice, a leading dash can indicate a nice number (e.g., -10 -10), including a negative one with a second dash (e.g., --10 --10). On the other hand, renice renice does not need the dash. does not need the dash.

You can renice processes interactively using top top's text interface by using the single-keystroke r r command. You will be prompted for the PID of the process whose nice number you wish to change and for the new nice number. If you are the superuser, you can enter negative values. The new nice number will be displayed by command. You will be prompted for the PID of the process whose nice number you wish to change and for the new nice number. If you are the superuser, you can enter negative values. The new nice number will be displayed by top top in the column labeled in the column labeled NI NI for the process you specify. for the process you specify.

Objective 7: Search Text Files Using Regular Expressions Linux offers many tools for system administrators to use for processing text. Many, such as sed, awk sed, awk, and perl perl, are capable of automatically editing multiple files, providing you with a wide range of text-processing capability. To harness that capability, you need to be able to define and delineate specific text segments from within files, text streams, and string variables. Once the text you're after is identified, you can use one of these tools or languages to do useful things to it.

These tools and others understand a loosely defined pattern language. The language and the patterns themselves are collectively called regular expressions (often abbreviated just regexp regexp or or regex regex). Regular expressions are similar in concept to file globs, but many more special characters exist for regular expressions, extending the utility and capability of tools that understand them.

Two tools that are important for the LPIC Level 1 exams and that make use of regular expressions are grep grep and and sed sed. These tools are useful for text searches. There are many other tools that make use of regular expressions, including the awk awk, Perl, and Python languages and other utilities, but you don't need to be concerned with them for the purpose of the LPIC Level 1 exams.

Regular expressions are the topic of entire books, such as Mastering Regular Expressions (O'Reilly). Exam 101 requires the use of simple regular expressions and related tools, specifically to perform searches from text sources. This section covers only the basics of regular expressions, but it goes without saying that their power warrants a full understanding. Digging deeper into the regular expression world is highly recommended in your quest to become an accomplished Linux system administrator. (O'Reilly). Exam 101 requires the use of simple regular expressions and related tools, specifically to perform searches from text sources. This section covers only the basics of regular expressions, but it goes without saying that their power warrants a full understanding. Digging deeper into the regular expression world is highly recommended in your quest to become an accomplished Linux system administrator.

Regular Expression Syntax It would not be unreasonable to a.s.sume that some specification defines how regular expressions are constructed. Unfortunately, there isn't one. Regular expressions have been incorporated as a feature in a number of tools over the years, with varying degrees of consistency and completeness. The result is a cart-before-the-horse scenario, in which utilities and languages have defined their own flavor of regular expression syntax, each with its own extensions and idiosyncrasies. Formally defining the regular expression syntax came later, as did efforts to make it more consistent. Regular expressions are defined by arranging strings of text, or patterns patterns. Those patterns are composed of two types of characters, literals literals (plain text or literal text) and (plain text or literal text) and metacharacters metacharacters.

Like the special file globbing globbing characters, regular expression metacharacters take on a special meaning in the context of the tool in which they're used. There are a few metacharacters that are generally thought of to be among the "extended set" of metacharacters, specifically those introduced into characters, regular expression metacharacters take on a special meaning in the context of the tool in which they're used. There are a few metacharacters that are generally thought of to be among the "extended set" of metacharacters, specifically those introduced into egrep egrep after after grep grep was created. was created.

The egrep egrep command on Linux systems is simply a wrapper that runs command on Linux systems is simply a wrapper that runs grep -E grep -E, informing grep grep to use its extended regular expression capabilities instead of the basic ones. Examples of metacharacters include the to use its extended regular expression capabilities instead of the basic ones. Examples of metacharacters include the ^ ^ symbol, which means "the beginning of a line," and the symbol, which means "the beginning of a line," and the $ $ symbol, which means "the end of a line." A complete listing of metacharacters follows in Tables symbol, which means "the end of a line." A complete listing of metacharacters follows in Tables 6-8 6-8 through through 6-11 6-11.

NoteThe backslash character () turns off (escapes) the special meaning of the character that follows, turning metacharacters into literals. For nonmetacharacters, it often turns on some special meaning.

Table6-8.Regular expression position anchors

Regular expression Description ^ Match at the beginning of a line. This interpretation makes sense only when the ^ ^ character is at the lefthand side of the character is at the lefthand side of the regex regex.

$ Match at the end of a line. This interpretation makes sense only when the $ $ character is at the righthand side of the character is at the righthand side of the regex regex.

<> Match word boundaries. Word boundaries are defined as whites.p.a.ce, the start of line, the end of line, or punctuation marks. The backslashes are required and enable this interpretation of <>< and="" and=""> >.

Table6-9.Regular expression POSIX character cla.s.ses

Character cla.s.s Description [:alnum:]

Alphanumeric [a-zA-Z0-9]

[:alpha:]

Alphabetic [a-zA-Z]

[:blank:]

s.p.a.ces or Tabs [:cntrl:]

Control characters [:digit:]

Numeric digits [0-9]

[:graph:]

Any visible characters [:lower:]

Lowercase [a-z]

[:print:]

Noncontrol characters [:punct:]

Punctuation characters [:s.p.a.ce:]

Whites.p.a.ce [:upper:]

Uppercase [A-Z]

[:xdigit:]

Hex digits [0-9a-fA-F]

Table6-10.Regular expression character sets

Regular expression Description [abc][a-z]

Single-character groups and ranges. In the first form, match any single character from among the enclosed characters a, b b, or c c. In the second form, match any single character from among the range of characters bounded by a a and and z z (POSIX character cla.s.ses can also be used, so (POSIX character cla.s.ses can also be used, so [a-z] [a-z] can be replaced with can be replaced with [[:lower:]] [[:lower:]]). The brackets are for grouping only and are not matched themselves.

[^abc][^a-z]

Inverse match. Match any single character not among the enclosed characters a, b b, and c c or in the range or in the range a-z a-z. Be careful not to confuse this inversion with the anchor character ^ ^, described earlier.

Match any single character except a newline.

Table6-11.Regular expression modifiers

Basic regular expression Extended regular expression (egrep) Description *

Match an unknown number (zero or more) of the single character (or single-character regex regex) that precedes it.

Match zero or one instance of the preceding regex regex.

+ + Match one or more instances of the preceding regex regex.

{n,m} {n,m} Match a range of occurrences of the single character or regex that precedes this construct. that precedes this construct. { {n} matches matches n n occurrences, occurrences, { {n,} matches at least matches at least n n occurrences, and occurrences, and { {n,m} matches any number of occurrences from matches any number of occurrences from n n to to m m, inclusively.

| | Alternation. Match either the regex regex specified before or after the vertical bar. specified before or after the vertical bar.

(regex) (regex) Grouping. Matches regex, but it can be modified as a whole and used in back-references. (1 expands to the contents of the first expands to the contents of the first () (), and so on, up to 9 9.)

It is often helpful to consider regular expressions as their own language, where literal text acts as words and phrases. The "grammar" of the language is defined by the use of metacharacters. The two are combined according to specific rules (which, as mentioned earlier, may differ slightly among various tools) to communicate ideas and get real work done. When you construct regular expressions, you use metacharacters and literals to specify three basic ideas about your input text: Position anchors A position anchor is used to specify the position of one or more character sets in relation to the entire line of text (such as the beginning of a line).

Character sets A character set matches text. It could be a series of literals, metacharacters that match individual or multiple characters, or combinations of these.

Quant.i.ty modifiers Quant.i.ty modifiers follow a character set and indicate the number of times the set should be repeated.

Using grep A long time ago, as the idea of regular expressions was catching on, the line editor ed ed contained a command to display lines of a file being edited that matched a given regular expression. The command is: contained a command to display lines of a file being edited that matched a given regular expression. The command is: g/regularexpression/p That is, "on a global basis, print the current line when a match for regular expression regular expression is found," or more simply, "global is found," or more simply, "global regular expression regular expression print." This function was so useful that it was made into a standalone utility named, appropriately, print." This function was so useful that it was made into a standalone utility named, appropriately, grep grep. Later, the regular expression grammar of grep grep was expanded in a new command called was expanded in a new command called egrep egrep (for "extended (for "extended grep grep"). You'll find both commands on your Linux system today, and they differ slightly in the way they handle regular expressions. For the purposes of Exam 101, we'll stick with grep grep, which can also make use of the "extended" regular expressions when used with the -E -E option. You will find some form of option. You will find some form of grep grep on just about every Unix or Unix-like system available. on just about every Unix or Unix-like system available.

Using sed sed, the stream editor stream editor, is a powerful filtering program found on nearly every Unix system. The sed sed utility is usually used either to automate repet.i.tive editing tasks or to process text in pipes of Unix commands (see utility is usually used either to automate repet.i.tive editing tasks or to process text in pipes of Unix commands (see "Objective 4: Use Streams, Pipes, and Redirects," "Objective 4: Use Streams, Pipes, and Redirects," earlier in this chapter). The scripts that earlier in this chapter). The scripts that sed sed executes can be single commands or more complex lists of editing instructions. executes can be single commands or more complex lists of editing instructions.

Examples Now that the gory details are out of the way, here are some examples of simple regular expression usage that you may find useful.

Name grep Syntax grep[options]regex[files]

Description Search files files or standard input for lines containing a match to regular expression or standard input for lines containing a match to regular expression regex regex. By default, matching lines will be displayed and nonmatching lines will not be displayed. When multiple files are specified, grep grep displays the filename as a prefix to the output lines (use the displays the filename as a prefix to the output lines (use the -h -h option to suppress filename prefixes). option to suppress filename prefixes).

Frequently used options -c Display only a count of matched lines, but not the lines themselves.

-h Display matched lines, but do not include filenames for multiple file input.

-i Ignore uppercase and lowercase distinctions, allowing abc abc to match both to match both abc abc and and ABC ABC.

-n Display matched lines prefixed with their line numbers. When used with multiple files, both both the filename and line number are prefixed. the filename and line number are prefixed.

-v Print all lines that do not do not match match regex regex. This is an important and useful option. You'll want to use regular expressions not only to select select information but also to information but also to eliminate eliminate information. Using information. Using -v -v inverts the output this way. inverts the output this way.

-E Interpret regex regex as an extended regular expression. This makes as an extended regular expression. This makes grep grep behave as if it were behave as if it were egrep egrep.

Examples Since regular expressions can contain both metacharacters and literals, grep grep can be used with an entirely literal can be used with an entirely literal regex regex. For example, to find all lines in file1 file1 that contain either that contain either Linux Linux or or linux linux, you could use grep grep like this: like this: $grep-ilinuxfile1 In this example, the regex regex is simply is simply linux linux. The uppercase L L in in Linux Linux will match since the command-line option will match since the command-line option -i -i was specified. This is fine for literal expressions that are common. However, in situations in which was specified. This is fine for literal expressions that are common. However, in situations in which regex regex includes regular expression metacharacters that are also sh.e.l.l special characters (such as includes regular expression metacharacters that are also sh.e.l.l special characters (such as $ $ or or * *), the regex regex must be quoted to prevent sh.e.l.l expansion and pa.s.s the metacharacters on to must be quoted to prevent sh.e.l.l expansion and pa.s.s the metacharacters on to grep grep.

As a simplistic example of this, suppose you have files in your local directory named abc abc, abc1 abc1, and abc2 abc2. When combined with bash bash's echo echo built-in command, the built-in command, the abc* abc* wildcard expression lists all files that begin with wildcard expression lists all files that begin with abc abc, as follows: $echoabc*

abcabc1abc2 Now, suppose that these files contain lines with the strings abc abc, abcc abcc, abccc abccc, and so on, and you wish to use grep grep to find them. You can use the sh.e.l.l wildcard expression to find them. You can use the sh.e.l.l wildcard expression abc* abc* to expand to all the files that start with to expand to all the files that start with abc abc as displayed with as displayed with echo echo in the previous example, and you'd use an identical regular expression in the previous example, and you'd use an identical regular expression abc* abc* to find all occurrences of lines containing to find all occurrences of lines containing abc abc, abcc abcc, abccc abccc, etc. Without using quotes to prevent sh.e.l.l expansion, the command would be: $grepabc*abc*

After sh.e.l.l expansion, this yields: grepabcabc1abc2abcabc1abc2no!

This is not not what you intended! what you intended! grep grep would search for the literal expression would search for the literal expression abc abc, because it appears as the first command argument. Instead, quote the regular expression with single or double quotes to protect it (the difference between single quotes and double quotes on the command line is subtle and is explained later in this section): $grep'abc*'abc*

or: $grep"abc*"abc*

After expansion, both examples yield the same results: grepabc*abcabc1abc2 Now this is what you're after. The three files abc abc, abc1 abc1, and abc2 abc2 will be searched for the regular expression will be searched for the regular expression abc* abc*. It is good to stay in the habit of quoting regular expressions on the command line to avoid these problems; they won't be at all obvious, because the sh.e.l.l expansion is invisible to you unless you use the echo echo command. command.

On the ExamThe use of grep grep and its options is common. You should be familiar with what each option does, as well as the concept of piping the results of other commands into and its options is common. You should be familiar with what each option does, as well as the concept of piping the results of other commands into grep grep for matching. for matching.

Name sed Syntax sed[options]'command1'[files]

sed[options]-e'command1'[-e'command2'...][files]

sed[options]-fscript[files]

Description The first form invokes sed sed with a one-line with a one-line command1 command1. The second form invokes sed sed with two (or more) commands. Note that in this case the with two (or more) commands. Note that in this case the -e -e parameter is required for each command specified. The commands are specified in quotes to prevent the sh.e.l.l from interpreting and expanding them. The last form instructs parameter is required for each command specified. The commands are specified in quotes to prevent the sh.e.l.l from interpreting and expanding them. The last form instructs sed sed to take editing commands from file to take editing commands from file script script (which does not need to be executable). In all cases, if (which does not need to be executable). In all cases, if files files are not specified, input is taken from standard input. If multiple are not specified, input is taken from standard input. If multiple files files are specified, the edited output of each successive file is concatenated. are specified, the edited output of each successive file is concatenated.

Frequently used options -e cmd cmd The -e -e option specifies that the next argument ( option specifies that the next argument (cmd) is a sed sed command (or a series of commands). When specifying only one string of commands, the command (or a series of commands). When specifying only one string of commands, the -e -e is optional. is optional.

Click Like and comment to support us!

RECENTLY UPDATED NOVELS

About LPI Linux Certification in a Nutshell Part 10 novel

You're reading LPI Linux Certification in a Nutshell by Author(s): Adam Haeder. This novel has been translated and updated at LightNovelsOnl.com and has already 1001 views. And it would be great if you choose to read and follow your favorite novel on our website. We promise you that we'll bring you the latest novels, a novel list updates everyday and free. LightNovelsOnl.com is a very smart website for reading novels online, friendly on mobile. If you have any questions, please do not hesitate to contact us at [email protected] or just simply leave your comment so we'll know how to make you happy.