The characteristics of the shell Ten

Source: Internet
Author: User
Tags arithmetic egrep

Shell's characteristics: input and output redirection and piping
I/O: device, register
I/O port
Linux: All Files
Name resolution
Document descriptor (HANDLE): File descriptor, FD is the kernel used to track the current file
Input: standard input, stdin,0
Output: Standard outputs, stdout,1
Errput: Standard error output, stderr,2
I/O redirection:
<,<<: Input redirection
<: Input redirection
<&LT: Create a file here, often used to create a file or build a menu in a script
<< eof:eof generate file, end character here;

, >>: Output redirection
: Overwrite output
: Append Output
SET-C: Prohibit overwrite redirect to existing file
? >|: Force overwrite redirection under-C feature
Set +c: Turn off the above features

/dev/null:bit bucket 位桶2>,2>>:错误重定向    会输出正确信息,错误信息写入指定文件    2>:覆盖    2>>:追加    2>&1:将错误结果转为正确结果

Simultaneous re-calibration of standard output and error output
Command >/path/to/outfile 2>/path/to/errfile
Command &>/path/to/somefile
Command >/path/to/somefile 2>&1 &1: File description with &; standard output of 1;
Pipeline:
Linux: Use a single small program, combined with small programs to complete complex tasks
Command1 | Command2 | Command3
Tee: Fork output, command will not process, will be sent to the next pipeline
~]# Cat/etc/rc.d/rc.sysinit | Tee/tmp/a.out | Wc-l is the output redirected to the file/tmp/a.out, and output to Wc-l

Arithmetic operations in Bash
Declare
-i:xxx variable
-X: Environment variable, similar to export
Let
Let-varname= arithmetic expression
varname=$[Arithmetic expression]
varname=$ ((arithmetic expression))
Varname=expr 变量1 + 变量2
Variable: named memory space
Operator: +,-,,/,% (remainder)
The remainder of the division operation is omitted (rounded)
Two common ways of operation:
? Let totalweight+=2
? totalweight=$[$totalWeight +2]
+=,-=,
=,/=,%=
i++,i--, +1 every time.

Bash single-Step execution
Bash-x/path/to/script

Positional parameter br/>$0: script itself
$: The first parameter of a script
$: second argument to script
....
${10}:
Special variables
$#: Displays the number of all positional parameters
$*,[email protected]: reference all positional parameters
All parameters passed to the script, all parameters are combined into a string
? [email protected] all parameters passed to the script, each argument being a separate string
o [email protected] $ only when the double quotation marks are wrapped up only when there is a difference
Set--Clears all positional variables

Interactive scripting
Read-p interaction [X] The use of the Read command?
-T 5: Delay of 5 seconds, waiting for the user to enter information, the user does not enter the default value of the variable
Give the variable a default value
Varname=${varname:-value}
If VarName has a value, its value does not change, otherwise varname uses value as its value

grep, Egrep, and fgrep of text processing tools
grep: (Global search Regular expression (RE) and print out of the line)
Text Search tool to search the target file on a row-by-line basis based on user-specified text patterns and display matching rows
Format: grep [options] ' PATTERN ' file, default to only basic regular expressions, characters to match extra features with-E
Options:
--color: Specify Color
-V: Reverse match, showing rows that cannot be matched by the pattern
-O: Matches only strings that are matched to the pattern, not the entire row
-E:-E keyword1-e KeyWord2 representation or relationship
-W: matches the entire word representing the character boundary, or you can use the \< keyword \>
-I: Case insensitive, ignore-case
-E: Support extended
-A #: also displays the following line in the mode
-B #: Also displays a row above the pattern
-C #: Display one row before and after each
Regular expression: is the pattern (pattern) written by a class of characters
Metacharacters: Similar to wildcard characters, does not represent the meaning of the character itself, for the description of additional functionality
Basic regular expressions and extended regular expressions
The metacharacters of the basic regular expression:

字符匹配:       . :任意单个字符    [ ]:指定范围内的任意单个字符    [^]:指定范围外的任意单个字符    [0-9],[[:digit:]]    [a-z],[[:lower:]]    [A-Z],[[:upper:]]    [[:alpha:]]: 所有的字母    [[:alnum:]]:字母+数字    [[:space:]]:空格    [[:punct:]]:标点符号次数匹配:用来指定匹配其前面的字符的次数    *:任意次

? Xy xxy xy y can match
.
: matches any character of any length
\?: The preceding character appears 0 or 1 times
{m}: matches M-Times
{M,n}: At least m times, up to N times
{m,}: at least m times
{0,n}: up to n times

    " ":做变量换算需要使用    贪婪模式:尽可能的长的去匹配字符位置锚定:用于指定字符出现的位置    ^:锚定行首

? ^char
$: Anchor Line End
? grepchar$
^$: Blank Line
? ^[[:space:] "$
The position of the word is anchored
\<char: Anchor word, or \b,\bchar
Char\>: Anchor ending, char\b
Group
( )
? (AB)
Xy
Reference
\1: A back reference, referring to the first opening parenthesis and the corresponding pattern in the right parenthesis to match the content, meaning that the previous occurrence, in \1 this will appear once the same;
(A.B) xy\1,a6bxya6b

Practice grep

grep egrep Fgrep
Egrep: Use an extended regular expression to build the pattern, which is equivalent to GREP-E
Metacharacters
Character matching
.: Any single character
[]: Any single character within the specified range
[^]: Any single character outside the specified range
Number of Matches
*: The character in front of the match is any time
?: the character in front of the match 0 or 1 times
+: Match its preceding character at least once
{m}: matches its preceding character m times
{M,n}: At least m times, up to N times
{m,}: at least m times
{0,n}: read-only n times
Group:
(): Group
|: Or, AC|BC means AC or BC

Fgrep:fast, non-parsing regular expression

Conditional Judgment of Bash programming
Determine if the prerequisites for subsequent operations are met
Common judging types of conditional judgment
Integer test:
Character test:
File test:

$?
? 0: Correct
? 1-255: Error
Boolean value:
? True and False
Logical operation:
and operations:
? True && true = True (0)
? True && false = False
? False && true = False
? False && false = False
OR operation:
? true | | true = True (0)
? true | | False = True
? False | | True = True
? False | | False = False
Non-arithmetic:
?! true = False
?! false = True

How to do tests in bash
Test expression
? [Test expression]
? [[Test expression]]

Bash conditional judgment using if
? Single branch:
if condition; Then
Branch 1;
Fi
? Dual Branch:
if condition; Then
Branch 1;
Else
Branch 2;
Fi
? Multi-branch:
if condition; Then
Branch 1;
Elif Condition 2; Then
Branch 2;
elif condition 3; Then
Branch 3;
...
Else
Branch N;
Fi
As long as the command is used as a condition, it represents the result of its status (that is, execution succeeds) rather than the output of the command, and therefore cannot use the command substitution or the command, which is Command used when the command execution result is saved with or with a variable.
Bash Programming: integer Testing
Duality test
NUM1 operator num2
-eq: Equals
-ne: Not equal to

        -le:小于等于        -ge:大于等于        -lt:小于        -gt:大于

Bash knowledge Point script automatically exits
Exit N/A is not 0,1,127,255

The characteristics of the shell Ten

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.