Unix/linux System Management Technical Manual Read (12)

Source: Internet
Author: User
Tags sorts stdin

2016.11.17

Variables and quoting

Variable names is unmarked in assignments if prefixed with a dollar sign when their values is referenced. For example:

$ Etcdir= '/etc '

$ echo $etcdir

/etc

Do not put spaces around the = symbol or the shell would mistake your variable name for a command name.

2.1.3 Variables and references

Variable names are not marked at the time they are assigned, but they should be preceded by a $ character before the variable name when accessing their values. For example:

$ Etcdir= '/etc '

$ echo $etcdir

/etc

Do not leave blank on either side of the equal sign, otherwise SHLL will mistake the variable name for the command name.

When referencing a variable, you can surround it name with curly braces to clarify to the parser and to human readers whe Re the variable name stops and other text begins; For example, ${etcdir} instead of just $etcdir. The braces is not normally required, but they can is useful when you want to expand variables inside double-quoted string S. Often, you'll want the contents of a variable to being followed by literal letters or punctuation. For example,

$ echo "Saved ${rev}th version of mdadm.conf."

Saved 8th version of mdadm.conf.

referring to a variable, use curly braces to enclose the name of the variable, so that the parser and the person reading the code can clearly know where the variable name starts and ends, for example, ${etcdir} instead of $etcdir. Curly braces are not normally required, but they will come in handy if you want to extend the variables in a double-quoted string. Because people often want to follow the contents of a variable with letters or punctuation. For example:

$ echo "Saved ${rev}th version of mdadm.conf."

Saved 8th version of mdadm.conf.

There's no standard convention for the naming of shells variables, but all-caps names typically suggest environment variabl Es or variables read from global configuration files. More often than not, local variables is all-lowercase with components separated by underscores. Variable names is case sensitive.

The name of the shell variable does not have a standard command specification, but if all the letters of the variable name are capitalized, it is generally indicated that the variable is an environment variable or a variable read from the global configuration file. Local variables are mostly lowercase for all letters, and are separated by underscores between the various parts of the variable name. Variable names are case-sensitive.

Environment variables is automatically imported into Bash's variable namespace, so they can is set and read with the Stan Dard syntax. Use the export varname to promote a shell variable to an environment variable. Commands for environment variables, want to set up at login time should being included in your ~/.profile or ~/.bash_ Profile file. Other environment variables, such as PWD for the current working directory, is maintained automatically by the shell.

environment variables are automatically imported into bash's variable name space, so they can be set and read using the syntax of the identity. Command Exportvarname to promote a shell variable to an environment variable. Those commands that are used to set environment variables when the user logs on are placed in the two files of the user's ~/.profile or ~/.bash_profile. Other environment variables like the PWD (which represent the current working directory) are automatically maintained by the shell.

The shell treats strings enclosed in a single and a double quotes similarly, except that double-quoted strings is subject to Globbing (the expansion of filename-matching metacharacters such as * and?) and variable expansion. For example:

$ mylang= "Pennsylvania Dutch"

$ echo "I speak ${mylang}."

I speak Pennsylvania Dutch.

$ Echo ' I speak ${mylang}. '

I speak ${mylang}.

In the case of single and double quoted strings, the shell handles them in a similar way, with the exception that a double-quoted variable can be substituted (* and?) and the variable extension. For example:

$ mylang= "Pennsylvania Dutch"

$ echo "I speak ${mylang}."

I speak Pennsylvania Dutch.

$ Echo ' I speak ${mylang}. '

I speak ${mylang}.

Back quotes, also known as Back-ticks, is treated similarly to double quotes, but they has the additional effect of exec Uting the contents of the string as a shell command and replacing the string with the command ' s output. For example,

$ echo "There is ' wc-l/etc/passwd ' lines in the passwd file."

There is lines in the passwd file.

The left quotation mark is also called an apostrophe, and it is treated like a quotation mark, but they have other functions, which are the ability to execute a string of content in a shell command, and replace the string with the output of the command. For example:

$ echo "There is ' wc-l/etc/passwd ' lines in the passwd file."

There is lines in the passwd file.

Common Filter Commands

Any well-behaved command, reads STDIN and writes STDOUT can be used

As a filter (that is, a component of a pipeline) to process data. In this section we briefly review some of the most widely used filter commands (including some used in passing above), but The list is practically endless. Filter commands is so team oriented that it's sometimes hard-to-show their use in isolation.

2.1.4 Common filtering commands

Any "read data from stdin, output to stdout" command, which can be used as a filter (that is, a part of management) to process the data, in this section, We briefly review some of the more widely used filter commands (including some that have been used above) but such filter commands are virtually endless. The filter commands are multi-oriented to "group warfare", so sometimes their usefulness is difficult to separate.

Most of the filter commands accept one or more filenames on the command line. Only

If you fail to specify a file does they read their standard input.

Most filtering commands accept one or more file names provided on the command line as input. Only when a file is not specified, does it read data from its own standard input.

Cut:separate lines into fields

The Cut command prints selected portions of its input lines. It's most commonly used to extract delimited fields, as in the example on page a, but it can return segments defined by C Olumn boundaries as well. The default delimiter is <tab>, but you can change delimiters with the-d option. The-f options specifies which fields to include in the output.

For a example of the use of cut, see the sections on Uniq, below.

Sort:sort lines

Sort sorts its input lines. Simple, right? Well, maybe Not-there is a few potential subtleties regarding the exact parts of each line that is sorted (the "keys") a nd the collation order to be imposed. Table 2.1 shows a few of the more common options, but check the mans page for others.

cut: Dividing the rows into fields

The cut command selects a number of parts from its input lines and prints them out. The common use of this command is to extract a number of fields that are qualified, as shown in the 32-page example, but it can also return several extents bounded by the column boundaries. The default qualifier is <tab>, but you can change the qualifier with the-D option. The-F option specifies which fields are included in the output.

Refer to the contents of the UNIP command section below for an example of cut usage, as described below

Sort: Sorting rows

The sort command sorts the input rows. It's simple, isn't it? It may not be easy-fine adjustments can be made to exactly which parts of each line (i.e. "keywords") are sorted, and the order in which they are sorted. Table 2.1 shows some of the more common options, but you can view the man page to see other options.


This article is from the "Zhao Dongwei blog" blog, make sure to keep this source http://zhaodongwei.blog.51cto.com/4233742/1873907

Unix/linux System Management Technical Manual Read (12)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.