Summary of Linux shell basics

Declares which interpreter the user uses to interpret the shell program:

#!{

    bin/bash,bin/csh,bin/sh,bin/tcsh,bin/ksh

}

implement:{

    chmod 755 run.sh,./run.sh

    sh ./run.sh

}

terminal(command) < [import] file, terminal(command) > [overwrite] file, terminal(command) >> [append] file

pipe (command result output | command start input)

Special characters{

    $ variable name (reference variable)

    #Comment , the first line is declared as a special case

    Quotes [ "$`\ are strings except for escaping)" , 'all are strings' , `when the command is executed` ]

}

variable{

    system variable {

        $#(Number of command line arguments)

        $n($1...parameter)

        $0 (current program name)

        $? (previous command/function return value)

        $* (save all parameters in the form of parameters 1, 2...)

        $@ (save all parameters in the form of parameters 1, 2...)

        $$(PID of this program)

        $!(last command PID)

}

environment ( set in ~/.bash_profile or ~/.bashrc ) {

        export new environment variable name=""

        The PATH command search paths are separated by colons. The difference from DOS is that the current directory is not in the search.

        HOME The pathname of the user's home directory is the default parameter of the cd command

        COLUMNS defines the length of the command line used in command edit mode

        Editor default line editor

        Visual default visual editor

        The editor used by the Fcedit command fc

        Histfile command history file

        Histsize command history file contains the maximum number of commands

        The maximum number of lines to include in the Histfilesize command history file

        Ifs defines the separator used by the shell

        Logname User login name

        Mail points to a file whose modification time needs to be monitored by the shell

        Mailcheck shell checks the cycle of mail files

        Mailpath ? message to user

        The pathname of the shell shell

        Term terminal type

        Tmout shell automatic exit time defaults to 0 seconds (forbids shell exit)

        Prompt_command specifies the command to execute before the main command prompt

        Ps1 main command prompt

        Used when data input is required during the execution of the Ps2 command.

        Command Prompt for Ps3 select

        Ps4 debug command prompt

        Manpath looks for man page paths separated by colons

        Ld_library_path looks for library paths separated by colons

}

user{

    The naming rules are the same as c, case sensitive

    No declaration required, assignment is declaration

    There must be no spaces at both ends of the equal sign

    Reference variable [($variable name), (${variable name} is recommended to use this)]

    [unset variable name] on the command line (clear variable assignment)

    [readonly variable name] in the command line (defining constants, then assigning errors)

    Variables are global by default

    [local variable name] (local variable)

array {

    array name=(v,v,v,v,v)

    array name=(k=v,k=v,,,,,)

    array name[index]=v

    Reference array: ${array name[index]}

    Number/array declaration (declare):

    declare –(open)/+(close)[i(integer)/a(array)/f(function)/r(readonly)/x(defined as output variable through environment)]

}

The operator is basically the same as c

flow control {

    -lt <

    -the <=

    -gt >

    -ge >=

    -eq =

    -ne !=

}

branch {

    test Judgment expression / [expression] (will not produce standard output, must be judged by the return value)

    test parameter [files already exist -f (normal file)/s (number of bytes>0)/r (readable)/w (writable)/x (executable)/d (directory)] file

    Operator {-a/-o/!,\(,\) escape brackets with spaces on both ends}


    If expression; then expression; fi


    If expression; then expression; else expression; fi


    If expression; then expression; elif then expression{1,} ;fi


    Case “$#” in

    0)      Echo…;;

    1)      Echo…;;

    *)  Echo…;;

    esac

}

cycle{

    For variable name in list (as long as it is a list, such as: ls*.txt, many strings separated by spaces, seq 1 9=1~9

    Do

    Statement (quoting variable ${variable name})

    $(expr $a \* $b) evaluates the expression or [] also evaluates the expression

    Done

 

    While expression;

    Do statement;

    Done

 

    Until expression;

    Do statement;

    Done

 

    Interrupt statement [break, continue, exit (termination of the program and return value can be obtained with $?, exit return value, so specify the return value)]

    }

}

 

function{

    Declare before available

    Function name{…[return]}

    Name(){…[return]}

    call: name parameter....

recursion {

    a(){

    local i=$1

    if [$i –eq 0]

        then echo 1

    else

        local j=`expr $i-1`

        local k=`a $j` write the function name in slash quotes to implement the call

        echo `expr $i  \* $k `

    be

 }

interact {

    echo escape {

    \aBell

    \bbackspace

    \c Force newline

    \eExit

    \fClear screen

    \nNewline

    \r Enter

    \t tab

    \v vertical tab

    \\

    \onnn Octal({0,3})

    \nnn Octal({1,3})

    \xhh hex({1,2})

    echo “dfdfdg\nsdgfsg”

}

user input {

    read{

        -a name read input into name array

        -d is used to truncate the input character default newline\n

        -n read in n characters

        -p show a prompt

        -r Unescape \n Display normally

        -s quiet mode input characters do not display

        -t timeout read automatically stops

        eg: read -p "Please enter a string:" str will input the string to the str variable

        echo $str shows str

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649514&siteId=291194637