Unix/linux System Management Technical Manual (1)----scripts and shells

Source: Internet
Author: User
Tags sorts

1. Piping and redirection

(i) to allow the second command to be executed only after the first command has been successfully completed, you can separate the two commands with a && symbol. For example:

ls /usr/local/dir-name  2>/dev/null &&  cd/usr/local/  Dir-name

This command indicates the Dir-name folder under the ls/usr/local/directory, and if this folder exists, execute the cd/usr/local/dir-name command to enter the folder, and the CD command is not executed if the directory does not exist. The ' 2 >/dev/null ' command indicates that the error message is redirected to/dev/null, which does not display an error message.

2. Variables and references

For strings enclosed in single and double quotes, the shell handles them in a similar way, with the exception that the variables enclosed in double quotation marks can be replaced (extended with the name match metacharacters of * and?) and variable extensions. The left quotation mark is also called the left apostrophe, and it is treated like a double quote, but they have other functions, namely the ability to execute the contents of the string as a shell command, and replace the string with the output of the command. For example:

Echo " $APACHE _home "/usr/local/apache2[email protected]:echo'$APACHE _home' $APACHE _home

#将 $APACHE _home handled as a command, so error [email protected]:echo/usr/local/apache2:is a directory

3. Common filtering commands

(i) Cut: dividing the line into fields

The cut command selects a number of parts from its input lines and prints them out. The most common use of this command is to extract several domains that are qualified. 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.

See the Uniq Command section below for an example of cut usage.

(ii) Sort: Sorting rows

The sort command sorts the input rows. A fine adjustment can be made----exactly which parts of each line (i.e. "keywords") are sorted, and the order in which they are sorted. The following table shows some of the more common options:

Options Meaning
-B Ignore the opening blank
-F Sort is case insensitive
-K Specify the columns that make up the sort key
-N Compare fields by integer value
-R Reverse the order of sorts [that is, sort by reverse]
-T Set the field delimiter (the default delimiter is blank)
-U Only output unique records [duplicate records are output only once]

The following command shows the difference between numeric sorting and dictionary sorting, which is sorted by dictionary by default. These two commands use the-T: and-k3,3 Two options to sort the contents of the/etc/group file in the third field separated by a colon (that is, the group ID). The first command is sorted by value, and the second command is sorted alphabetically:

 [email protected]:~$ sort -T:-k3,3  -n/etc/grouproot:x:  0  :bin:x:  1  :d aemon:x:  2  :sys:x:  3  :adm:x:  4  : .....  
sort -T:-k3,3 /etc/grouproot:x:0: bin:x:1: wheel:x: Ten : users:x:  - : boss:x:  + : bossmysql:x: 1001 :.................

(iii) Uniq: Duplicate lines are printed only once

The Uniq command is similar in thought to Sort-u, but it has some options that sort cannot emulate:-C accumulates the number of occurrences per row, and-D displays only duplicate rows, and-u displays only the rows that are not duplicates. The input to the Uniq command must be ordered first, so it is usually run after the sort command.

For example, the following command shows that there are 3 users who have/bin/bash as their own login shell,1 user/bin/sync as the shell to log in, and so on:

Cut -D:-f7/etc/passwdsortuniq -      C3 /bin/bash       1 /bin/sync      1 /sbin/halt      /sbin/  Nologin      1 /sbin/shutdown

(iv) WC: Count the number of rows, words, characters

One way to do this is to count the number of rows, words, and characters in one file as another common operation, WC, which represents the word count, or count of words. If you run the WC without any parameters, it displays all 3 statistical results:

WC /etc/passwd     2352 /etc/passwd

and the Parameters-L,-W,-C respectively make the WC output file content of the number of lines, words, characters:

WC /etc/passwd -l /etc/passwd[email protected]:WC /etc/passwd -w /etc/passwd[email protected]:WC /etc/ passwd -c2352 /etc/passwd

(v) Tee: Copy input to two locations

Command pipelines are generally linear, but it is often helpful to insert a copy of the data stream from the middle into a file, or send it to a terminal window. This can be done with the tee command, which sends its standard input both to the standard output and to a file specified on the command line. You can think of it as a tee on a pipe.

For example, the following command sends the output value of date to the standard output and redirects the output to the Date.txt file:

Date tee ./Date:notoginseng: CST

The following command should have been displayed on the standard output, but after being redirected to the WC, the row count was not shown on the standard output:

Date Tee Date WC -l1

Unix/linux System Management Technical Manual (1)----scripts and shells

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.