shell 脚本函数封装参数接收返回值
shell 脚本
[root@Bing-Data-node2 cluster]# vim fun.sh
#!/bin/bash
# 无参函数
function test(){
echo "hello function!"
}
test
s=""
#两个参数带返回值
function add(){
echo "接收到的两个参数是${!1}${!2}"
echo "Shell 函数的返回值只能是一个介于 0~255 之间的整数,其中只有 0 表示成功,其它值都表示失败。"
s="cctv"
return
}
add 1 2
echo $s
执行结果
[root@Bing-Data-node2 cluster]# sh fun.sh
hello function!
接收到的两个参数是12
Shell 函数的返回值只能是一个介于 0~255 之间的整数,其中只有 0 表示成功,其它值都表示失败。
cctv
[root@Bing-Data-node2 cluster]#