当前位置:网站首页>Shell script quick start-01
Shell script quick start-01
2022-07-29 02:38:00 【51CTO】
1 Shell summary
1.1 Why study shell
1.2 shell summary
2 Shell Parser
3 Shell Script entry
3.1 Script format
3.2 Create a Shell Script
3.3 Multi command processing
4 Shell The variables in the
4.1 System variables
4.2 Custom variable
4.3 Special variables :$n
4.4 Special variables :$#
4.5 Special variables $*
4.6 Special variables $?
5 Operator
6 conditional
1 Shell summary
1.1 Why study shell?
- Need to understand the preparation of operation and maintenance personnel shell Programming
- Occasionally, it needs to be written shell Management cluster , So as to improve the development efficiency
1.2 shell summary

Shell Be situated between Linux Between the kernel and the outer application
Shell It's a command line interpreter , It receives applications / User command , And then invoke the kernel of the operating system
Shell Is a powerful programming language , Easy to write , Easy to debug , Strong flexibility .
2. Shell Parser
- Linux Provided Shell The interpreter has
- sh and bash The relationship between ,sh yes bash The soft links
- Centos The default interpreter is bash
3 Shell Script entry
3.1 Script format
With #!/bin/bash start
3.2 Create a Shell Script
- Create a shell Script ,, And the output helloworld
- Write script content
- perform shell Script
use sh
use bash
Take an absolute path to see
use ./ Give it a try , I can't find out , The reason is that it was originally through sh perhaps bash The parser executes , But now it's your permission to execute , No, x jurisdiction
Therefore, modify permissions
3.3 Multi command processing
stay /home/fenfen/ Create one in the directory banzhang.txt stay banzhang.txt Add to file “I love lili”
- establish shell Script
- shell The script content
- Execute the script and see the results
4 Shell The variables in the
4.1 System variables
- Common system variables
- $HOME: View the user's home directory
- $PWD: View current directory
- $SHELL: Check the default parser
- #USER: View the current user
- The principle is to encapsulate these as variables
4.2 Custom variable
- Basic grammar : Variable = value
- Be careful : The value of a variable can be changed , The assignment will change
- A=1 No space in the middle
- Revoke variables :unset Variable
- Declare a static read-only variable :readonly Variable
- But not to unset
- Variable definition rules
Variable names may consist of letters 、 Numbers and underscores , But it can't start with a number , Environment variable name is recommended to be capitalized
No spaces on both sides of equal sign
Variable default type is string type , No direct numerical operation
- Value of variable if there is a space , You need to use double quotation marks and single quotation marks
- Global variables
If you promote a variable to a global variable export, For other purposes Shell Program usage
- shell Add... To the script
- But because it doesn't belong to the same process , also D It's a local variable , So use shell Can't print out
- So upgrade to global variables
4.3 Special variables :$n
$n:n Is the number , also 0 Indicates the name of the script ,1-9 Represents the first to ninth parameters , More than ten parameters need to be enclosed in braces
${10}, Generally not more than 5 individual
example
- Create a sh file
- shell Script a
- Under execution , Will come out $0
- Give a parameter to execute
4.4 Special variables :$#
Basic grammar :$# Get the number of all input parameters , Often used in loops
example
- Shell Script
- Execution includes $# Script results for
- Give me a parameter
4.5 Special variables $*
- Basic grammar
$*: This variable represents all the parameters on the command line , Treat all parameters as a sort
[email protected]: This variable also represents all the parameters on the command line , Just treat each parameter differently
- example
- write in shell Script
- Take a look at it , In the end, although the result is the same , But [email protected] Indicates that the kernel is still one by one
4.6 Special variables $?
- Basic grammar : The return status of the last command executed . If the value of this variable is 0, The last command on the table is executed correctly , If the value of this variable is not 0, Then the last command is incorrect
- example
5 Operator
- Basic grammar
- $(( Arithmetic expression ))
- $[ Arithmetic expression ]
- expr +,-,*,/,% Addition, subtraction, multiplication, division and remainder expr Space between operators
- example
- 2+3 First calculate and remember ` once
- Use [] Calculation
6 conditional
- Basic grammar :[ condition ] condition There should be a space before and after
If the condition is not empty, it is true,[] Returns the false
- Common judgment conditions
- String comparison
- -lt Less than
- -le Less than or equal to
- -eq be equal to
- -gt Greater than
- -ge Greater than or equal to
- -ne It's not equal to
- File permission judgment
- -r Have read permission
- -w Have the right to write
- -x Have the authority to execute
- Judge according to the document type
- -f The file exists and is a regular file file
- -e File exists existence
- -d The file exists and is a directory directory
- Multi condition judgment
- && It means the previous one is successful , Only then implements the latter
- || Indicates that the previous one failed , Only then implements the latter
- example
- 23 Greater than or equal to 22
- helloworld Whether you have write permission
- Whether the file of the directory exists
- Multi condition judgment
边栏推荐
- Three implementation methods of Servlet
- How to use RPA to achieve automatic customer acquisition?
- Code implementation - the greatest common factor of polynomials (linear algebra)
- time_wait和close_wait产生原因
- Source code and display of 18 classic programs in C language vs2019
- where、having、group by、order by,is null,not in,子查询,delete,日期函数
- When synchronized encounters this thing, there is a big hole, so be careful
- STM32F103 learn the steps and template fool tutorial of 1-keil5 project establishment
- 结合Retrofit 改造OKHttp 缓存
- 7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
猜你喜欢

Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译

XSS range (II) xss.haozi

MySQL驱动中关于时间的坑

物联网组件

Quickly master nodejs installation and getting started

Read the recent trends of okaleido tiger and tap the value and potential behind it

Code implementation - the greatest common factor of polynomials (linear algebra)

多边形点测试

漫画算法_小灰灰面试

会议OA之会议通知
随机推荐
Split, an avalanche caused by connection pool parameters
I was stunned by this question that I browsed 746000 times
Experiment 2: Arduino's tricolor lamp experiment
ROCBOSS开源微社区轻论坛类源码
Why is redis fast? Message queue, single thread
3D intelligent factory process flow visualization interactive display application advantages
网络安全漏洞管理的探索与实践
工程经济学知识点总结
Redis master-slave mode, sentinel cluster, fragment cluster
FPGA刷题——存储器(RAM和FIFO的Verilog实现)
裂开了,一次连接池参数导致的雪崩问题
Never pass a request to an asynchronous thread. There is a hole
Continuous learning / life long learning
Production scheme and advantages of online 3D digital exhibition hall
XSS靶场(二)xss.haozi
ES6 detailed quick start!
[upload pictures can be cut-1]
Rust list (VEC) replication
[quality] code quality evaluation standard
Summary of knowledge points of Engineering Economics