Shell overview and programming specifications

One, shell script overview

1. The concept of shell script:

Save the commands to be executed to a text file in order

Give the file executable permissions

Can be combined with respective shell control statements to complete more complex operations

2. Shell script application scenarios

Repetitive operation

Interactive task

Bulk transaction processing

Script running status monitoring

Scheduled task execution

To put it simply, save various linux commands that are usually used in order to a text file, and add executable permissions to complete more complex operations, which is a shell script.

3. The role of the shell: command interpreter, "translator"

Between the system kernel and the user, responsible for interpreting the command line

User's login shell

Shell program used by default, generally /bin/bash

Two, write a shell script

1. Write script code:

Use vim text editor (vim can check the grammatical structure, whether there are problems in writing)

Each linux command is written in order of execution

Grant executable permissions: make scripts executable

chmod +x [file name]

2. Implementation method

Method 1: sh script file path (no execution permission required)

Method 2: ./Script file path (requires execution permission to operate)

Method 3: Source script file path (does not require execution permission)

A more complete script composition includes

Script statement#! /bin/bash

Annotation information# Annotation information

Executable statement

Three, redirect

Standard input (STDIN): The default device is a keyboard, and the file number is 0. The command will read the input data needed in the execution process from the standard input file.

Standard output (STDOUT): The default device is a display, and the file number is 1. The command sends the output result after execution to the standard output file.

Standard Error (STDERR): The default device is the display, and the file number is 2. The command sends various error messages during execution to the standard error file.

Standard input, standard output, and standard error use keyboard and display as associated devices by default to interact with the operating system to complete the most basic input and output operations, that is, receive various command strings and auxiliary control information input by the user from the keyboard. And output the command result to the screen; if the command execution makes an error, the error message will be fed back to the screen.

In actual Linux system maintenance, you can change the direction of input and output content, instead of using the default standard input and output devices (keyboard and monitor). This operation is called redirection.
Insert picture description here

Redirect operation
Insert picture description here

Redirect input example:

vi pass.txt

useradd lisi

passwd --stdin lisi <pass.txt #Change the password of user lisi

Insert picture description here
Insert picture description here

Pipe operation symbol "|"

The output result of the command on the left is used as the processing target of the command on the right.
Insert picture description here

The role and type of shell variables
Insert picture description here

Four, custom variables

Insert picture description here

When the variable name is confused with other characters immediately after it, you need to add "{ }" to enclose it.

echo ${product}2.5

Special operation of variable assignment:
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Set the scope of the variable

By default, newly defined variables are only valid in the current shell environment, so they become local variables. When entering a subroutine or a new subshell environment, local variables can no longer be used.

In order to enable user-defined variables to continue in all subshell environments, the specified variables can be exported as global variables through export.

Calculation of numerical variables

expr variable 1 operator variable 2 [operator variable 3]…
Insert picture description here

Regarding the syntax of nested exp:
T=expr $( expr $X + $Y ) \ $X

Five, special shell variables

1. Environmental variables

Created in advance by the system to set up the user’s working environment

Configuration files: /etc/profile, ~/.bash_profile

Common environment variables:

PWD、PATH

USER、SHELL、HOME

2. Read-only variables

In a special case of Shell variables, once set, its value cannot be changed. Such variables are called read-only variables.
Insert picture description here

Note: The change of read-only variable is irreversible, once read-only, it cannot be undone

3. Positional variables (also called positional parameters, represented by $1, $2, $3...$9)

When executing command line operations, the first field represents the command name or script program name, and the remaining string parameters are assigned to the position variable in the order of the books from left to right.

Insert picture description here

4. Predefined variables

$#: indicates the number of positional parameters in the command line

$*: indicates the content of all positional parameters

$?: indicates the return status after the previous command is executed, 0 means correct, and anything other than 0 means abnormal

$0: indicates the name of the currently executing script or program

Guess you like

Origin blog.csdn.net/yuiLan0/article/details/109155642