当前位置:网站首页>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” “”
边栏推荐
- MySQL internal component structure
- 3. integrate eslint and prettier
- Slf4j: failed to load class "org.slf4j.impl.staticloggerbinder"
- Redis docker master-slave mode and sentinel
- How do databases go to the enterprise cloud? Click to view the answer
- Enterprise data center "cloud" transformation solution
- Shenhe thermomagnetic: Super fusion dual active cluster solution for MES system
- JVM garbage collector G1 & ZGC details
- Practice of super integration and transformation of core production business of public funds
- Self service terminal development process
猜你喜欢
Comparison problems encountered in recent study
Flutter 中的 ValueNotifier 和 ValueListenableBuilder
训练一个图像分类器demo in PyTorch【学习笔记】
Practice of super integration and transformation of core production business of public funds
Dart 开发技巧
【Ubuntu-redis安装】
The present situation and challenge of the infrastructure of Yiwen parsing database
MySQL explain
Shenhe thermomagnetic: Super fusion dual active cluster solution for MES system
Distributed things
随机推荐
JWT expiration processing - single token scheme
Mysql database learning 1
Valuenotifier and valuelistenablebuilder in fluent
Numpy (time date and time increment)
小程序手持弹幕的原理及实现(uni-app)
JVM garbage collector G1 & ZGC details
Alibaba billion concurrent projects in architecture
CRF (conditional random field) learning summary
机器学习笔记 九:预测模型优化(防止欠拟合和过拟合问题发生)
ABAP time function
qmlplugindump executable not found.It is required to generate the qmltypes file for VTK Qml
Add / delete query of topic
Cftpconnection:: getfile() download FTP server files and related parameter descriptions
How can we have high performance and simple agility in the enterprise cloud on oracle?
2021-07-26
Enum demo
How to build a private cloud and create a hybrid cloud ecosystem?
OCX child thread cannot trigger event event (forward)
【新书推荐】Deno Web Development
Comparison problems encountered in recent study