Linux Bash Shell (3): Basic I/O operations, special characters, control keys, and help

Source: Internet
Author: User
Tags echo command first string

This article is also the first chapter of "learning the bash shell" 3rd edition, the Reading Notes of bash basics. But we will not limit this. Bash is used in Linux and has never been systematically studied.

Some I/O operations

Linux I/O has two features: 1. I/O is divided into character and block. 2. All generated and received data can be regarded as file. In other words, these I/O operations can be considered as file operations. Several common Commands include

Cat (copy input to output)

Grep (query certain strings in the input );

Sort (sort );

Cut (extract column );

Sed (edit is provided in the input );

Tr (replace some characters in the input with other characters)

All UNIX programs have standard input stdin, stdout, and stderr. These input and output can be re-routed to other files, and we can redirect them through "<" and ">. The input and output directions are clearly displayed. For example, Echo "Hello, world! "> Temp, Which is output to the temp file. If we use the output of a command as the input of another command, this structure becomes a pipeline (PIPE) and use "|". If a command contains two or more commands through pipe, this command becomes pipeline. For example, LS-L | less. The following is a complex example:

  

Extract the usernames from/etc/passwd and display them in order. We use the cut command, cut-D:-F1, which indicates that the columns are separated by ":". We need the first column. In/etc/passwd, the first column is the user name. Command: Cut-D:-F1/etc/passwd | sort. If we want to print the result, command: Cut-D: -F1/etc/passwd | sort | lp.

Foreground and background commands

If you need to run the command in the background, that is, you can execute other operations in the current terminal before the execution ends. Use &. A common command is a prerequisite, that is, other commands cannot be executed before the command ends. Background running is often used for the deployment of our services. We usually add our service command: myservice & to the RC. Local file that runs after the system is started, so that myservice will continue to run as a background job after the system is started. If it is used as a background command, it cannot be input through stdin. If the command needs to wait for the user to press the keyboard, it will always be there. If we start the background command in terminal, but input it again, it will be output to termial. If the current foreground command also has output, it will be mixed together, this is a good choice for redirecting output results to a file.

Generally, foreground and background commands have the same priority level. However, when the computer resources are insufficient, we may want the background commands not to affect the foreground commands, or our background services have a high priority level and don't want to be impressed. You can use nice commands to set their priority level. You can refer to it through man nice. In cgywin, the default priority is 10. The greater the value, the lower the priority. Nice-n8 command1 sets the priority of command1 to 8.

Some special symbols

There are some special symbols in shell, such as <,>, |, $, &, *, and so on. Sometimes we don't need these special symbols for special purposes, but just a character. In this case, quotation marks are used. The quotation marks include single quotation marks and double quotation marks. single quotation marks represent strong quote, while double quotation marks represent weak quote, for double quotation marks, he can only identify some special symbols. This is suitable for some situations and will be introduced in the subsequent sections of this book.

Echo 2*3> 5 is a valid inequality,> 5 indicates redirecting the output to a file named 5, which is equivalent to echo 2*3 is a valid inequlity> 5, while * is a wildcard, the echo command does not need to be selected or matched. We tested it and put 2 temp 3 is a valid inquality in the 5 file. But in fact, we want to output 2*3> 5 is a valid inequality on termimal. We need to block the special uses of> and *, which are enclosed by quotation marks, for example, echo '2*3> 5 is a valid inequality ', we can also just quote the part that contains the special symbol, for example, echo '2*3> 5' is a valid inequality, and their effects are the same.

If we need to block the special usage of a special character, we can use the backslash/, for example, '*' is equivalent to/*. In the above example, we can write: echo 2/* 3/> 5 is a valid inequality. For example, if we want to display '2*3> 5' is a valid inequality, it can be written as echo/'2/* 3/> 5/'is a valid inequality. If/is displayed first, you can use //. Shell is parsed in order. We need to note the nested quotation marks.

For example, if echo 'Hello/"world 'is output as hello/" world, the first single quotation mark starts and the other single quotation mark is searched. All special characters in the middle are ignored, including the/symbol. For example, echo 'Hello/'World', and the second quotation mark is in the middle, so the first string of characters hello/and then a 'are found, and the system will wait for the next ', if the command is not completed, <is displayed, prompting you to continue entering the command.

For the weak quote of double quotation marks, the situation changes. echo "hello/world" shows hello/world, while echo 'Hello/world' shows hello/world, for example, echo "hello/" world ", show hello" world, echo "hello/'World", show hello/'World, for double quotation marks/is valid, where/", //, and is valid,/is still a special conformity, And/is invalid and is not considered a special conformity. In this chaotic situation, there is no need to remember (it is easy to forget). We only need to avoid writing such a method. If not, do not use double quotation marks or between double quotation marks, avoid using special symbols. If so, test it.

We can press Enter next to/to indicate that the Enter next to/is a normal line feed, rather than the end of the input. Shell uses this method to implement multi-line Input. When wrapping a line, you only need to type.

Some control keys

CTRC + A button has a special control purpose, I generally only use CTRC-C, also written as ^ C. The record is as follows. We can also use stty-a to view these controls.

CTRL-C: stop the current command

CTRL-D: Stop input, usually logout

CTRL-/: Invalid use in the CRTL-C, used to stop the current command, in fact, use kill can also, there is no need to remember too much

CTRL-S: Stop screen output

CTRL-Q: Re-output screen

CTRL-U: Delete the main flight command, the general CTRL-C effect is the same

CTRL-Z: hangs the current command, detailed in chapter 1 of this book

Help

We usually use man for help. In bash, you can also use help to list commands and use help <command> to provide help for specific commands.

 

Related Links: My articles on Linux operations

Related Article

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.