• Aucun résultat trouvé

Introduction to grep-Relevant Environment Variables

Dans le document grep Pocket Reference (Page 53-58)

In previous examples, we came across the concept of environ-ment variables and their effect on grep. Environenviron-ment variables allow you to customize the default options and behavior of grep by defining the environment settings of the shell, thereby making your life easier. Issue an env command in a terminal to output all the current parameters. The following is an example of what you might see:

$ env USER=user LOGNAME=user HOME=/home/user

PATH=/usr/local/sbin:/usr/local/bin:/usr /sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:.

SHELL=/usr/local/bin/tcsh OSTYPE=linux

LS_COLORS=no=0:fi=0:di=36:ln=33:ex=32 :bd=0:cd=0:pi=0:so=0:do=0:or=31 VISUAL=vi

EDITOR=vi

MANPATH=/usr/local/man:/usr/man:/usr /share/man:/usr/X11R6/man

...

By manipulating the .profile file in your home directory, you can make permanent changes to the variables. For example, using the output just shown, suppose you decide to change your EDITOR from vi to vim. In .profile, type:

setenv EDITOR vim

After writing out the changes, this permanently ensures vim will be the default editor for each session that uses this .profile. The previous examples use some of the built-in variables, but if you are code-savvy, there is no limit (save for your imagination) on the variables you create and set.

To reiterate, grep is a powerful search tool because of the many options available to the user. Variables are no different. There are several specific options, which we describe in detail later.

However, it should be noted that grep falls back onto C locale when the variables LC_foo, LC_ALL, or LANG are not set, when the local catalog is not installed, or when the national language support (NLS) is not complied.

To start off, “locale” is the convention used for communicating in a particular language. For example, when you set the varia-ble LANG or language to English, you are using the conventions tied in with the English language for interacting with the sys-tem. When the computer starts up, it defaults to the conven-tions set up in the kernel, but these settings can be changed.

LC_ALL is not actually a variable, but rather a macro that allows you to “set locale” for all purposes. Although LC_foo is a locale-specific setting for a variety of character sets, foo can be re-placed by ALL, COLLATE, CTYPE, MONETARY, or TIME, to name a few.

These are then set to create the overall language conventions for the environment, but it becomes possible to use one lan-guage’s conventions for money and another for time conventions.

How is this related to grep? For instance, many of the POSIX character classes depend on which specific locale is being used.

PCRE also borrows heavily from locale settings, especially for the character classes it uses. Because grep is designed for searching for text in text files, the way language is processed on a machine matters, and that is determined by the locale.

For most users, leaving the locale settings as the default is fine.

Users who wish to search in other languages or want to work in a different language than the system environment might want to change these.

Now that we have familiarized ourselves with the concept of the locale, the environment variables specific to grep are:

GREP_OPTIONS

This variable overrides the “compiled” default options for grep. This is as if they were placed in the command line as options themselves. For example, suppose you want to create a variable for the option --binary-files and set it to text. Therefore, --binary-files automatically implies --binary-files=text, without the need to write it out.

However, --binary-files can be overridden by specific and different variables (--binary-files=without-match, for example).

This option is especially useful for scripting, where a set of “default options” can be specified once in the environ-ment variables and it never has to be referenced again.

There is one gotcha, though: any options set with GREP_OPTIONS will be interpreted as though they were put on the command line. This means that command-line

options don’t override the environment variable, and if they conflict, grep will produce an error. For instance:

$ export GREP_OPTIONS=-E

$ grep -G '(red)' test

grep: conflicting matchers specified

Some care and consideration needs to be taken when put-ting options into this environment variable, and it is al-most always best to set only those options that are of a general nature (for instance, how to handle binary files or devices, whether to use color, etc.).

GREP_COLORS (or GREP_COLOR for older versions)

This variable specifies the color to be used for highlighting the matching pattern. This is invoked with the --color[=WHEN] option, where WHEN is never, auto, or always. The setting should be a two-digit number from the list in Table 6 that corresponds to the specific color.

Table 6. List of color options Color Color code

Black 0;30

Dark gray 1;30

Blue 0;34

Light blue 1;34

Green 0;32

Light green 1;32

Cyan 0;36

Light cyan 1;36

Red 0;31

Light red 1;31

Purple 0;35

Light purple 1;35

Brown 0;33

Yellow 1;33

Color Color code Light gray 0;37

White 1;37

The colors need to be specified in a particular syntax be-cause the highlighting covers additional fields as well, not just matching words. The default setting is as follows:

GREP_COLORS='ms=01;31:mc=01;31:sl=:

cx=:fn=35:ln=32:bn=32:se=36'

If the desired color starts with 0 (as with 0;30, which is black), the 0 can be discarded to shorten the setting.

Where settings are left blank, the default terminal color normal text is used. ms stands for matching string (i.e., the pattern you enter), mc is matching context (i.e., lines shown with the -C option), sl is for the color of selected lines, cx is the color for selected context, fn is the color for the filename (when shown), ln is the color for line num-bers (when shown), bn is for byte numbers (when shown), and se is for separator color.

LC_ALL, LC_COLLATE, LANG

These variables have to be specified in that order, but ul-timately they determine the collating or an arrangement in the proper sequence of the expressed ranges. For in-stance, this could be the sequence of letters for alphabet-izing.

LC_ALL, LC_CTYPE, LANG

These variables determine LC_CTYPE, or the type of char-acters to be used by grep. For example, which charchar-acters are whitespace, which will be a form feed, and so on.

LC_ALL, LC_MESSAGES, LANG

These variables determine the MESSAGES locale and which language grep will use for the messages that it outputs.

This is a prime example of where grep falls back on the C locale’s default, which is American English.

POSIXLY_CORRECT

When set, grep follows the POSIX.2 requirements that state any options following filenames are treated as file-names themselves. Otherwise, those options are treated as if they are moved before filenames and treated as op-tions. For instance, grep -Estring filename-C 3 would interpret “-C 3” as filenames instead of as an option. Ad-ditionally, POSIX.2 states that any unrecognized options be labeled as “illegal”—by default, under GNU grep, these are treated as “invalid.”

Choosing Between grep Types and

Dans le document grep Pocket Reference (Page 53-58)

Documents relatifs