当前位置:网站首页>Shell script functions
Shell script functions
2022-06-30 09:54:00 【thengsd】
Here's the catalog title
One 、Shell function
The function lists the parts of the program that need to be used many times , Then give this part a name , All other repeated calls to this part of the code can only call this name ( Similar to alias ). When you need to modify this part of the duplicate code , You only need to change a copy of the code in the function body to realize the call modification .
The advantages of functions
Define the same program segment as a function , It can reduce the amount of code in the whole program .
Increase the readability of the program , And easy to manage .
When modifying, you only need to modify the function , There is no need to look everywhere in the program for changes
Two 、Shell Function format

3、 ... and 、 Function return value
1、return
return Represents the exit function and returns an exit value , You can use $ ? The variable displays the value
Usage principle
Take the return value as soon as the function ends , because $? Variable returns only the exit status code of the last command executed
Exit status code must be 0~255, When exceeded, the value will be divided by 256 Remainder 
2、echo
echo You can return any string result
Usually used to return data , for example : String value or list value
Four 、 Function arguments
Parameter passing is to pass the parameters in the body code to the function body , Position parameters are generally used $n($1、$2、$3…)
#!/bin/bash
sum() {
s=$[$1 + $2]
echo $s
}
read -p “ Please enter the first parameter :” first
read -p “ Please enter the second parameter :” second
sum $first $second
5、 ... and 、 The scope of function variable
Function in Shell In the script, only in the current Shell Effective in the environment
Shell Variables in scripts are globally valid by default
Restrict variables to functions and use local command
#!/bin/bash
Part() {
echo “ This is the first one inside i Value : i " i = 10 e c h o " this yes Inside Ministry The first Two individual i Of value : i" i=10 echo " This is the second one inside i Value : i"i=10echo" this yes Inside Ministry The first Two individual i Of value :i”
local i=15
echo “ This is the third one inside i Value : $i”
}
i=5
echo " This is the outside i Value : i " P a r t e c h o " this yes Letter Count Of board That's ok End i Of value i" Part echo " This is the end of function execution i Value i"Partecho" this yes Letter Count Of board That's ok End i Of value i
6、 ... and 、 recursive
1、 Factorial
The factorial of a positive integer is the product of all positive integers less than or equal to the number
fact() {
if [ 1 − e q 1 ] t h e n e c h o 1 e l s e l o c a l t e m p = 1 -eq 1 ] then echo 1 else local temp= 1−eq1]thenecho1elselocaltemp=[ 1 − 1 ] l o c a l r e s u l t = 1 - 1] local result= 1−1]localresult=(fact $temp)
echo $[$1 * KaTeX parse error: Expected 'EOF', got '}' at position 12: result] fi }̲ read -p " Please enter …(fact $n)
echo $result
2、 Recursive directory
Display all directories and files in this directory ( Include directories and files under subdirectories )
#!/bin/bash
list_files () {
for f in ls $1
do
if [ -d “ 1 / 1/ 1/f” ]
then
echo “+ 2 2 2f”
list_files “ 1 / 1/ 1/f” " $2"
else
echo “- 2 2 2f”
fi
done
}
list_files “/var/log” “”
边栏推荐
- IDC released the report on China's software defined storage and hyper convergence market in the fourth quarter of 2020, and smartx hyper convergence software ranked first in the financial industry
- thrift简单使用
- 布隆过滤器
- Recommend a very easy-to-use network communication framework HP socket
- Framework program of browser self-service terminal based on IE kernel
- Pass anonymous function to simplification principle
- Configuring MySQL for error reporting
- utils session&rpc
- Abstract classes and interfaces
- How to build an all-in-one database cloud machine that meets the needs of information innovation?
猜你喜欢

Follow the wechat oauth2.0 access scheme

Dart development skills

Oracle cross database replication data table dblink
![[new book recommendation] cleaning data for effective data science](/img/42/1258694fc55a178a666391669a67ad.png)
[new book recommendation] cleaning data for effective data science

Hospital integration platform super fusion infrastructure transformation scheme

Numpy (data type)

JVM tuning tool introduction and constant pool explanation

Eight sorts (I)

CRF (conditional random field) learning summary

NER – Named Entity Recognition Summary
随机推荐
About the smart platform solution for business hall Terminal Desktop System
(zero) most complete JVM knowledge points
How to reduce the delay in live broadcast in the development of live broadcast source code with goods?
Cftpconnection:: getfile() download FTP server files and related parameter descriptions
Small program development journey
3. integrate eslint and prettier
Idea shortcut key settings
Xlnet (generalized autorefressive trainingfor language understanding) paper notes
Distributed ID
Demo of guavacache
Numpy (constant)
目标检测yolov5开源项目调试
【新书推荐】Cleaning Data for Effective Data Science
IDC released the report on China's software defined storage and hyper convergence market in the fourth quarter of 2020, and smartx hyper convergence software ranked first in the financial industry
2021-11-15
【新书推荐】Deno Web Development
Enterprise data center "cloud" transformation solution
JVM notes (III): analysis of JVM object creation and memory allocation mechanism
布隆过滤器
Based on svelte3 X desktop UI component library svelte UI