Shell overview

A shell is a program with special features that provides an interface for users to interact with the kernel. It receives the command entered by the user and sends it to the kernel for execution. The kernel is the heart of the Linux system. It resides in the computer's memory from the power-on self-test until the computer is turned off, and the user's application is stored on the computer's hard disk and is only loaded into memory when needed. Shell is an application. When a user logs in to the Linux system, the shell is called into memory for execution. The shell is independent of the kernel. It is the bridge between the kernel and the application. The input device reads the command and converts it to a mechanical code that the computer can understand. The Linux kernel can execute the command.

Shell programming usage

Advantage

The benefits of Shell scripting language are simple, easy to learn, easy to use, suitable for handling objects such as files and directories. Quickly completing some complex things in a simple way is often an important principle for creating scripts. The characteristics of scripting languages ​​can be summarized as follows. several aspects:

The syntax and structure are usually relatively simple.

Learning and using is usually simpler.

It is usually done in an "interpretation" that is easy to modify the program, rather than "compilation."

The development capacity of the program is better than the running performance.

Shell scripting language is an important scripting language on Linux/Unix systems. It is widely used in Linux/Unix. Mastering the shell scripting language is an excellent way for Linux/Unix developers and system administrators. Shell scripting languages ​​can be used to implement complex operations succinctly, and shell scripts can often be used on different versions of Linux/Unix systems.

Shell programming

Basic format

The shell script's file name suffix is ​​usually .sh (of course you can use other suffixes or no suffix, .sh is for specification)

Programming format:

[java] view plain copy#! /bin/bash

#注使用#号

Code example:

[java] view plain copy / / use vi editor to write shell script (a.sh does not exist will be new)

Vi a.sh

Write the execution code after entering vi edit mode

[java] view plain copy / / fixed format, remember it will be

#! /bin/bash

//executed code

Echo Hello World

Grant permissions and execute:

[java] view plain copy / / give executable permissions

Chmod +x a.sh

/ / Execute (call /bin/bash to execute a.sh script)

. /a.sh

Results of the:

Shell programming usage

Here are a few of the following:

[java] view plain copya.sh

In this case, you need to ensure that the script has execute permission and has (.) in the environment variable PATH, so it will be looked up from the current directory when it is executed.

Shell programming usage

[java] view plain copy./a.sh

Just make sure this script has execute permission

[java] view plain copy/usr/local/a.sh

Just make sure this script has execute permission

[java] view plain copybash a.sh

It can be executed directly. Even the first line in this script file can be imported without the /bin/bash. It is executed by passing hello.sh as a parameter to the bash command.

[java] view plain copybash -x /path/to/aa.sh

Bash single stepping

[java] view plain copybash -n /path/to/aa.sh

Bash syntax check

variable

Variables do not need to be declared, initialization does not require a type

Variable naming

1, can only use numbers, letters and underscores, and can not start with a number

2, variable names are case sensitive

3, the proposed order should be easy to understand

Note: Variable assignments are assigned by an equal sign (=), and no spaces can appear between variables, equal signs, and values.

Display variable values ​​using the echo command (similar to system.out in java), plus $ variable name, you can also use ${variable name}

E.g:

[java] view plain copyecho $JAVA_HOME

Echo ${JAVA_HOME}

Declaration and use of variables:

Shell programming usage

Variable classification:

Shell variables have these categories: local variables, environment variables, local variables, position variables, special variables.

Local variables:

Only valid for the current shell process, invalid for the child process of the current process and other shell processes.

Definition: VAR_NAME=VALUE

Variable reference: ${VAR_NAME} or $VAR_NAME

Cancel variable: unset VAR_NAME

Equivalent to the private variable (private) in java, can only be used by the current class, subclasses and other classes can not be used.

For example, if you use bash in a bash command window, it becomes a child process, and local variables are not accessed by this child process.

Shell programming usage

Environmental variables:

Custom environment variables are valid for the current shell process and its subshell processes, and are invalid for other shell processes.

Definition: export VAR_NAME=VALUE

Valid for all shell processes needs to be configured into the configuration file

[java] view plain copyvi /etc/profile

Source /etc/profile

Equivalent to the protected modifier in java, can be shared under the current class, descendant class, and the same package.

Similar to the environment variables in windows

Custom environment variables:

Shell programming usage

Local variables:

Called in the function, the function execution ends, the variable will disappear

Valid for a code snippet in a shell script

Definition: local VAR_NAME=VALUE

The local variable defined in one of the methods in the java code is only valid for this method.

Position variable:

For example, the parameters in the script:

$0: the script itself

$1: The first argument of the script

$2: The second parameter of the script

Equivalent to the args parameter in the main function in java, you can get external parameters.

Shell programming usage

Special variables:

$? : Receive the return status code of the previous command

Return status code between 0-255

$#: number of parameters

$*: or $@: all parameters

$$: Get the current shell's process number (PID) (can implement script suicide) (or use exit command to exit directly or use exit [num])

quotation marks

There are three types of quotes in Shell programming: single quotes, double quotes, and back quotes.

''Single quotes do not resolve variables

[java] view plain copyecho '$name'

"" double quotes will resolve variables

[java] view plain copyecho "$name"

The ``backquote is the result of executing and referencing a command, similar to $(..)

[java] view plain copyecho `$name`

Example:

Shell programming usage

Smart BMS

Smart Bms,Bms For Battery,Bms For Lithium Battery,Bms Module

HuiZhou Superpower Technology Co.,Ltd. , https://www.spchargers.com