当前位置:网站首页>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” “”
边栏推荐
- 【新书推荐】Cleaning Data for Effective Data Science
- Differences and relationships among hyper convergence, software defined storage (SDS), distributed storage and server San
- Pass anonymous function to simplification principle
- Bluetooth BT RF test (forwarding)
- Practice of super integration and transformation of core production business of public funds
- Enterprise data center "cloud" transformation solution
- Pytorch graduate warm LR installation
- Machine learning note 9: prediction model optimization (to prevent under fitting and over fitting problems)
- Oracle cross database replication data table dblink
- Flume learning 4
猜你喜欢

Pytorch graduate warm LR installation

Distributed ID

抽象类和接口

prometheus 监控之 ntp_exporter

Cb/s Architecture - Implementation Based on cef3+mfc

Financial private cloud infrastructure scheme evaluation (Architecture and storage)

Oracle cross database replication data table dblink

仿照微信Oauth2.0接入方案

Comparison problems encountered in recent study

About the smart platform solution for business hall Terminal Desktop System
随机推荐
Plan the IT technology route for the new year? Let's learn about Gartner infrastructure hype cycle
Galaxy Kirin server-v10 configuration image source
2021-07-26
Enum demo
Critical applications and hyper converged infrastructure: the time has come
Network based dynamic routing protocol (OSPF)
近期学习遇到的比较问题
Eight sorts (II)
Eight sorts (I)
Hospital integration platform super fusion infrastructure transformation scheme
八大排序(二)
机器学习笔记 九:预测模型优化(防止欠拟合和过拟合问题发生)
UltraEdit delete empty line method
【AGC】构建服务3-认证服务示例
Self service terminal handwritten Chinese character recognition input method library tjfink introduction
安装和使用
Idea shortcut key settings
【新书推荐】MongoDB Performance Tuning
JVM garbage collector G1 & ZGC details
关于字符串的split和join操作