The use of VIM compiler and the basis of shell programming under Linux

Keywords: Linux shell vim CentOS yum

Installation and use of VIM compiler

    vim editor installation
    In CentOS, execute: yum -y install vim

Common mode

        h: Move one character left
         j: Move down one line
        k: Move up one line
        l: One character to the right

        PageDown (or Ctrl+F): next screen
        PageUp (or Ctrl+B): flip up one screen
        G: Move to last line of buffer
        num G: move to line num in the buffer
        gg: move to the first line of the buffer

Command line mode

        Press colon key in normal mode

        Save, exit command:
        q: Exit if buffer data is not modified
        q!: cancel all changes to buffer data and exit
        w filename: save the file to another file
        wq: save buffer data to a file and exit

        Delete data:
        x: Delete the character where the current cursor is located
        dd: delete the line of the current cursor
        dw: delete the word where the current cursor is
        d $: delete the content from the current cursor position to the end of the line

        Edit data:
        J: Delete the line break (concatenated line) at the end of the line where the current cursor is located
        u: Undo previous edit command
        a: Append data after current cursor
        A: Append data at the end of the current cursor line

        Edit data:
        r char: replace the single character of the current cursor position with char
        R text: use text to overwrite the data at the current cursor position until the ESC key is pressed
        o: Append data to the next line of the current cursor
        O: Append data on the current cursor line

        Edit data:
        i: Insert before current cursor
        1: I nsert at the beginning of the line where the current cursor is located

        Copy and paste:
        yw: copy a word
        y $: copy to end of line
        yy: copy whole line
        p: paste

Visual Modes

        To enter the visual mode, move the cursor to the position where you want to start copying, and press the v key

        Find and replace:
        To enter a search string, press the slash (/) key
        Use the n key to indicate the next (next)

        Find and replace:
        The replace command allows you to quickly replace a word in the text with another word.
        You must enter command line mode to use the replace command.
        Command:: s/old/new/

        : s/old/new/g: one line command replaces all old
        : n,ms/old/new/g: replace all old between line numbers N and m
        :% s/old/new/g: replace all old's in the entire file
        :% s/old/new/gc: replace all old's in the entire file, but prompt each time they appear

shell programming

    #!/bin/bash
    echo "Hello Bash"

    Run shell
    1. Executable rights
    chmod +x ./test.sh
    2. Execute script
    ./test.sh

Definition and use of variables

When defining variables, the variable name does not add dollar sign ($, required for variables in PHP language)
The readonly command can be used to define a variable as a read-only variable. The value of a read-only variable cannot be changed
Use unset command to delete variables, unset command cannot delete read-only variables

Scope:
Local variables (as valid in the current shell as possible, programs started by other shells cannot be accessed)
Environment variables (accessible to all programs)

        #!/bin/bash

        test="test.com"
        readonly test

        echo $test

        test="www.test.com"

        echo $test

        web="http://test.com"

        readonly web

        unset web

        echo $web

Character string

        #!/bin/bash

        var=100
        #String splicing
        str1="$var \"test\".c\nom"

        str2='w\nww'$var'.test.com'

        str3="www"$var".test.com"

        #Output string length
        echo ${#str1}

        #String truncation
        echo ${str2:1:3}

        #String lookup
        echo `expr index "$str3" w`

        #str3=$var http://www.test.com

        #echo -e $str1

        #echo -e $str2

        #echo $str3

array

        #!/bin/bash

        arr1=(1 2 3 4 5 "str")

        echo ${arr1[2]}

        #Use the @ symbol to get all the elements in the array
        echo ${arr1[@]}
        #echo ${arr1[*]}

        #Get array length
        echo ${#arr1[@]}

        #multiline comment
        :<<EOF
        arr2[0]=100
        arr2[3]=200

        echo ${arr2[3]}
        EOF

parameter

        #!/bin/bash

        echo $0

        echo $1

        echo $2

        echo $3

        # $# : number of parameters passed to script
        echo "The number of the parameters: "$#

        # $* : Display all parameters passed to the script in a single string
        echo "All parameters: "$*

        # $! : Of the last process running in the background ID Number
        # $@: And $*Same, but use in quotation marks and return each parameter in quotation marks
        echo "All parameters: "$@

        for i in "$*"
        do
            echo $i
        done

        for i in "$@"
        do
            echo $i
        done

        # $$: current process ID number of the script run
        echo "The Progress ID: "$$

        # Displays the exit status of the last command. 0 indicates no error, any other value indicates an error
        echo $?

        #$- : display Shell Current options used

Basic operators

        #!/bin/bash

        # expr is an expression evaluation tool, which can be used to evaluate expressions
        # Space between expression and operator
        # A complete expression should be included by
        i=`expr 2 + 3`
        echo $i

        a=100
        b=100

        # Conditional expressions should be placed between square brackets with spaces
        if [ $a != $b -o $a -eq $b ]
        then
            echo "a = b"
        fi

        # Multiplication sign(*)Forward slash required(\)To realize multiplication
        echo `expr 100 \* 100`

        # Relational operators only support numbers, not strings, unless the value of the string is a number
        # -eq , -ne , -gt , -lt , -ge , -le
        if [ $a -eq $b ]
        then
            echo "eq"
        fi

        # Boolean operator! , -o , -a
        if [ $a == 100 -a $b == 100 ]
        then
            echo "a=100,b=100"
        fi

        if [[ $a == 100 && $b == 100 ]]
        then
            echo "a=100,b=100"
        fi

        # String operator  = , != , -z , -n , str
        str1='This'

        if [ -z "$str1" ]
        then
            echo "The length of the string is 0"
        fi

        if [ -n "$str1" ]
        then
            echo "The length of the string is not 0"
        fi

        if [ $str100 ]
        then
            echo "This string is not empty"
        fi

        # File test operators are used to detect various properties of Unix files
        # -b , -c , -d , -f , -g , -k , -p , -u , -r , -w , -x , -s , -e
        file='/root/shell/hello_bash'

        if [ -e $file ]
        then
            echo "This file exists"
        fi

echo

        #!/bin/bash

        echo 'This is string'

        echo "This is string"

        # Quotes for strings can be omitted
        echo This is string

        # The read command reads a line from standard input and assigns the value of each field in the input line to the shell variable
        echo -n "Please input your age: "
        read age

        echo "My Age is "$age

        # Show newline:
        echo -e "OK! \n" # -e Opening escape

        # Show no line breaks:
        echo -e "OK! \c" # -e Opening escape \c nowrap

        # Show results directed to file:
        echo "Learning IT" > test

        # Display command execution results:
        echo `date`

printf

        #!/bin/bash

        # format-string: Control string for format
        # %s %c %d %f Are all format alternators
        # %-10s Refers to a width of 10 characters(-Indicates left alignment, if not, indicates right alignment). Any character will be displayed within 10 character wide characters. If it is not enough, it will be automatically filled with spaces. If it is more than 10, it will also display all content
        printf "%-10s is string %d %.4f" "string" "100" 210

        if [ 1 == 1 ]
        then
            echo "correct"
        fi

        # The test command is used to check whether a certain condition is true. It can test three aspects: numerical value, character and file
        if test 1 == 1
        then
            echo "test correct"
        fi

Process control

        #!/bin/bash
        i=2

        if [ $i == 1 ]
        then
            echo "i = 1"
        elif [ $i == 2 ]
        then
            echo "i = 2"
        else
            echo "i != 1"
        fi
        --------------------------

        #!/bin/bash

        for i in 1 2 3 4 5
        do
            if [ $i -eq 1 ]
            then
                break
            fi
            echo $i
        done
        --------------------------

        #!/bin/bash

        i=1

        while (( $i < 10 ))
        do
            echo $i
            # let command, which is used to execute one or more expressions, does not need to add $to the variable calculation to represent the variable
            let "i++"
        done

        while :
        do
            echo "unlimit"
        done

        while true
        do
            echo "unlimit"
        done

        # Infinite loop:
        # while Remove condition
        # while true
        # for (( ; ; ))
        for (( ; ; ))
        do
            echo "for unlimit"
        done
        --------------------------

        #!/bin/bash

        # until Loop through a series of commands until the condition is true Stop when
        # until Cycle and while The cycle is just the opposite of the way it's handled
        i=10

        until (( $i == 0 ))
        do
            echo $i
            let "i--"
        done
        --------------------------

        #!/bin/bash

        i=4

        # case statement is a multiple choice statement
        case $i in
            1) echo 'i=1'
            ;;
            2|3|4) echo 'i=2 or i=3 or i=4'
            ;;
            *) echo 'i != 1 && i != 2'
            ;;
        esac

function

        #!/bin/bash

        # myfunc 2 2

        # function myfunc()
        myfunc()
        {
            echo "myfunc"
            # Parameter return, can display plus:return Return. If it is not added, the result will be run with the last command as the return value. return Heel value n(0-255)
            # In Shell, you can pass parameters to a function when you call it. Inside the function body, get the value of the parameter in the form of $n, for example, $1 for the first parameter, $2 for the second parameter
            # $10 Can't get the tenth parameter, it needs to get the tenth parameter ${10}. When n>=10 You need to use ${n}To get parameters
            return `expr $1 + $2`
        }

        myfunc 1 2

        # The function return value passes the $? To get
        echo $?

        # myFunc

I / O redirection
If you want to execute a command and you don't want to display the output on the screen, you can redirect the output to / dev/
/ dev/null is a special file, the content written to it will be discarded; if you try to get the content from the file, you can't read anything. But the / dev/null file is very useful, redirecting the output of the command to it will have the effect of "disable output"

File contains

        #!/bin/bash

        source ./public.sh

        myfunc

Posted by gravedig2 on Tue, 14 Jan 2020 22:50:03 -0800