当前位置:网站首页>Shell programming specifications and variables
Shell programming specifications and variables
2022-07-28 00:02:00 【Jijijijijijijiji】
Catalog
2、Shell Script application scenarios ( Basics )
3、 ... and 、 Shell The composition of the script
5、 ... and 、 Pipe operator operation
1、 Interactive hardware devices
7、 ... and 、 Classification of variables
1、 Classification of variables
4、 Use quotation marks when assigning values
8、 ... and 、read Command to get input
3、 The operation of integer variables
One 、Shell Script Overview
1、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
2、Shell Script application scenarios ( Basics )
Repetitive operations
Interactive tasks
Batch transactions
Service running status monitoring
Scheduled task execution
......
3、Shell The role of
Command translator ,“ The interpreter ”
Between systems Kernel and user Between , Responsible for interpreting the command line

Two 、 User login Shell
After default login shell The procedure is /bin/bash
Different shell The internal order of , The operating environment will be different

3、 ... and 、 Shell The composition of the script
Script statement ( Interpreter ): If the first act “#!/bin/bash” , Indicates that the code statement below this line is passed /bin/bash Program to execute ,#!/bin/bash Is the default interpreter , There are other types of interpreters , such as #!/usr/bin/python、#!/usr/bin/expect.
Annotation information : With “#” The opening statement is expressed as a comment message , The annotated statement will not be executed when the script is run .
Executable statement : such as echo command , For output “ ” String between .
Four 、Shell Script execution
1、 Method 1 : Command to specify the path , It is required that the document must have x jurisdiction
chmod -x /root/liji.sh
Specify the absolute path :/root/liji.sh
Specify the relative path :./first.sh

2、 Method 2 : Appoint shell To explain the script , It is not required that the document must have x jurisdiction
sh Script path :sh liji.sh
source Script path :.liji.sh or soure liji.sh: or bash liji.sh

5、 ... and 、 Pipe operator operation
The pipe symbol "|" The command output on the left , As input to the command on the right ( Deal with people ), Multiple pipes can be used in the same command line .
stay shell Script , Pipeline operation is usually used to filter the required customs information .

combination awk Print using spaces for segmentation , Intercept the status of the running socket

6、 ... and 、 Redirect
1、 Interactive hardware devices
| type | Device file | Document description number | Default device |
| The standard input | /dev/stdin | 0 | keyboard |
| standard output | /dev/stdout | 1 | Monitor |
| Standard error output | /dev/stderr | 2 | Monitor ,2, |
2、 Redirection operation
| type | The operator | purpose |
| Redirect input | < | Read data from the specified file |
| Redirect output | > | Put the standard output result , preservation , To the specified file |
| >> | Append the output result to the end of the specified file | |
| Standard error output | 2> | Put the error message preservation To the specified file , And cover the original content |
| 2>> | Put the error message preservation To the specified file , Don't cover the original content | |
| Mixed output | &> | Standard output , Standard errors are saved to the same file |
| 2>&1 | Redirect standard error output to standard output |
Example ①:> Indicates coverage ;>> Indicates append

Example ②

Example ③: Standard error output

7、 ... and 、 Classification of variables
1、 Classification of variables
It is used to store specific parameters that the system and users need to use
environment variable : System maintenance , Application settings working environment
Positional variable : Pass parameters to the script through the command line
Custom variable : Defined by the user , Modify and use
Predefined variables :bash A class of variables built into , Cannot be modified directly
2、 Define a new variable
Format : Variable name = A variable's value
Variable naming rule : Start with a letter or underline , Case sensitive

3、 Custom variable
Bash The variable operation in is relatively simple , Unlike other high-level programming languages (( Such as c/C++、Java etc. ) So complicated . When defining a new variable , In general, you don't need
Make a statement in advance , Instead, specify the variable name directly and assign it to the initial value ( Content ) that will do
Format : Variable name = A variable's value
Variable name : A place where data is temporarily stored
There is no space on either side of the equal sign . Variable names should start with letters or underscores , Do not include special characters in the name ( Such as +、 One 、*、/、.、 ?、8、&、# etc. )
use echo View and reference the values of variables :
By adding a leading symbol before the variable name "$”, You can reference the value of a variable , Use echo Command can view variables , Can be in one echo View multiple variable values at the same time in the command
When variable names are easily confused with other characters immediately following them , Need to add “{}” Cover up , Otherwise, the correct variable name cannot be determined , For undefined variables , Will display as empty .
4、 Use quotation marks when assigning values
Double quotes : Allowed to pass through $ 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 .`..` and $(..) The same effect
8、 ... and 、read Command to get input
Purpose : Let users themselves , Assign a value to a variable , flexible
grammar read [ Options ] Variable name
1、 Common options :
| Options | paraphrase |
| -p | Define information to prompt users |
| -n | Define the number of characters ( Limit the length of variable values ) |
| -s | No display ( Don't show user input ) |
| -t | Define timeout , The default unit is seconds ( Limit the timeout of user input variable value ) |
2、 Variable action range
By default , The newly defined variable is only in the current shell Effective in the environment , So it's called a local variable , When entering a subroutine or a new subroutine shell Environmental time , Local variables will no longer be used
It can be done by internal command export Exports the specified variable as a global variable , Make user-defined variables in all sub shell Can continue to be used in the environment .
Format 1:export Variable name
Format 2:export Variable name = A variable's value
3、 The operation of integer variables
| expression | give an example |
| $(()) | echo $((9+9)) |
| $[] | echo $[15-9] |
expr | expr 10/5 |
| let | i=1;let i+=10 be equal to let i=i+10 |
i++ amount to i=$[$i+1]
i-- amount to i=$[$i-1]
i+=10 amount to i=$[$i+10]
Nine 、 environment variable
Use env The command can view the environment variables of the current working environment
Variable user Represents the user name ,home Indicates the user's add directory ,lang Represents the language and character set ,pwd Indicates the current working directory , Variable path Represents the default search path for executable programs .
Ten 、 A read-only variable
Used when the variable value cannot be modified
边栏推荐
- What is the prospect of low code development? Are you really optimistic about low code development?
- xss.haozi.me练习通关
- 【zer0pts CTF 2022】 Anti-Fermat
- 基于mediapipe的姿态识别和简单行为识别
- Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card
- Why do I need to wait for 2msl?
- Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
- [NCTF2019]babyRSA1
- 资深如何确定软件测试结束的标准
- BUUCTF-Baby RSA
猜你喜欢

Decrypt the secret of 90% reduction in oom crash~

BUUCTF-RSA4

UE4 official AEC blueprint case course learning notes

蚁剑常见报错

The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?

Key points of data management

JUC toolkit learning
![[GWCTF 2019]BabyRSA1](/img/31/6727fd04be13ddd6bd46969fe2c50f.png)
[GWCTF 2019]BabyRSA1

Latex common summary (2): input matrix (input matrix, diagonal matrix, equations, etc.)

2022 summer vacation daily question (5)
随机推荐
主数据管理理论与实践
Xss.haozi.me practice customs clearance
Redis 哈希Hash底层数据结构
JS promotion: array flattening in JS
Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
网站被黑后处理方法及删除批量恶意代码的方法步骤
重新定义分析 - EventBridge 实时事件分析平台发布
[GWCTF 2019]BabyRSA1
29. Learn the stacked column chart of highcharts using percentage
What is the difference between concurrency and parallelism?
JS array copy speed test 220320
BUUCTF-Baby RSA
Spark 离线开发框架设计与实现
(12) 51 Single Chip Microcomputer -- use DS18B20 to measure the outdoor temperature in Gongjiang West
软件运维监控有哪些?
Realization of gobang man-machine combat
BUUCTF-RSA roll
Use a grayscale filter
[RoarCTF2019]babyRSA威尔逊定理
基于原生js实现今日新闻网站