当前位置:网站首页>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
边栏推荐
- Chapter 3 business function development (deletion and modification of clue remarks)
- 平凡的快乐
- Quickly master nodejs installation and getting started
- How much is the report development cost in the application system?
- 当Synchronized遇到这玩意儿,有个大坑,要注意
- 3d智能工厂工艺流转可视化交互展示应用优点
- Three implementation methods of Servlet
- Exploration and practice of network security vulnerability management
- 主从复制及其原理
- 裂开了,一次连接池参数导致的雪崩问题
猜你喜欢

Remember error scheduler once Asynceventqueue: dropping event from queue shared causes OOM

When I look at the source code, what am I thinking?

MySQL驱动中关于时间的坑

What happens if you have to use ArrayList in multithreading?

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

FPGA刷题——存储器(RAM和FIFO的Verilog实现)

STM32C8T6编码器电机测速与arduino光电模块测速

Object based real-time spatial audio rendering - Dev for dev column

Understand the evolution of redis architecture in one article

HTTP缓存
随机推荐
PHP幸运抽奖系统带后台源码
[upload picture 2-cropable]
CUDA details GPU architecture
工程经济学简答题
代码实现 —— 多项式的最大公因式(线性代数)
How much is the report development cost in the application system?
Explain the four asynchronous solutions of JS in detail: callback function, promise, generator, async/await
Quickly master nodejs installation and getting started
Excel uses countif statistics
FPGA刷题——存储器(RAM和FIFO的Verilog实现)
STM32C8T6编码器电机测速与arduino光电模块测速
如何把thinkphp5的项目迁移到阿里云函数计算来应对流量洪峰?
Continuous learning / life long learning
Driverless obstacle avoidance technology
time_ Wait and close_ Cause of wait
Double write consistency of MySQL and redis
Esbuild Bundler HMR
2022/07/28 学习笔记 (day18) 常用API
详解异步任务:任务的状态及生命周期管理
Full solution of 3D model format | including more than 70 kinds of RVT, 3ds, DWG, FBX, IFC, osgb, obj, etc