Notes--shell script (1 command, regular)

        =========Command=========
            --echo
                escape\n\t
                    echo -e '1\t2\n3'
                    -e enable interpretation of backslash escapes
                    
                with color output:
                    echo -e'\e[1;31m red \e[0m'
                        \e[Display mode; foreground color; background color m
                            can use all control parameters, or only use foreground or background color
                                display mode Meaning
                                0 terminal Default setting
                                1 Highlighted
                                4 Underlined
                                5 Flashing
                                7 Reversed display
                                8 Invisible
                                Foreground color Background color Color
                                30 40 Black
                                31 41 Red
                                32 42 Green
                                33 43 Yellow
                                34 44 Blue
                                35 45 Fuchsia
                                36 46 Cyan
                                37 47 White
                        \e[0m Close color mode
                        
                Cancel command interactive:
                    some commands are interactive Standard input is received in the command line, and the blank is directly passed to the standard input to give the command, most of which are directly exit
                    # Get a certificate
                    echo -n | openssl s_client -showcerts -connect docker-hub.tools.com:443 2>/dev/null| sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
                        -n     do not output the trailing newline
                    # 执行python
                    echo "dic={'a':1,'b':2};print dic['a']"|python
                        或:
                            ]echo "
                            > import json
                            > ss = {'a':1,'b':2}
                            > print json.dumps(ss)
                            > " | python                         echo -n|sqlplus test/[email protected]/test @test.sql
                    # After the oracle script is executed, exit directly

                    #类似:
                        echo -n|supervisorctl
                        echo -n|impala-shell
                
                取消脚本交互:
                    echo xxx.war | ./tools/down_wars.sh
                    echo -e '111\n222\n' | ./tools/xxx.sh
                    echo -e "${arg2}\n"|xxx.sh ${arg1}
        
            --xargs
                xargs -i COMMAND {}
                xargs -I [] COMMAND []
                    -0, --null                   Items are separated by a null, not whitespace.
                                   Disables quote and backslash processing
                    -I R                         same as --replace=R (R must be specified)
                    -i,--replace=[R]             Replace R in initial arguments with names
                                               read from standard input. If R is
                                               unspecified, assume {}
                删除文件最好用第二种(防误删)
                    $ find . -type f -name "*.txt"  -print | xargs rm -f
                    $ find . -type f -name "*.txt" -print0 | xargs -0 rm -f

            -----read----------
                read -n 2 var #-n 2 After entering two characters, it will end automatically (no need to enter)
                
                read -p'input:' var # input:wahaha- p Prompt message
                
                read -s var #do not display (do not display the input content, can be used as a password)
                
                read -t 1 var #-t 1 timeout after 1 second (may need to cooperate with null value judgment and default value)
                
                read -d ' :'var
                
            #The delimiter ending character is: (carriage return does not end, only the specified symbol is entered, it ends, note: only a single character can be used) --tee
                is output to the file and standard output at the same time:
                ls 666 2>&1|tee 3.txt
                tee -a XXX append
                    2>&1, default tee does not accept error output, it needs to be redirected to standard output
                            
            ----------expect-------------- -
                script Example:
                    for i in `grep" ^ 1 "$ 1`; do
                    the Expect << EOF
                    spawn ssh-copy-id $i #Command to be executed
                    set timeout 5 #Set the timeout time, -1 means no timeout
                    expect {#With branch expect
                            "*yes/no" {                                     send "yes\n" #Send command, also Can "yes\r"                                     exp_continue #match to continue after branch                                     }                             "*password" {




                                    send "password01\n"
                                    }
                            }
                    expect "*password" {send "password02\n"} #Retry the password
                    ....
                    #You can add a few more passwords here expect eof #end expect
                    EOF
                    done in the
                    
                command line:
                    expect -c "set timeout -1;spawn ssh-copy-id 192.168.80.166;expect \"yes/no\" {send \"yes\r\"; };expect eof;"
                    expect -c'set timeout -1;spawn ssh-copy-id 192.168.80.166;expect "yes/no" {send "yes\n"; };expect eof;'
                        expect -c is equal to
                        expect <<EOF
                            ... ...
                        EOF
                    cat ssh(-C big C, you don’t need to write) The command is similar, EOF can be replaced by other symbols, such as!
                
            -----printf------------
                printf "%-5s %-10s %-4s\n" No Name Mark;printf "%-5s %-10s %-4.2f\n" 1 James 90.9989
                    such as %-4.2f
                    -means left-aligned, default right-aligned
                    4 means width.
                    2f means Keep two decimal places
                commonly used control characters:
                    %d int
                    %s string
                    %f float
                    %x hexadecimal%#x can output standard format, such as printf'%#x\n' 111 >>0x6f
            
            ----cat------
                cat -n pull_tag_push.sh | tr -s'\n'
                    -n display line number
                    
        --------------regular--------------------
            (pattern)    
                Match the pattern and get this match. The obtained matches can be obtained from the generated Matches collection. The SubMatches collection is used in VBScript, and the $0...$9 property is used in JScript. To match parenthesis characters, use'\(' or'\)'.

            (?:pattern)    
                matches the pattern but does not obtain the matching result, which means that this is a non-acquisition match and will not be stored for later use. This is useful in using the "or" character (|) to combine parts of a pattern. For example,'industr(?:y|ies) is a simpler expression than'industry|industries'.

            (?=pattern)    
                look ahead positive assert, which matches the search string at the beginning of any string that matches the pattern. This is a non-acquisition match, that is, the match does not need to be acquired for later use. For example, "Windows(?=95|98|NT|2000)" can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3.1". Pre-check does not consume characters, that is, after a match occurs, the search for the next match starts immediately after the last match, instead of starting after the character that contains the pre-check.

            (?!pattern)    
                Positive negative pre-check (negative assert), match the search string at the beginning of any string that does not match the pattern. This is a non-acquisition match, that is, the match does not need to be acquired for later use. For example, "Windows(?!95|98|NT|2000)" can match "Windows" in "Windows3.1" but cannot match "Windows" in "Windows2000". Pre-check does not consume characters, that is, after a match occurs, the search for the next match starts immediately after the last match, instead of starting after the character that contains the pre-check.

            (?<=pattern) The look behind (look behind) positive pre-check is similar to the positive positive pre-check, but in the opposite direction. For example, "(?<=95|98|NT|2000)Windows" can match "Windows" in "2000Windows", but cannot match "Windows" in "3.1Windows".
            (?<!pattern) Reverse negative pre-check, similar to positive negative pre-check, but in the opposite direction. For example, "(?<!95|98|NT|2000)Windows" can match "Windows" in "3.1Windows", but cannot match "Windows" in "2000Windows".        
        
            Example:
                match blank characters but not newline:
                    (?:(?!\n)\s)+
                    Note: The post-condition is included, and the pre-condition is outside the            
                search for ip other than 127.0.0.1.
                    ip a|grep -P'inet (?!127.0.0.1)'

Guess you like

Origin blog.csdn.net/weixin_42573277/article/details/114093038