当前位置:网站首页>Shell programming core technology "three"
Shell programming core technology "three"
2022-07-04 19:24:00 【Wu_ Candy】
Today I will share how to write a Shell Script , And master the preparation Shell The necessary knowledge of script .
Want to write a Shell Script , Necessary knowledge required : 1、 The first thing you need to master is annotation , Annotate with # start , Used to increase script readability ; 2、 The second is parameters , We need to pass in parameters to the script and parse it ; 3、 Finally, function encapsulation , And master how the script is executed and debugged .
notes
1、 First, let's look at the notes :
Use vim Create a file , You can also use it vs code Wait for other editors ,vs code Syntax highlighting is supported , It's also very good , Input vim /tmp/test_001.sh Instruction created test_001.sh file .
2、 Next, start writing scripts :
Press i Key to enter edit mode , And enter comments # this is a test script! Used to tell you what this script is for , Of course, comments are not necessary , Just for readability , meanwhile Shell And will not execute comment statements .
Parameters
When the script starts executing , We need to master the default parameter resolution rules of the system . When passing a parameter to a script , How is it parsed ?
1、$0 Indicates the program being executed , That is, the current script ;
2、1、2 Respectively represents the transmitted number 1 And the first 2 Parameters ,Shell Only supported by default 9 Parameters , If you need to support more parameters, you can use shift;
3、@、* Represents all the parameters , But does not contain
4、${#*}、${#@} Indicates the number of parameters ;
5、{*:1:3}、{*:
interpretative statement :001、@ And * The difference is as follows :
Use command :sh /tmp/test_difference.sh The results are as follows :
You can see that without quotation marks , Both return the parameters passed in , But with quotation marks , here * Take the parameter as a whole string ( Single string ) return ,@ Return each parameter as a string
002、${#*} And ${#@} Can be used as follows :
The results are as follows :
It can be seen from the results that both are the number of parameters
003、{*:1:3}、{*:
As shown in the figure above , You know :{*:1:3} It means taking the front 3 The value of parameters ,{*:
Next, continue to write scripts :
As shown in the figure , And print a0=0、a1=1 a2=2, as well as @ and *. The command I use to execute the script is :sh /tmp/test_001.sh, In the execution result 0 Is the current file name , But at this time, the parameter has no value , The number of parameters is empty .
We enter parameters a,b Pass it to the script , You can see that the first parameter in the output result is a, The second parameter is b, This is the parsing rule of parameters .
function
Finally, the function , The function is named () Heel {} The enclosed structure consists of , Function can realize some function encapsulation , At the same time, the function also supports parameter parsing logic similar to script .
As shown in the figure above , Define a name :helloworld Of function , Pass through in function if Statement to determine the first parameter $1 Is it equal to python, If equal to, print python auto tester. And then call helloworld function , And pass in a name :python Parameters of , System printout python auto tester.
We can also encapsulate the above functions directly input on the command line , Put in shell Script and call , adopt elif Judge $1 Is it equal to java, If it is equal to java Just print java auto tester.
The execution result of the calling function is as follows :
Execute and pass in python Parameters , You can see the final print python auto tester.
If the passed parameter is :java when , Then print out :java auto tester
Friendship tips :“ The way of immeasurable testing ” Original works , Welcome to exchange , It is forbidden for a third party to reprint the article without displaying its source .
边栏推荐
- 2022 ByteDance daily practice experience (Tiktok)
- Go microservice (II) - detailed introduction to protobuf
- 大佬们,求助一下,我用mysql cdc 2.2.1(flink 1.14.5)写入kafka,设置
- 2022养生展,健康展,北京大健康展,健康产业展11月举办
- The kth largest element in the array
- 问下各位大佬有用过cdc直接mysql to clickhouse的么
- 2019年蜀山区第十五届青少年信息学竞赛
- ThreadLocal原理与使用
- 使用SSH
- 数组中的第K个最大元素
猜你喜欢

Rookie post station management system based on C language

Scala基础教程--19--Actor

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

从实时应用角度谈通信总线仲裁机制和网络流控
![[uniapp] uniapp development app online Preview PDF file](/img/11/d640338c626249057f7ad616b55c4f.png)
[uniapp] uniapp development app online Preview PDF file

Bi skills - permission axis

Oracle with as ORA-00903: invalid table name 多表报错

Basic tutorial of scala -- 16 -- generics

MXNet对GoogLeNet的实现(并行连结网络)

神经网络物联网是什么意思通俗的解释
随机推荐
整理混乱的头文件,我用include what you use
prometheus安装
Scala basic tutorial -- 18 -- set (2)
基于NCF的多模块协同实例
国元期货是正规平台吗?在国元期货开户安全吗?
发送和接收IBM WebSphere MQ消息
从实时应用角度谈通信总线仲裁机制和网络流控
Unity adds a function case similar to editor extension to its script, the use of ContextMenu
Scala基础教程--16--泛型
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
Scala基础教程--17--集合
Caché JSON 使用JSON适配器
ThreadLocal原理与使用
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
物联网应用技术的就业前景和现状
Basic tutorial of scala -- 16 -- generics
The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
6.26cf simulation match B: solution to array reduction problem
Process of manually encrypt the mass-producing firmware and programming ESP devices
每日一题(2022-07-02)——最低加油次数