Records : 429
Scenario : Shell script function application, function definition, function call, function parameter passing.
Version : CentOS Linux release 7.9.2009.
1. Ordinary functions
The example ordinary function has no input parameters and no return value.
1.1 Script
Screenplay name: b2023051701.sh
Script content:
#!/bin/bash
#1.定义函数-带function关键字
function f1(){
for city in "Hangzhou" "Suzhou" "Shanghai"
do
echo "城市名称: ${city}"
done
}
#2.定义函数-不带function关键字
f2(){
for province in 'Zhejiang' 'Jiangsu'
do
echo "省名称: ${province}"
done
}
#3.调用函数
echo "f1输出结果:"
result=$(f1)
echo "${result}"
echo "f2输出结果:"
result=$(f2)
echo "${result}"
1.2 Execution and output
Execute the command: bash b2023051701.sh
Output result:
f1输出结果:
城市名称: Hangzhou
城市名称: Suzhou
城市名称: Shanghai
f2输出结果:
省名称: Zhejiang
省名称: Jiangsu
2. The function uses return to return the value
The return value of the Shell function can only be an integer between 0 and 255, and only 0 indicates success, and other values indicate failure.
When using the Shell function, the actual meaning of using return to return the value is not very obvious.
2.1 Script
Screenplay name: b2023051702.sh
Script content:
#!/bin/bash
#1.定义函数,使用return返回值
function f1(){
echo '计算两数相乘结果'
echo '输入第一个数字:'
read firstNum
echo '输入第二个数字:'
read secondNum
return $((${firstNum}*${secondNum}))
}
#2.调用函数f1
f1
echo "f1计算结果: $? "
2.2 Execution and output
Execute the command: bash b2023051702.sh
Output result:
计算两数相乘结果
输入第一个数字:
33
输入第二个数字:
3
f1计算结果: 99
3. The function uses the echo output as the return value
The function uses the echo output as the return value.
3.1 Script
Screenplay name: b2023051703.sh
Script content:
#!/bin/bash
#1.定义函数,使用echo输出作为返回值
function f2(){
firstNum=1024
secondNum=3
echo $((firstNum*secondNum))
}
#2.调用函数f2
result=$(f2)
echo "f2计算结果:"
echo "${result}"
3.2 Execution and output
Execute command: bash b2023051703.sh
Output result:
f2计算结果:
3072
4. Functions use global variables as return values
Functions use global variables as return values.
4.1 Script
Screenplay name: b2023051704.sh
Script content:
#!/bin/bash
#1.定义函数,使用全局变量作为函数返回值
result=0 #全局变量存放函数结果
function f3(){
firstNum=1024
secondNum=4
result=$((firstNum*secondNum))
}
#2.调用函数f3
f3
echo "f3计算结果:"
echo "${result}"
4.2 Execution and output
Execute the command: bash b2023051704.sh
Output result:
f3计算结果:
4096
5. Function passing parameters
Functions pass parameters using placeholders.
5.1 Script
Screenplay name: b2023051705.sh
Script content:
#!/bin/bash
# 1.定义函数,使用占位符定义参数
function f1(){
echo "country: $1"
echo "Province: $2"
echo "City: $3"
}
#2.调用函数,传递参数
result=$(f1 "China" "Zhejiang" "Hangzhou")
echo "执行结果: "
echo "${result}"
5.2 Execution and output
Execute the command: bash b2023051705.sh
Output result:
执行结果:
country: China
Province: Zhejiang
City: Hangzhou
6. The function uses return to return the value
Functions pass parameters using placeholders. Input parameters from the script execution entry.
6.1 Script
Screenplay name: b2023051706.sh
Script content:
#!/bin/bash
# 1.定义函数,使用占位符定义参数
function f1(){
echo "country: $1"
echo "Province: $2"
echo "City: $3"
}
#2.调用函数,传递参数
v1=$1
v2=$2
v3=$3
result=$(f1 ${v1} ${v2} ${v3})
echo "执行结果: "
echo "${result}"
6.2 Execution and output
Execution command: bash b2023051706.sh Hangzhou, Zhejiang, China
Output result:
执行结果:
country: 中国
Province: 浙江
City: 杭州
Above, thanks.
May 17, 2023