当前位置:网站首页>6 shell tips to make the script no longer amateur
6 shell tips to make the script no longer amateur
2022-07-21 13:19:00 【58 Shen Jian】
Former colleague Wang Xiao has published a book ,《 Starting from scratch Linux Operation and maintenance practice 》, Let him send me a copy .
After reading the book , I said, , Your book is not “ On the tall ” ah , speak Linux, Say command tool set , speak vim, speak shell Programming , speak awk practice ... Want to sell , Don't you have a whole new term of high technology ?
Wang Xiao said , We don't know much about new high-tech terms , You can't scare others , Instead, I flustered myself , It's better to be honest , Let the technical people who love learning have a few gains ...
The following is what I learned from Wang Xiao 6 Practice ( Listen to Wang Xiao , He's with me, too Google learn ), this , It's also the style of his new book .
Voice over : The new book is on the shelves , Discount at the end of the article , Welcome to .
How to make your own shell Not so Amateur ?
Share 6 Practice .
One 、 Start with the following sentence
set -o nounset
By default , Encountered a variable that doesn't exist , Will ignore and continue to execute , And that's often not what you expect , Add this option , It can prevent the consequences from spreading , Terminate execution of script .
Voice over : Some mistakes in variable names , It's going to take a long time to debug , In this way , This kind of hand Miss second detection .
set -o errexit
By default , Encountered execution error , Will skip and continue to execute , And that's often not what you expect , Add this option , It can prevent the consequences from spreading , Terminate execution of script .
Voice over : There are some Linux command , for example rm Of -f Parameters can force errors to be ignored , At this point, the script cannot capture errexit, Such parameters are not recommended in scripts .
These two options , All conform to fail fast Design concept .
Two 、 It is necessary to encapsulate functions
Don't patronize and write down , Encapsulation can improve reuse .
As in the above example :
log()
Simple packaging , Can save a lot of
[$(date +%Y/%m/%d\ %H:%M:%S)]
Duplicate code for .
Voice over : This log() I little interesting , Have you learned ?
meanwhile , Encapsulation can also improve the readability of the code .
As in the above example :
ExtractBashComments
Than
egrep "^#"
It's much more readable .
Voice over : Some friends will say , I don't know English .
3、 ... and 、 Use readonly and local Modifying variables
readonly
seeing the name of a thing one thinks of its function , read-only .
local
Variables within a function .
Don't try to save time , While improving security , Can avoid a lot of people to collapse of the inexplicable mistakes . Is the script written professionally , It's not a high point , But the embodiment of basic skills .
Voice over : It is said that , One C++ What level of programmers , from ta In the code const How often do you use it , Can see .
Four 、 Use $() Instead of `( Back single quotes )
Why? ? Look at the example above and you will understand :
(1)$() Can support embedded ;
(2)$() No need to escape ;
(3) Some Fonts ,`( Back single quotes ) and ’( Single quotation marks ) It's like , It's easy to confuse people ;
5、 ... and 、 Use [[]] Instead of []
Use single brackets :
Use double brackets :
See the difference ?[[]] More in line with the human code :
(1) Avoid escape problems ;
(2) There are many new features ;
New features include, but are not limited to :
|| : Logic or
&& : Logic and
< : String comparison ( There is no need to escape )
== : wildcard (globbing) String comparison
=~ : Regular expressions (regular expression, RegEx) String comparison
It should be noted that , from bash3.2 Start , Neither wildcards nor regular expressions can be enclosed in quotation marks ( therefore , The above example , Quotation marks are literal comparisons ).
So if there's a space in the expression , It has to be stored in a variable , Then compare the wildcard with the regular .
6、 ... and 、echo It's not the only way to debug
It can be used -n Check the syntax of the script .
It can be used -v Tracking the execution of each command in the script .
It can be used -x Tracking the execution of each command in the script , And add expanded information .
Of course , It can also be in scripts , add to
set -o verbose
set -o xtrace
To permanently specify the output debug information .
Voice over : Try more on the machine and you'll see .
Former colleague Wang Xiao's new book , learn Linux, learn shell, learn awk, learn vim, Learn the operation and maintenance tool set ... Not so much “ On the tall ” The noun , Only dry goods knowledge points ...
Scan the code to buy books , Enjoy discounts now
research :
Do you think Linux,shell,awk,vim Is it necessary for engineers to know and master ?
Read the original , Enjoy discounts now .
边栏推荐
- 11.3构造方法
- OpenBMB x 清华NLP:20小时大模型公开课带你从入门到精通
- 【C语言】文件操作
- What is the difference between self built database transmission through dedicated line /vpn gateway / intelligent access gateway?
- 绘图库Matplotlib图形绘制
- 绘图库MatplotlibMatplotlib快速入门
- Guangdong Xinghao sterile injection has passed the certification of the European drug administration and obtained the "quality pass" in the international market
- 【爬虫知识】比lxml和bs4都要好?解析器parsel的使用
- 性能测试:登录接口密码使用rsa加密
- CCF201503-1图像旋转
猜你喜欢
This is probably the most complete summary of web crawlers you have ever seen
【图像处理】Pyefd.elliptic_fourier_descriptors的使用方式
Kubesphere 3.3.0 offline installation tutorial
C语言——扫雷游戏
腾讯云服务器的搭建
C语言——三子棋游戏
前中电技术总监精讲高频面试题:谈谈你对负载均衡的理解
全新出炉:7 月编程语言最新排行榜
Qt编写物联网管理平台45-采集数据转发
Use the add () method in calendar to add and subtract time, obtain the time range, and read the dynamic data dictionary.
随机推荐
【C语言】文件操作
正则表达式获取两个标识中间的内容
C language - Sanzi game
Mathpix公式提取器
华为云数据治理生产线DataArts,让“数据‘慧’说话”
每日刷题记录 (二十九)
VGA设计(原理说明。Verilog代码实现,仿真结果)
Kubesphere 3.3.0 offline installation tutorial
LM386 practical schematic diagram
12.2包 和导入包
shell基础之条件判断
[translation] in depth understanding of modern web browsers (4)
Ugui basic controls part 2
Performance test: use RSA encryption for login interface password
C语言——扫雷游戏
Generate verification code
带你认识一下数仓的分区自动管理
11.3构造方法
Guangdong Xinghao sterile injection has passed the certification of the European drug administration and obtained the "quality pass" in the international market
Xiaohongshu mall whole store commodity API interface (all store commodity interfaces)