Linux Novice must master commands (2)

Source: Internet
Author: User

I. INPUT and OUTPUT redirection

Input redirection refers to importing a file into a command, while output redirection refers to writing data that is intended to be output to the screen to the specified file.

In everyday learning and work, we use output redirection more frequently than input redirection.

Therefore, the output redirection is divided into standard output redirection and error output redirection of two different techniques, as well as clear write and append write two modes.

    • Standard input redirection (STDIN, file descriptor 0): input from the keyboard by default, or from other files or commands.

    • Standard output Redirect (STDOUT, file descriptor 1): Output to screen by default.
    • Error output Redirection (STDERR, file descriptor 2): Output to screen by default.

For example, we see the properties of the two files, where the second file does not exist, although the operation for the two files will be output on the screen some data information, but the difference between the two operations is actually very large.

First use LS to view the current directory, and then view the LC details, and then randomly enter a nonexistent file name query.

In the above command, the file named LC is present, and the output information is the standard output information for the command. The second file named LCLC does not exist, so the error message that is displayed after executing the LS command is also the wrong output information for the command.

Then, in order to write the original output to the screen data into the file, it is necessary to treat the two output information.

For input redirection, the symbols used and their effects are as follows:

Symbol

Role

Commands < Documents

Standard input of a file as a command

Commands << Delimiters

Read from the standard input until you meet the demarcation characters stop

Commands < Files 1 > file 2

File 1 as the standard input to the command and output the standard to file 2


For output redirection, the symbols used and their effects are shown in the following table:

symbol

action

commands > Files

will Standard output redirected to a file (emptying the data for the original file)

command 2> file

redirect the error output to a file (empty the data for the original file)

Command >> file

command 2>> file

REDIRECT error output to a file (appended to the original content)

command >> file 2>&1 or Command & amp;>> file

For the standard output mode in redirection, you can omit the file descriptor 1 not write, and the error output mode of the file descriptor 2 is required to write.

The Systemctl Status vsftpd command is written to the file LC using the standard output redirection, and then the contents of the LC file are displayed with the Cat command. The specific commands are as follows:

If you want to write the command error message to the file, usually not used.

However, this is especially useful when the user executes an automated shell script, because it can log error messages throughout the execution of the script to the file, making it easy to debug the installation.

Next we perform an experimental demonstration with a non-existent file:

Because we are using >> so append to the original content of the document.

Input redirection is relatively unpopular, and the probability of a job encounter is smaller.

The role of input redirection is to import the file directly into the command. Next, use input redirection to import the LC file to the Wc-l command and count the number of rows in the file.

Second, Pipeline command character

The function of the pipe command can also be summed up in a sentence "the original command to output to the screen standard normal data as a standard input of the latter command."

For example, in the form of page flipping to view the list of files and attributes in the/etc directory (these will be peremptorily to the screen by default, it is not clear at all):

Ls-l/etc/| More

The top shows the total number of bars, we can use the more command to browse this information.

Iii. commonly used escape characters

The 4 most commonly used escape characters are shown below.

    • backslash (\): Causes a variable after the backslash to become a pure string.
    • Single quotation mark ('): Escapes all of its variables as pure strings.
    • Double quotation mark (""): retains the variable attribute in it and does not escape processing.
    • Anti-quote ('): Returns the result after executing the command.

We first define a variable named price and assign a value of 5, and then output the string and variable information enclosed in double quotation marks:

Next, we want to be able to output "price is $", which is a string content of $5.

But it happens that the $$ effect of the combination of the dollar sign and the variable extraction symbol is to display the process ID number of the current program, and the output after the command executes is not what we expected:

To get the first "$" as the dollar sign, you need to use a backslash (\) to escape, and extract the command into a simple text and remove the special functionality.

Four, vim text editor

The VIM editor has three modes-command mode, last line mode and edit mode, each of which supports a variety of different command shortcuts, which greatly improves productivity.

To operate the text efficiently, you must first understand the operational differences between the three modes and the switching methods between them.

    • Command mode: Control the movement of the cursor, you can copy, paste, delete and find the text and other work.
    • Input mode: Normal text entry.
    • Last-line mode: Save or exit the document, and set the editing environment.

Each time you run the VIM editor, the command mode is entered by default, and you need to switch to the input mode before you write the document.

Each time you finish writing the document, you need to return to the command mode, then go to the last line mode, and perform the document save or exit operation.

In vim, you cannot switch directly from input mode to last-line mode.

Some of the commands most commonly used in the Vim Editor in command mode are as follows.

Command

Role

Dd

Delete (cut) the entire line of the cursor

5dd

Delete (cut) 5 lines starting at the cursor

Yy

Copy the entire line of the cursor

5yy

Copy 5 lines starting at the cursor

N

Displays the next string that the search command navigates to

N

Displays the previous string to which the search command is anchored

U

Undo action from the previous step

P

Pastes previously deleted (DD) or copied (yy) data after the cursor


The last-line mode is primarily used to save or exit files, as well as to set the working environment of the Vim editor, and allows the user to execute external linux commands or jump to a specific number of lines in the document being written.

To switch to the last line mode, enter a colon in the command mode. The commands available in the last-line mode are the following table.

Command

Role

: W

Save

: Q

Exit

: q!

Force quit (Discard modifications to the document)

: wq!

Force Save exit

: Set Nu

Show line Numbers

: Set Nonu

Do not display line numbers

: command

Execute the command

: integer

Jump to this line

: S/one/two

Replaces the first one in the row where the current cursor is located with two

: s/one/two/g

Replaces all one of the lines in which the current cursor is located

:%s/one/two/g

Replace all of the one in the full text with the

? string

Search for the string from bottom to top in text

/String

Search for the string from top to bottom in text

The 1th step in writing a script document is to name the document, which is named Practice.txt. If the document exists, it is opened. If it does not exist, it creates a temporary input file.

When you open the Practice.txt document, the default is to enter the command mode of the Vim editor. You can only execute commands in this mode at this time, and you cannot enter text content at will, we need to switch to input mode to write the document.

V. Writing Shell scripts

Shell Terminal interpreter can be used as a "translator" between human and computer hardware, as a communication medium within the user and Linux system, in addition to supporting a variety of variables and parameters, but also provides such as loops, branches and other advanced programming language has the control structure characteristics.

To properly use these features in the shell, it is particularly important to place commands exactly.

Shell script commands work in two ways: interactive and batch.

    • Interactive (Interactive): The user executes each command as soon as it is entered.
    • Batch: A complete shell script is written by the user beforehand, and the shell executes many commands in the script at once.

In the shell script, not only will use many Linux commands and regular expressions, pipe characters, data flow redirection and other grammatical rules, but also need to put the internal functions of the module through the logical statement processing, the final form of the daily shell script.

Look at the shell variable to see that the current system already uses bash as the command line terminal interpreter by default:

Using the Vim editor to write the Linux commands sequentially into an LC file, this is a simple script.

For example, if you want to see the current working path and list all the file and attribute information in the current directory, the script that implements this function should look like this:

The first line of the script declaration (#!) is used to tell the system which shell interpreter to use to execute the script;

The executable statement in line second to third is the Linux command we normally execute.

Execute via Bash LC (LC is the file name you just edited)

However, a script like the above can only perform some pre-defined functions that are too rigid.

In order for the shell script to do its job more flexibly, the script must be able to receive the input parameters as it did before the command was executed.

Variables that receive parameters can use a space interval between variables.

For example, it corresponds to the name of the current shell script, $ #对应的是总共有几个参数, $* corresponds to the parameter values for all locations, $? Corresponds to the return value of the execution of the last command, and $, $, $ ... The parameter values for the nth position are corresponding to each other.

For example, edit the following file:

After execution:

The following describes the user parameters to determine

When the system executes the mkdir command, it will judge the information entered by the user, that is, whether the user-specified folder name already exists, and if present, indicate an error;

Instead, it is created automatically. The conditional test syntax in the shell script can determine if the expression is true, and returns the number 0 if the condition is true, otherwise it returns other random values.

The execution format of the conditional test syntax. Remember that there should be a space on both sides of the conditional expression.

According to the test object to divide, the condition test statement can be divided into 4 kinds:

    • File test statement;
    • logical test statement;
    • Integer value comparison statement;
    • A string comparison statement.

A file test is an operator that uses a specified condition to determine whether a file exists or whether the permission satisfies the condition, such as a table:

Operator

Role

-D

Test whether the file is a directory type

-E

Test whether the file exists

-F

Determine if it is a generic file

-R

Tests whether the current user has permission to read

-W

Tests whether the current user has permission to write

-X

Tests whether the current user has permission to execute

Use the file test statement below to determine if/etc/fstab is a directory-type file, and then through the shell interpreter, the $? variable displays the return value after the previous command was executed.

If the return value is 0, the directory exists, or if the return value is nonzero, it means that the directory does not exist:

Again to determine whether/etc/fstab is a generic file

Logical statements are used for logical analysis of test results, and different effects can be achieved based on test results.

For example, in the shell terminal, the logical "and" operation symbol is &&, which means that when the previous command executes successfully, the command after it is executed, so it can be used to determine if the/dev/cdrom file exists, and if so, the output exist word.

In addition to the logical "and", there is a logical "or", it is in the Linux system operation symbol is | |, indicating that the previous command failed to execute after the command, it can be used to combine the system environment variable user to determine whether the currently logged on user is non-administrator identity:

Switch to non-administrator status for administrator authentication:

The third logical statement is "non", and the operation symbol in the Linux system is an exclamation mark (! ), which represents the inverse value of the judgment in the condition test.

That is, if the result of the original test is correct, it becomes wrong, and the result of the original test error turns it into the correct one.

We now switch back to root administrator, and then determine whether the current user is a non-administrator user.

Because the judgment result is correct due to two negatives, the preset information is normally output:

The following example is executed in order to determine whether the user variable name of the current logged-on user is equal to root, and then use the logical operator "non" to reverse the operation, the effect becomes to determine whether the currently logged on users are non-administrator users.

Finally, if the condition is set, the user typeface will be output according to the logical "and" operator, or if the condition is not satisfied, the word "root" will be output through the logical "or" operator, and if the previous && is not established, then the following | | Symbol.

An integer comparison operator is only an operation on numbers, cannot manipulate numbers with strings, files, and so on, and cannot be taken for granted by using the equals sign, greater than sign, less than, etc. in everyday life.

Because the equals sign conflicts with an assignment command, the greater-than and less-than-sign conflicts with the output redirection command and the input Redirection command, respectively.

Therefore, be sure to use the canonical integer comparison operator. The available integer comparison operators are shown in the table.

operator

action

-eq

equals

-ne

is not equal to

-gt

is greater than

-lt

is less than

-le

is equal to or less than

-ge

is greater than or equal to


Next Test whether 10 is greater than 10 and 10 is equal to 10 (judging by the output return value content):

Linux Novice must master commands (2)

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.