2017-11-5linux Basics Bash Basic Features

Source: Internet
Author: User
Tags stdin


In the previous chapter we described the command completion and path completion of Bash's basic features and its command references, as well as the file management commands, such as copy, move, and delete, and then we also talked about variables, mainly around storage formats, data scopes, and engagement operations. So this time we're going to talk about the basic features of bash, this time we're talking about file wildcards and Io redirection.

First, globbing: file name wildcard

When we query some files, usually in the directory to find, sometimes a large number of files, want to find some specific files from the directory to be matched to find the matching, if not found is empty, found that the following shows the matching results of the file or directory, Then we need to use some ordinary characters to give them special meaning, for example, in the file system to match a single, or match the whole, once given special meaning, it can not be ordinary character processing, but with special characters to match the character, this is the globbing file wildcard mechanism, It is a match, not a part, of the whole file name.
So Glob also has its matching pattern, just now we have talked about, with some ordinary characters to give it special meaning, used to match the file or directory, this is its matching pattern, then the matching pattern example is as follows:

*: matches any character of any length; pa*, *pa*, *PA, *p*a PA, paa, passwd?: matches any single character in the specified range [a-z], [A-z], [0-9], [a-z0-9] has several special formats: [[: Upper:]]: all uppercase letters; [[: Lower:]]: all lowercase characters; [[: Alpha:]]: all numbers; [[:d Igit:]]: all numbers; [[: Alnum:]]: all letters and numbers; [[: Spa CE:]]: all blank symbols; [[:p UNCT:]]: all punctuation marks; [^]: matches any single character outside the specified range; [^[:upper:]] [^[0-9]] [^[:alnum:]]

So then we have the following exercises:
1. Show all files or directories with a single lowercase letter ending with 1 in the/var directory, and a file or directory with any characters appearing in the middle

# LS/VAR/1? [[: Lower:]]

2. Displays files or directories that start with any number and are not the end of a number in the/etc/directory

# ls-d/etc/[[:alnum:]]*[^[:alnum:] [ls-d/etc/[0-9]*[^0-9]

3, display in/etc directory, start with a non-letter, followed by a letter, and any other characters of the file or directory

# ls-d/etc [^a-z][a-z]*

4. Copy/etc directory, all files or directories starting with M and ending with non-digits to the/tmp/admin directory

# Cp-r/etc M*[^0-9]/tmp/admin

5, copy the/usr/share/man directory, all start with man, followed by a number end of the file or directory into the/tmp/man directory

# cp/usr/share/man/man[[:d igit:]]/tmp/man

6. In the copy/etc directory, all files or directories that end with. conf and begin with M,n,r,p/TMP/CONF.D directory

# Cp-r/etc [MNRP]*.CONF/TMP/CONF.D

Second, IO redirection and pipelines

IO redirection is simply the input and output redirection, in the computer components, all IO devices, then called input, in fact, the input and output devices are many, wherein the display is the cheapest one output device, the early output is printed to output, can not see the results in real time, Later, the display can be transmitted in real time, but the output can not be searched forward, so the function of Io redirection is to redirect the default output device to any other files or devices.
We all know that one of the philosophy of Linux is that everything is file, input and output device is also a file, then can be used for input devices: files, keyboard devices, file systems on the regular files and network cards, etc., can be used for output device is also a file, usually a monitor, file system on the regular files, network cards and so on.
Then there are three kinds of data flow in the program: one is the standard data flow: <--(symbol) We call standard input (stdin), usually the keyboard device, the other is the output of the data stream:--> we call the standard output stream (stdout), the most common is the display just said. There is also the wrong output stream:--> We call the error output (stderr), which is the display output by default, so we can summarize:

There are three data streams in the program: input data stream: <--standard input (stdin), keyboard, output stream:--standard output (stdout), monitor, error output stream: Error output (stderr), display;

There is a file descriptor on our device, each device has a different file descriptor that identifies the unique device, so is the input, so the standard output device is identified as 0, the standard output is identified as 1, and the error output is identified as 2.

Fd:file descriptor, File descriptor standard input: 0 standard output: 1 Error Output: 2

2.1 IO redirect

If we redirect a device that displays the result of one output to another device, we need to use > the , which is the output redirect, which is characterized by an overlay output, and another feature is the redirection of the append output, which we use >> .

Output redirection:> feature: Overwrite output, output redirect:>> feature: Append output;

Compared to this feature, the former direct coverage is more dangerous, so please think twice before the line, but we can put this override feature to close, we can use the SET command.

# set-c prohibit overwrite output redirect to existing file, you can use force overwrite output:> # set +c to turn off the above features;

the other is the wrong output stream redirection, which we > and the >> plus a 2 before, it's a 2> and the 2>> , in the previous section we said that the error output of its file descriptor is 2, it is important to note that the correct result is not redirected to the target file or device, and instead uses an output redirect, and its error results cannot be redirected to the target device or file, which is displayed by default. However, we can combine the correct output and error output redirection into a normal output stream and an error output stream, as shown here:

(1) &>, &>> (2) command >/path/to/somefile 2&>1 command >>/path/to/somefile 2& >1

Note that:& refers to the result of having the current screen output transferred to the background output.
There is also a special device in the/dev directory, the device name is null, empty means that we have all the data redirected here, is undoubtedly deleted, once deleted is not found, it can be understood as a black hole.
The above is the output redirection, as well as the input redirection, the symbol is , and the < other is << , what we call here Document, use as follows:

# cat << EOF # cat >/path/to/somefile << EOF

Iii. some common commands

We introduce the TR command, which is the conversion of the input characters, all of which occur within the scope of the SET1 definition and convert the bits to SET2 occurrences. It replaces, reduces, and/or deletes characters from standard input and writes the results to standard output. Its usage is as follows:

Usage 1:# tr [OPTIONS] ... SET1 [SET2] usage 2:# tr-d SET1 </path/to/somefile-d: deleting content that matches SET1 without replacing

It is important to note that this command does not modify the source file.

Iv. Pipelines

A pipeline is a connection between a command and a command, and the principle is to pass the output of the COMMAND1 through the pipeline to COMMAND2, which directs the output of the previous command directly to a program and treats it as its data stream. Use the following:

# COMMAND1 | COMMAND2 | COMMAND3

So let's talk about the tee command, which is used to output standard input to standard output and to save its files. Use the following:

# COMMAND | Tee/path/to/somefile

Exercise 1: Convert the information in the first 6 rows of the/etc/passwd file into uppercase characters after output

# HEAD-N6/ETC/PASSWD | Tr ' A-Z ' A-Z

This article from "Scorpio" blog, reproduced please contact the author!

2017-11-5linux Basics Bash Basic Features

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.