当前位置:网站首页>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
边栏推荐
- urllib.error. URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
- 4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?
- Construction and application of super large scale knowledge map of ants
- 【飞控开发基础教程6】疯壳·开源编队无人机-SPI(六轴传感器数据获取)
- [MRCTF2020]babyRSA
- Redis distributed lock
- Unity 实现简单画板画画功能(笔记)
- [Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
- 蚁剑常见报错
- [GWCTF 2019]BabyRSA1
猜你喜欢

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

14、 C pointer explanation (IV): pointer of pointer

BUUCTF-RSA

给网站套上Cloudflare(以腾讯云为例)
![[RoarCTF2019]RSA](/img/0e/8c8371ccf40094e5b03e502d6ae851.png)
[RoarCTF2019]RSA

Explain the idempotence of distributed system in detail

为什么 Redis 集群要使用反向代理? 看这篇就明白了

Xss.haozi.me practice customs clearance

Spark 离线开发框架设计与实现

如果我们是那晚负责修复 B 站崩了的开发人员
随机推荐
软件运维监控有哪些?
BUUCTF-RSA
传奇服中怎么刷装备
[roarctf2019] babyrsa Wilson theorem
15million per day! BYD masks won a US $1billion order in California
【开发教程11】疯壳·ARM功能手机-定时器实验教程
Zcmu--1720: death is like the wind, I want to pretend to force
Senior how to determine the standard of software test completion
【飞控开发基础教程6】疯壳·开源编队无人机-SPI(六轴传感器数据获取)
Use of date
Redefine analysis - release of eventbridge real-time event analysis platform
传奇外网架设教程带图文解说——Gom引擎
Sort sort
Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
Posture recognition and simple behavior recognition based on mediapipe
【zer0pts CTF 2022】 Anti-Fermat
Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
[MRCTF2020]babyRSA
QT with OpenGL (shadow mapping)
BUUCTF-RSA4