当前位置:网站首页>Shell_ 04_ Shell script
Shell_ 04_ Shell script
2022-07-06 16:50:00 【Coke loving w】
Shell_04_Shell Script
Shell Script
Script : A file consisting of multiple commands ( It can be viewed through a text editor )
1)Shell Belong to Linux Built in scripts ( Common scripts :JS、JSP、PHP、SQL、Python etc. )
2) Script cycle : One Shell The process of file operation completion
3)Shell Scripts are divided into : Simple writing ( The accumulation of simple commands )、 Complex writing ( Programming )
To write Shell Script
To write Shell The script should follow the following rules :
(1) File name specification
1) File name naming format : file name .sh
//.sh The suffix is to distinguish ordinary files , And .sh yes Bash Default suffix
(2) Code specification
1)Shell The first line in the script file must be :#!/bin/bash
// Indicates the Shell Interpreter
2) Script to “exit N” Form ends (N Is the returned value ), And N The scope is 0~255
// It can be done by ${?} get N Value , Easy to adjust
// if N The value of exceeds 255, Then divide the value by 256 The remainder of is used as the return status code
Common exit status codes
| Status code | explain |
|---|---|
| 0 | The command ended successfully |
| 1 | Invalid parameter |
| 2 | Not suitable Shell command |
| 126 | Command execution failed ( No authority ) |
| 127 | No specified command |
| 128 | Invalid exit parameter |
| 128+x | x Signal related errors |
| 130 | By Ctrl+C End |
| 255 | Exit status code out of normal range |
(3) Annotation specifications
1) Notes should include : Script function 、 Version information and environment required for operation
// It is better to include : author 、 Contact information 、 History of script and special commands of script
(4) Call specification
1) The calling of the script requires the user to have the execution permission of the file (x);
2) Script files can be called by :
Script path 、sh Script name 、source Script name
// The first two methods are both sub Shell Execute script in , and source Then in the parent Shell Execute script in
(5) Special symbols
| Special symbols | meaning |
|---|---|
| # | Annotation symbols |
| ; | Command separator |
| & | Commands become background tasks |
Such as : Use Shell Script program , Output “Hello world!”
1)a. Create with .sh Suffix script file 
1)b. Write relevant programs ( The first line specifies the interpreter ), And save 
1)c. Give script execution permission , Call script 
2)a. Create and write hello.sh Script files 
2)b. The contents of the script file are as follows :
// It is strongly recommended to use the second way , It can ensure the maintenance and update of subsequent scripts
// Scripting tools are best used Vim instead of Vi(Vim There is additional syntax detection function )
trap command : Set when Shell The response of the script after receiving the signal
Command format 1:trap command The signal
1) After receiving the specified signal , Run the specified command ;
2) Multiple trap On command , Then only run this trap Command related parts , To execute the specified command ;
Such as : Use Ctrl+C Termination with trap Command the script that captures the signal
1) To write test23.sh Script files 
2) call test23.sh Script files 
Command format 2:trap Single dash or double dash The signal // Restore the function of the signal generator
1) If the signal is captured before recovery , According to trap The command specified by the command executes
Such as : adopt trap After the command captures the signal , Restore the function of the signal generator
1) To write test24.sh Script files 
2) call test24.sh Script files 
call Shell Script
sh command :shell command interpreter
Command format :sh Options File path
1) The most commonly used is to make the system with Bash Function execution file
2)sh Is to execute the script file in the subprocess
| Options | meaning |
|---|---|
| -v | perform shell Before script , First output the content to the screen |
| -n | Only right shell Script syntax checking , Not implemented |
| -x | Yes shell The script tracks each statement |
// The most commonly used -x Option to modify the script file debug
Such as : Yes hello.sh Document carried out shell Script tracking and debugging 
source command : At present Bash Execute the document in the environment and take effect immediately
Command format :source File path
1)source The command is also “.” Click command ( They're exactly the same )
2)source The command is often used to execute the initialization file just modified , Make the document effective immediately
3)source Is to execute the script file in the parent process
Other ways to call Shell Script :
1) Call with absolute path or relative path ;
2) adopt Shell The absolute path of the interpreter and the path of the script file are called ;
// Such as :/bin/bash File path
边栏推荐
- ~78 radial gradient
- Li Kou leetcode 280 weekly match
- 我在字节跳动「修电影」
- LeetCode1556. Thousand separated number
- 视频压缩编码和音频压缩编码基本原理
- Spark独立集群Worker和Executor的概念
- Market trend report, technical innovation and market forecast of China's desktop capacitance meter
- 字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
- 7-10 punch in strategy
- LeetCode 1545. Find the k-th bit in the nth binary string
猜你喜欢

~79 Movie card exercise

第五章 Yarn资源调度器

Usage of insert() in vector

Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog

第三章 MapReduce框架原理

~Introduction to form 80
![[unsolved]7-14 calculation diagram](/img/33/39802c0106f2bef4b9979e5ae12b83.jpg)
[unsolved]7-14 calculation diagram

Chapter 1 overview of MapReduce

Basic principles of video compression coding and audio compression coding

两个礼拜速成软考中级软件设计师经验
随机推荐
LeetCode 1641. Count the number of Lexicographic vowel strings
~79 Movie card exercise
Record the error reason
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
这116名学生,用3天时间复刻了字节跳动内部真实技术项目
力扣leetcode第 280 场周赛
Chapter 6 datanode
LeetCode 1566. Repeat the pattern with length m at least k times
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
LeetCode 1551. Minimum operand to make all elements in the array equal
Market trend report, technical innovation and market forecast of double-sided foam tape in China
Solve the problem that intel12 generation core CPU single thread only runs on small cores
~85 transition
「博士毕业一年,我拿下 ACL Best Paper」
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
Cartesian tree (modified)
Codeforces Round #771 (Div. 2)
~70 row high
~87 animation
Research Report on market supply and demand and strategy of double drum magnetic separator industry in China