当前位置:网站首页>29shell function
29shell function
2022-07-27 01:40:00 【Play Jingwu】
shell function
Definition of function ( Two ways )
function Function name {
command
} // This is a standard way of writing
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-RoVD3uux-1656741104115)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701151123346.png)]
Function name (){// Most commonly used because of the most concise
command }
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-LVAJt58b-1656741104116)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701154853329.png)]
Function function
After the function is defined, it will not execute automatically , You need to call , The advantage is that you can write a piece of function code as a function , Call directly if necessary . It doesn't matter if there are grammatical errors in the definition , If you don't call, you won't report an error . Of course, the ultimate purpose of writing functions is to call , In order to implement a function block .
Function call
Directly define the code block of the function in the script and write the function name to complete the call
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-IkEWFQQg-1656741104117)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701151544013.png)]
Be careful : Function name must be unique , If you define a , Use the same name to define , The second will override the function of the first , There are results you don't want , So be careful not to duplicate your name here !( In the same environment , But different environments , Can be repeated , And the call results in different environments will not be overwritten )
In case of nested calls in other places , You can't write the value of a function directly , Avoid being unable to recognize , You can use a reverse apostrophe , It is equivalent to the result of calling the function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-l3KShwMO-1656741104117)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701155351675.png)]
The scope of the function
stay Shell The execution of the function in the script does not open a new child Shell, But only in the currently defined Shell Effective in the environment . If Shell The variables in the script are not specially set , The default is valid throughout the script . When writing scripts , Sometimes you need to limit the value of a variable inside a function , You can use built-in commands local To achieve . Use of internal variables of functions , It can avoid the influence of variables with the same name inside and outside the function on the script results .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-YmP1mFZp-1656741104117)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701094904074.png)]
shell Variables in scripts are globally valid by default ,local command : Restrict variables to functions and use .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-dYPchIaU-1656741104117)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701095157970.png)]
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-t97dQvx9-1656741104117)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701095454996.png)]
The parameters of the function
Function arguments
1. Sum two numbers
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-plvzEaGo-1656741104118)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701154639061.png)]
Calculate the location variable $1 and $2 And
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-GD2M7CUH-1656741104119)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701160012608.png)]
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-qyUOvN1E-1656741104119)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701160320155.png)]
Factorial
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-tO2LvDBE-1656741104119)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701161016190.png)]
Optimized version : Calculate the factorial of several according to the user's needs
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-VSztfKlt-1656741104119)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701161704277.png)]
Function return value
return: Represents the exit function and returns an exit value , You can use ? change The amount display in The value send use primary be : 1 、 Letter Count One junction beam Just take return return value , because by ? The variable displays the value Usage principle : 1、 Take the return value as soon as the function ends , because ? change The amount display in The value send use primary be :1、 Letter Count One junction beam Just take return return value , because by ? Variable returns only the exit status code of the last command executed , encounter return That is, the end function will not execute further .
2、return The range of values ( Exit status code ) Must be 0~255, If the value exceeds the value, it will be the remainder 256
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-YPLXo2Af-1656741104119)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701173449191.png)]
The return value of the function , use return Custom return code . The return value of the function is the status code of the last command running in the function , The default success is 0, Failure is wrong 0 value , Can pass $? see , But you can customize the return code yourself , use return.
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-6Ep7nNq3-1656741104119)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701162928055.png)]
Local and global variables
Variables defined in the script or not declared as local variables in the function body are global variables , It means at present shell The environment recognizes . If you need this variable to be used only in the function, you can use... In the function local Keyword declaration , In this way, even if there is a variable with the same name outside the function, it doesn't matter , It does not affect the use in the function body . use source If you execute the script, you can see the difference
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-bmohL9sL-1656741104120)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701164337569.png)]
Definition of global variables
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-54mWILBQ-1656741104120)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701165008373.png)]
The scripts for calculating the sum of numbers and the sum of odd and even numbers are merged into one script, and functions are used to call them according to the user's input .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-BQg5Jiwn-1656741104120)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701171544543.png)]
Recursion of a function
The function itself calls itself
List the files in the directory , The catalogue is shown in blue , The file displays hierarchical relationships
[ Outside the chain picture transfer in …(img-BQg5Jiwn-1656741104120)]
Recursion of a function
The function itself calls itself
List the files in the directory , The catalogue is shown in blue , The file displays hierarchical relationships
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-YIbn63MN-1656741104120)(C:\Users\ Ash \AppData\Roaming\Typora\typora-user-images\image-20220701172626238.png)]
边栏推荐
- 【Oracle】获取最近工作日及前N个工作日
- Longest substring without duplicate characters
- [untitled]
- Jenkins -- Basic -- 04 -- install Chinese plug-ins
- ESP8266 STA_Server
- Shell(6)if判断
- Buuctf casual note, exec, easysql, secret file
- Understanding and learning of internal classes
- XPath of software test interview questions
- ESP8266-----JSON----c函数库提供字符串函数
猜你喜欢

Lnmp+discuz Forum

ESP8266 AP_ UDP_ Server

Esp8266 access to cloud platform ----- DNS domain name connection server

Shell(7)case语句

7、 Loop statement
![[SQL injection] extended injection method](/img/a1/d4218281bfc83a080970d8591cc6d4.png)
[SQL injection] extended injection method

Unity twitter login access

Shell(9)函数

ESP8266 AP_UDP_Server

Excel changes the format of scientific counting method into text
随机推荐
Shell脚本——自动部署DNS服务
OJ question of sequence table
Web服务(07)——LNMP一键部署
MySQL system variables and state variables
ESP8266 STA_TCP_Server
[SQL injection] error injection
ESP8266-----SNTP获取网络时间
Lnmp+discuz Forum
正则表达式
Shell编程规范与变量
ESP8266 AP_ MODE
DNS
Introduction to mathematical modeling - from real objects to mathematical modeling [2]
Adding, deleting, checking and modifying dynamic sequence table with C language
[question] what if Yum resources are occupied
SSH和NFS服务
Navicat操作数据库
软件测试面试题之网络基础
Traversing binary trees in non recursive pre -, middle -, and post order
Web server (01) -- Introduction to web server