当前位置:网站首页>Shell programming specifications and variables
Shell programming specifications and variables
2022-07-27 07:00:00 【1701y】
Shell Programming specification
Shell A script is a special application , It lies between the operating system kernel and the user , Acting as a “ command interpreter ” Role , Be responsible for receiving the operation instructions input by the user ( command ) And explain , Pass the operation to be performed to the kernel for execution , And output the execution result .
Shell The role of
command interpreter ," The interpreter "
Translator between kernel and user
Shell The concept of script
Save the commands to be executed in order to a text file
Give the file executable permissions
It can combine all kinds of Shell Control statements to perform more complex operations
Shell Script application scenarios
Repetitive operations
Sometimes you need to repeat the operation , You can write a script to implement the loop
Interactive tasks
Batch transactions
Service running status monitoring
Scheduled task execution
Write script to crotable -e Come in and do the scheduled task
Write script code
Use vim Text editor
One in each line Linux command , Write... In order of execution
Writing shell Script makes , Put... After the name .sh, To represent this is shell Script command
Give executable permission
Make the script executable
chomd +x file
Execute script file
Method 1 : Script file path ( Absolute path and relative path )
There must be x jurisdiction
Method 2 :sh Script file path
Method 3 :source Script file path ( Execute the script in the system environment )
It can also be done through “.” To execute


shell Script composition
Script declaration
Also called “ Interpreter ”: If the first act "#!/bin/bash", This line of code is represented by /bin/bash Program to explain the execution of ,#!/bin/bash Is the default interpreter , There are other types of interpreters
Annotation information
Executable statement

Redirection and pipeline operations
Interactive hardware devices
The standard input : Receiving data input from the user from the device
standard output : Output data to the user through the device
The standard error : Report execution error information through this device

Redirection operation

Pipe operation symbols “|”
Output the command on the left , As the processing object of the right command

Shell The role and type of variables
Role of variables
It is used to store specific parameters that the system and users need to use ( value )
Variable name : Use a fixed name , Preset by the system or defined by the user
A variable's value : According to the user settings , The system changes with the change of the system environment
The type of variable
environment variable : Maintained by system , Used to set up the work environment
Predefined variables :Bash A class of variables built into , Cannot be modified directly
Custom variable : Defined by the user 、 Modify and use
Positional variable : Pass parameters to the script through the command line
Define a new variable
Variable names start with letters or underscores , Case sensitive , It is suggested to use all capitals .
Variable name = A variable's value (“=” Is the meaning of assignment )
Look at the value of the variable
echo $ Variable name

If you want to closely connect other content behind the output , Adding directly is not enough , We need to use it at this time “{}”, Use the symbol , You can realize the above statement . Or use " ", take $ Put all the codes in quotation marks , It can also be realized .
Let's set a variable to output normally . If we want to add 123 Add... Directly after , It cannot be output . Or the middle space is being added

At this time, we can use {}

perhaps “”

Or will 123 Also assign values and output them together

Use quotation marks when assigning values
Double quotes : Allow to use $ The symbol references other variable values

Single quotation marks : Do not reference other variable values ,$ Treat as normal characters

Apostrophe : Command substitution , Extract the output of the command after execution (`` Only quotation marks You can't use braces )
notes : Backhanded cannot be nested inside backhanded

Similar to backhand
Assign values to variables from keyboard input
read -p " Prompt information " Variable name

Action range of variable
In order to make user-defined variables in all sub variables shell Can continue to be used in the environment , Reduce duplicate setup work , It can be done by internal command export Exports the specified variable as a global variable

Format 1:export Variable name
Format 2:export Variable name = A variable's value

The operation of integer variables
expr Variable 1 Operator Variable 2 [ Operator Variable 3]…
Common operators
- Addition operation :+
- Subtraction :-
- Multiplication :\*
- Division operations :/
- modulus ( Remainder ) operation :%
notes : This method is not simple enough , Just for understanding

Variables and operators should be separated by spaces Otherwise, it will be recognized as a character and output directly, such as

Concise operation method
1 echo $[1+2]
2 echo $((1+2))
3 let i=1+2
echo $i


n++ amount to i=$[$i+1]
n-- amount to i=$[$i-1]
n+=2 amount to i=$[$i+2]



边栏推荐
- NAT (network address translation)
- 基于SSM实现的校园新闻发布管理系统
- Esxi virtual machine starts, and the module "monitorloop" fails to power on
- PNA modified polypeptide arms PNA PNA DNA suc aapf PNA suc - (ALA) 3 PNA
- Fix the problem that the paging data is not displayed when searching the easycvr device management list page
- DNA modified zinc oxide | DNA modified gold nanoparticles | DNA coupled modified carbon nanomaterials
- Redis fast learning
- What is special about the rehabilitation orthopedic branch of 3D printing brand?
- What is the reason why dragging the timeline is invalid when playing device videos on the easycvr platform?
- [unity URP] the code obtains the universalrendererdata of the current URP configuration and dynamically adds the rendererfeature
猜你喜欢

基于SSM学生学籍管理系统

Ftx.us launched stock and ETF trading services to make trading more transparent

Speech and language processing (3rd ed. draft) Chapter 2 - regular expression, text normalization, editing distance reading notes

Boostrap

含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制

How can chrome quickly transfer a group of web pages (tabs) to another device (computer)

Matlab drawing (ultra detailed)

PNA polypeptide PNA TPP | GLT ala ala Pro Leu PNA | suc ala Pro PNA | suc AAPL PNA | suc AAPM PNA

PNA肽核酸修饰多肽Suc-Tyr-Leu-Val-pNA|Suc-Ala-Pro-Phe-pNA 11

Variance and covariance
随机推荐
Introduction to the official functions of easyrecovery14 data recovery software
Alibaba cloud SMS authentication third-party interface (fast use)
Disk management and file system
To improve the baby's allergy, take yiminshu. Azg and aibeca love la Beijia work together to protect the growth of Chinese babies
How to make the minimum API bind the array in the query string
基于SSM实现的校园新闻发布管理系统
聊聊大火的多模态
【12】 Understand the circuit: from telegraph to gate circuit, how can we "send messages from thousands of miles"?
关于ES6的新特性
ES6 new features (getting started)
After adding a camera (camera) to the UAV in gazebo, the UAV cannot take off
DNA(脱氧核糖核酸)供应|碳纳米管载核酸-DNA/RNA材料|DNA/RNA核酸修饰磁性纳米颗粒
Boostrap
客户案例 | 聚焦流程体验,助银行企业APP迭代
Shell编程的规范和变量
Some applications of std:: bind and std:: function
Log in to Alibaba cloud server with a key
Add virtual network card and configure OP route in win10
DNA modified zinc oxide | DNA modified gold nanoparticles | DNA coupled modified carbon nanomaterials
DNA research experiment application | cyclodextrin modified nucleic acid cd-rna/dna | cyclodextrin nucleic acid probe / quantum dot nucleic acid probe
