1. Overview of shell

The Shell in Linux system is a special application program, which is between the kernel of operating system and the user and acts as a “command interpreter”. It is responsible for receiving and interpreting the operation instructions (commands) input by the user, passing the operation to the kernel and output the execution results.

2. Composition of shell scripts

1. Script declaration (interpreter) : the first line begins with “#! “/bin/bash” indicates that the following code statements are interpreted and executed using the /bin/bash program. #! /bin/bash is the default interpreter and there are other types of interpreters, #! / bin/python #! /bin/expect

2. Comment information: Statements starting with # represent comment information

3. Executable statements: For example, the echo command is used to output the string between “and”

3. How to call the written script?

Source and. Execute scripts in the current shell environment bash sh Absolute path relative to path The script that is executed creates a subshell environment and executes the script in this subshell environment

4, redirection details

type Device file File Description No. The default device
The standard input /dev/stdin 0 The keyboard
The standard output /dev/stdout 1 display
Standard error output /dev/stderr 2 display

Interactive hardware

  • Standard input: Receives user input data from the device
  • Standard output: Output data to the user through the device
  • Standard error: Execution error messages are reported through this device

Redirection means, instead of standard output to the screen, output to the location you specify

type The operator use
Redirect input < Reads data from the specified file instead of typing from the keyboard
Redirection output 1 > Save the output to the specified file (overwriting the original)
>> Appends the output to the end of the specified file
Standard error output 2 > Save the error message to the specified file (overwrite the original)
2 > > Standard error output is appended to the end of the specified file
Mixed output &> Can be redirected whether it’s right or wrong Save the contents of standard output and standard error in the same file
Ls-lh &> log.txt ls-LH > log.txt 2>&1 ls-LH Standard output 1 --> screen > log.txt 1 --> log.txt 2>&1 2 --> 1, 1 points to log.txt, So 2 points to log.txt '... 'is equivalent to $(...) [root@test1 ~]# vim passwd. TXT \\ Redirection enter 123321 [root@test1 ~]# setenforce 0 \\ Turn selinux off or there will be a problem [root@test1 ~]# passwd --stdin lisi < passwd. TXT # input redirection change user lisi password. Passwd: All authentication tokens have been updated successfully. [root@test1 ~]# cat passwd. TXT 123321 [root@test1 ~]# echo "123123" > passwd. TXT [root@test1 ~]#cat passwd. TXT 123123 ## multi-line redirection [root@localhost ~]#cat > ky15.txt # [root@localhost ~]#cat >file.txt <<EOF 111 222 333 444 EOF [root@localhost ~]#cat file.txt 111 222 333 444Copy the code

5. Detailed explanation of variables

5.1 Common types of shell variables include:

Custom variables: defined, modified, and used by the user

Environment variables: Maintained by the system and used to set the working environment can be viewed using the env command *

Read-only variables: can only be read and cannot be changed *

Positional variables: Pass the argument * to the script through the command line

Predefined variables: A class of variables built into Bash that cannot be modified. Some specified variables are put there for you to use *

5.1.1. Location Variables

Positional variables, also known as positional parameters, use “$1, $2, $3… “Said $9

./first.sh one two three four five …… 10… 20

 $0    $1   $2   $3    $4    $5     ${10}   ${20}
Copy the code

5.1.2 Predefined Variables

Bash is a special class of variables that are predefined by the Bash program and can be used directly, but you cannot create new predefined variables or assign values to them directly

$0 represents the script itself

$# represents the number of arguments (positional variables) that follow the script

$* without double quotation marks, $* and $@ represent all the following arguments in the script. The double quotation marks “$*” treat all arguments as a single argument (the number of arguments in this case is 1)

$@ with double quotes “$@” treats each argument that follows the script as an individual

$? Indicates the status code returned after the last command or script is executed. If the value is 0, the command is executed correctly. If the value is not 0, the command is not executed properly

5.2 Naming Requirements for variables

– Case sensitive

– Use only digits, letters, and underscores (_), and do not start with a number. Note that the hyphen (-) is not supported, which is the opposite of the host name

– Do not use built-in variables, use English to try to understand the meaning of the word, PATH – big hump StudentFirstName

– Small hump studentFirstName

– Underscore student_name

5.3 Methods for defining variables

Method 1: Variable name = variable value

Method 2:

Read Variable name Retrieves the value of the variable name from keyboard input

Read -p “Prompt” variable name

Echo -n “Info”

Read the variable name

5.4 Scope of variables

By default, newly defined variables are valid only in the current shell environment and are therefore called local variables. Local variables cannot be used again when entering a subroutine or a new shell environment.

You can use the internal command export to set the specified variable as a global variable so that the user-defined variable can continue to be used in the sub-shell environment

/etc/profile   /etc/bashrc   /root/.bash_profile   /root/.bashrc

1. The system automatically executes commands in the /etc/profile file at startup. This file is globally valid (all shell environments and users).

2. When different users log in to the system, the system automatically runs the ~/. Bash_profile command in the home directory of the user

3. ~/. Bashrc The /etc/bashrc command is automatically executed every time the current user switches a shell environment

4. /etc/bashrc for all users is automatically executed each time the user switches shell environments

5.5 Special symbols for variable assignment

1. Use double quotation marks (” “) to call variables directly

2. Using single quotes (“)$when assigning is only considered a character $and does not call a variable

3. Replace the value with the (‘ ‘invert on TAB) command to extract the output of the command. Same as $()

4. {} can separate variable values

5.6 Shell integer operations

The shell only supports integer arithmetic by default

Operator:

Add +

Subtraction –

Multiplication *

Division /

Mod %

Common operation expressions are as follows:

i=$(expr 12 * 5)

i=$((12*5))

i=$[12*5]

let i=12*5

let i++   i=$[$i+1]

let i–   i=$[$i-1]

let i+=2  i=$[$i+2]

let i/=2  i=$[$i/2]

6, pipeline operators: |

Pipe |Copy the code

The output of the command on the left can be superimposed as the input (processing object) of the command on the right

[root @ localhost opt] # ls/opt | wc 2 2 12 modify password [root @ localhost opt] # echo "123123" | passwd -- stdin zhangsan change user zhangsan The password. Passwd: All the authentication token has been successfully updated/root @ test1 ~ # grep "/ bin/bash $"/etc/passwd root: x: 0-0: root: / root: / bin/bash zhangsan:x:1000:1000:zhangsan:/home/zhangsan:/bin/bash lisi:x:1001:1001::/home/lisi:/bin/bash [root@test1 ~]# grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}' root /bin/bash zhangsan /bin/bash lisi /bin/bashCopy the code