当前位置:网站首页>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
边栏推荐
- Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
- Why does redis cluster use reverse proxy? Just read this one
- JUC toolkit learning
- Lua basic grammar learning
- Buuctf childrsa Fermat theorem
- JS promotion: array flattening in JS
- The interviewer asked the thread safe list, and I'm not afraid after reading it!
- [NPUCTF2020]EzRSA
- Decrypt the secret of 90% reduction in oom crash~
- BUUCTF-Baby RSA
猜你喜欢

NDK series (6): let's talk about the way and time to register JNI functions

Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further

Explain the idempotence of distributed system in detail

2022年土木,建筑与环境工程国际会议(ICCAEE 2022)

J9数字科普:Sui网络的双共识是如何工作的?
![[RoarCTF2019]RSA](/img/0e/8c8371ccf40094e5b03e502d6ae851.png)
[RoarCTF2019]RSA

4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?

字符流学习14.3
![[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters](/img/05/7029eb1fe36d7ddab2640f07247c81.png)
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters
![[RoarCTF2019]RSA](/img/0e/8c8371ccf40094e5b03e502d6ae851.png)
[RoarCTF2019]RSA
随机推荐
TOGAF10标准读书会首场活动圆满举办,精彩时刻回顾!
传奇服中怎么刷装备
What are the software operation and maintenance monitoring?
C # delegate usage -- console project, which implements events through delegation
Monologue of a software Investor: why don't I pursue fast-growing companies
The first activity of togaf10 standard reading club was successfully held, and the wonderful moments were reviewed!
How to use FTP to realize automatic update of WinForm
JUC toolkit learning
洛谷 P1009 [NOIP1998 普及组] 阶乘之和
解密 OOM 崩溃下降 90% 的秘密~
[RoarCTF2019]babyRSA威尔逊定理
【飞控开发基础教程6】疯壳·开源编队无人机-SPI(六轴传感器数据获取)
NDK series (6): let's talk about the way and time to register JNI functions
[极客大挑战 2019]RCE ME
Features of hardwired controller:
Notes on Shangpin project in shangsilicon Valley (I)
Explain the idempotence of distributed system in detail
MapReduce (III)
Realize today's news website based on native JS
J9数字科普:Sui网络的双共识是如何工作的?