LINUX shell-quick reference [1]

Source: Internet
Author: User
Tags control characters
**************************************** *****************************
* 1. Files and permissions
**************************************** *****************************
Chmod who [operator] [permission] filename relative mode:
Operator: + /-
Permission: r, w, x, l (lock the file), s (set the file host or group set_ID), t (paste position) Absolute mode:
Permission: r (2), w (4), x (1)
Uid: owner (set_uid) (2), group (set_gid) (4), post position (1)
Put the absolute combination number of the uid at the beginning of umask when setting the uid:
Allows you to set the default mode when creating a file. Each type of user has a corresponding mask number.
The maximum value is 6 because the system does not allow execution permissions when creating files.
The value is 7, for example:

(Directory) umask: 101 corresponds to 676 Permissions

**************************************** *****************************
* 2. Replace the file name
**************************************** *****************************
* Match any string
? Match a single string
[...] Match any character
[!...] Matched non! ************************************* ********************************
* 3. shell Input and Output
**************************************** *****************************
Echo output statement
Read variable 1 variable 2...
Cat is used to display the control characters of a file split display file.
Tee transfers a COPY to the standard output, and copies another COPY to the corresponding file. shell 3 related file descriptors:
0 standard input
1 standard output
2. standard error file redirection:
> Direct to file
>>> The file descriptor: command 0/1/2>/> filename must be used to append to a file for standard error redirection.
Command>/> filename 0/1/2> & amp; 0/1/2 command <filename
Command <delimiter reads data from standard input until the delimiter is encountered
Command <& m uses m as the standard input
Command> & m redirects the standard output to m
Command <&-close standard input ********************************* ************************************
* 4. shell Command Execution Sequence
**************************************** *****************************
& For example: command 1 & command 2 is executed only when command 1 returns true (0) and command 2 returns true (0)
| Example: command 1 | command 2 If command 1 fails to be executed, command 2
() Execute a group of commands in the current shell, such as (command 1; command 2;...). If {} is used, the output of all the commands in the group is
When redirected as a whole, it is placed in the sub-shell for execution; otherwise, it is placed in the Current shell for execution.

**************************************** *****************************
* 5. text filtering (regular expression)
**************************************** *****************************
^ Match the beginning of a row as ^ c
$ Only matches the end of a row, such as $ c
* A single character followed by * matches 0 or more.
[] Match the characters in []. It can be a single word or a character sequence, or use-to indicate the character range, for example, [1-5].
\ Is used to match the special meaning of a metacharacter.
. Match any single character
Pattern \ {n \} matches the previous pattern. n is the number of times.
Pattern \ {n, \} m is the same as above, but the number of times is at least n
Patterb \ {n, m \} is the same as above, but the number of times is between n and m ***************************** ****************************************
* 5. awk is mainly used to format text.
**************************************** *****************************
Awk script:
You can use-F to specify the delimiter. If not, use the space mode and action:
The mode can be any statement. The mode contains the BEGIN and END fields, and the actual action is specified within {}. The mode can be
To omit the domain and record:
When awk is executed, Its browsing domain ID is $1, $2.... $0 indicates that all domain regular expressions are used:
In addition to the regular expression of grep, awk also supports the following two:
+ Match one or more characters
? The occurrence frequency of the matching mode, such as/XY? Z/match XYZ and XZ
The expression must be enclosed by a slash as a condition OPERATOR:
<, <=, = ,! =,>,> = ,~ (Match regular expression ),!~ (Do not match the regular expression), such:
Awk '{if ($4 ~ /Brown/) print $0} 'grade.txt meets the expression:
& Both sides of the statement must be true
| Either side of the statement must have a true match
! Non-built-in variables:
Number of ARGC command line parameters
The ARGV command line parameters are arranged. Each parameter is accessed using ARGV [n] And the subscript starts with 0.
ENVIRON supports the use of system environment variables in the queue, such as ENVIRON ["PATH"] = "usr/local/bin"
FILENAME: the name of the file browsed by awk,
Number of FNR browsing file records
The input domain delimiter set by FS is equivalent to the-F option.
Number of NF browsing records
Number of records read by NR
Specifies the OFS output domain delimiter. The default Delimiter is space.
The delimiter of the ORS output record. The default value is \ n.
The RS record delimiter. The default value is \ n.

OPERATOR:
=, + =, * =,/=, % =, ^ =
? Conditional expression Operator
| ,&&,!
~, !~
<, <=, = ,! =,>,> =
+,-, *,/, %, ^
++, -- Built-in string functions:
Gsub (r, s), replacing r with s in $0
Gsub (r, s, t) Replace r with s in the entire t
Index (s, t) returns the first position of string t in s.
Length (s)
Match (s, r) test whether s contains a string matching r
Split (s, a, fs) divides s into sequence a in fs
Sprintf (fmt, exp), formatted exp
Sub (r, s) replaces s with the leftmost longest string in $0
Substr (s, p) returns the portion starting with p in s.
Substr (s, p, n) output function:
Printf ([format controller], parameter)

Modifier:
Format:
-Left alignment
. Prec maximum string length or number of digits on the right of the decimal point
The step in the Width field. 0 indicates the STEP 0.

Format symbol:
% C characters
% D
% E
% F
% G awk determines which floating point number to convert e or f
% O octal values
% S
% X hexadecimal
Awk array:
You do not need to define or specify the number of array elements before using arrays, for example:
'{Split ("123 #224 #245", myarrary ,"#")}'

You can also define it before using it.



**************************************** *****************************
* 6. sed text filtering Tool
**************************************** *****************************
Sed [Option] sed command input file sed [Option]-f script file input file query method:
Sed starts from the first line by default.
Positioning method:
X is a row number.
Range of rows x and y
/Pattern/query rows in the include Mode
/Pattern/query rows that contain two modes
/Pattern/, x query the rows in the contain mode on the specified row
X,/pattern/query matched rows by row number and pattern
X, y! Query rows that do not contain the specified row number x and y

Command:
P print matching rows
= Display the file line number
A \ add new text information after locating the row number
I \ Insert new text information in the positioning line number
D. Delete the row to be located.
C \ Replace the positioning line with the new text
S replace the corresponding mode with the replacement Mode
R read text from another file
W. Write text to a file
Q. exit after the first mode is matched.
L display control characters equivalent to the octal ASCII code
{} Command group executed on the target line
N read the next row from another file and append it to the next row
G paste Mode 2 to/pattern n/
Y Transfer Character
N continues to the next input line, and supports cross-row pattern matching statements.


Example:
Sed-n'2p' starts from the second line of filename
Sed-n'1, 3 p 'filename 1 ~ 3 rows
Sed-n'/pattern/P' filename
Sed-n'4,/pattern/P'
Sed-n '1, $ P' filename displays the entire file, $ indicates the last row
Sed-n'/pattern/= 'display row number
Sed '/pattern/a \ text' additional text, after the specified row
Sed '4i \ text' inserts text before the specified row
Sed '/pattern/c \ text' replaces the specified line



Replace text:
[Address [, address] s/pattern-to-find/replace-pattern/[g p w n]
By default, the g mode is replaced only once, and the global mode is replaced with g.
P by default, sed writes all replaced rows to the standard output. If p is used, no result is printed.
W. The file name directs the output to a file.




**************************************** *****************************
* 7. Tools
**************************************** *****************************
Delete and replace tr character conversion
Stty terminal features ************************************ *********************************
* 8. shell environment and variables
**************************************** *****************************
Local variable:
Variable-name = value set the actual value to the variable
Variable-name + value if variable-name is set, reset its value
Variable-name value if variable-name is not set, the undefined user error message is displayed.
Variable-name? Value: If variable-name is not set, the system error message is displayed.
Variable-name: = value if its value is not set, the original value is used if it is set.
Variable-namne:-value if value is not set to replace but value is not set to variable-name, if
Use its value unset variable to clear the variable
Set variable value: If no parameter is provided, the system displays whether all local variables are set:
Echo "The file is $ {FILE ?} "
Echo "The file is $ {FILE :? Sorry...} "set read-only variables:
Readonly variable-name:
Export variable-nameenv shows all environment variables unset variable clear environment variable reserved variable names:
This variable is found when CDPATH cd is a directory.
EXINIT saves VI initialization settings
HOME directory
LFS is used to specify the shell default delimiter.
LOGNAME
Save MAIL path
MAILCHECK checks new emails every 60 seconds by default.
MAILPATH if multiple mailboxes require MAILPATH, this variable overwrites the MAIL settings.
PS1 shell prompt. The default value is "root" and "$" for other users.
PS2 shell affiliated prompt. The default value is>. It is used to execute a command that exceeds one line.
SHELL default shell
TERMINFO terminal initialization variable save terminal Initialization Configuration
TERM terminal type
TZ Time Zone
EDITOR Default EDITOR
PWD current path name
PAGER screen paging command
MANPATH man path
PRINTER default PRINTER name to pass parameters to the script, separated by spaces, $0 ~ $9 among them, $0 always saves the Script Name specific variable parameters:
$ # Number of parameters passed to the script
$ * Display all passed parameters with a single character variable. This option can contain more than 9 Parameters
$ ID of the current script Process
$! ID of the last process running in the background
$ @ Is the same as $ #, but it is enclosed by quotation marks.
$-Current options of SHELL, same as set
$? Displays the final exit status of the command. The value 0 indicates no error, any other value indicates an error ********************************** ***********************************
* 9, quotation marks
**************************************** *****************************
Double quotation marks can be used to reference any character except the character $, backquotes, and \.
Any value can be referenced using double quotation marks.
Back quotes are used to output system commands to variables.
Backslash references special characters: & * + ^ $ '"|? **************************************** *****************************
* 10, condition test
**************************************** ******************************
Test File status:
Test condition or [condition] use [] to add spaces on both sides
-D directory
-F: regular file
-L symbolic connection
-R readable
-S file length greater than 0 non-empty
-W writable
-U file has suid settings
-X executable
-E exists
-Block B files
-C character Device File
-G sets the guid bit.
... For example: [-f filename]
Echo $?

Perform logical operations during testing:
-A logic and-o logic or! Non-logical, such:
[-W result.txt-a-w test.txt] string test:
Test "string"
Test string_operator "string"
Test "string" string_operator "string" string_operator:
= Equal
! = Unequal
-Z empty string
-N non-null string test value:
"Number" number_operator "number"
Or
["Number" number_operator "number"]
-Equal eq
-Ne
-Gt greater
-Lt is less
-Le is less than or equal
-If ge is greater than or equal to expr, it is used for numerical calculation:
Exper argument operator argument ************************************ **********************************
* 11. Control Flow Structure
**************************************** ******************************
If-else:
If condition 1
Then
Command 1
Elif condition 2
Then
Command 2
Else
Command 3
The fi NULL command is always a true case statement:
Case value in
Mode 1)
Command 1
...
;;
Mode 2)
Command 2
...
;;
Esacfor loop:
For variable name in list
Do
Command 1
Command 2
...
Doneuntil loop:
Until Condition
Do
Command 1
Command 2
...
Donewhile loop:
While command
Do
Command 1
Command 2
...
Done can use break continue to control the process, it is effective for case and loop *********************************** **********************************
* 12. shell functions
**************************************** *****************************
Format:
Function Name ()
{
Command 1
Command 2
} Or
Function Name (){
} You can also add the function parameter before the function name to pass:
The same as using parameters in shell, such as $1 ~ $9, for example: test (){
Echo $1
Echo $2
Echo $3
}
Call: test 1 2 3 function to return:
Return: the return value is determined by the final command status.
Return 0
Return 1 locate the file in shell:
The positioning format is:
. Pathname:
Function name parameter... delete function:
Unset function name ************************************* ********************************
* 13. Pass parameters to the script.
**************************************** *****************************
Shift command:
Each time a parameter is executed, the number of parameters is reduced by one and the value is moved forward by one digit, for example:
While [$ #-ne 0]
Do
Echo $1
Shift
Done *************************************** *****************************
* 14. Create screen output
**************************************** ****************************
Tput:
Character output:
Bel alarm
Blink flashing
Bold
Civis hide cursor
Clear Screen
Cnorm does not hide the cursor
Move the cursor position to x, y
Clear el to end of line
Ell to the beginning of the line
Smso startup highlighted
Rmso normal display
Smul starts underline Mode
Rmul stop underline Mode
SC saves the current cursor position
Rc restores the cursor position
Sgr0 normal Screen
Rev reverse view

Digital Output:
Cols column count
It TAB to set the width
Lines

Boolean output:
Chts cursor invisible
Hs has status line

Example: Use Boolean output:
STATUS_LINE = 'tput hs'
If $ STATUS_LINE; then
Echo "Your terminal has a status line"
Else
Echo "Your terminal don't has a status line"
Fi


Color used:
Foreground color:
30 (black) 31 (red) 32 (green) 33 (yellow) 34 (blue) 35 (purple) 36 (green) 37 (white)
Background color:
40 (black) 41 (red) 42 (green) 43 (yellow) 44 (blue) 45 (purple) 46 (green) 47 (white)

Color format:
<ESCAPE> [background_number; foreground_number] m
Example:
Echo-e "\ 033 [40; 32 m"

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.