shell 函数 返回值
来源:互联网 发布:windows模拟器 编辑:程序博客网 时间:2024/05/22 01:40
#!/bin/bash
#函数返回值:
#1. return(0-255) 通过$?获得
#2. echo 通过`function`获得,非结果echo到文件或者/dev/null ,返回值是数组,字符串,大于255的整数,可以使用echo
#3. 声明全局变量 declare 返回值是数组,字符串,大于255的整数,可以使用echo
# echo 最好用,返回数组也要依靠echo
declare g_res
function test1 {
local a=1
local b=2
local c=$[$a+$b]
return $c
}
function test2 {
local a=4
if [ $a -eq 4 ]
then
echo "four"
else
echo " null"
fi
}
function test3 {
g_res=1
}
function test4 {
local i
local arr
for (( i=0; i<5; ++i))
do
arr[$i]=$i
done
echo ${arr[*]}
}
test1
echo "function test1 return : "$?
test2Result=`test2`
echo "function test2 return :"$test2Result
test3
echo "function test3 return : "$g_res
test4Res=`test4`
#函数返回值:
#1. return(0-255) 通过$?获得
#2. echo 通过`function`获得,非结果echo到文件或者/dev/null ,返回值是数组,字符串,大于255的整数,可以使用echo
#3. 声明全局变量 declare 返回值是数组,字符串,大于255的整数,可以使用echo
# echo 最好用,返回数组也要依靠echo
declare g_res
function test1 {
local a=1
local b=2
local c=$[$a+$b]
return $c
}
function test2 {
local a=4
if [ $a -eq 4 ]
then
echo "four"
else
echo " null"
fi
}
function test3 {
g_res=1
}
function test4 {
local i
local arr
for (( i=0; i<5; ++i))
do
arr[$i]=$i
done
echo ${arr[*]}
}
test1
echo "function test1 return : "$?
test2Result=`test2`
echo "function test2 return :"$test2Result
test3
echo "function test3 return : "$g_res
test4Res=`test4`
echo "function test4 return : "$test4Res
运行结果:
function test1 return : 3
function test2 return :four
function test3 return : 1
function test4 return : 0 1 2 3 4
阅读全文
0 0
- Shell函数返回值
- Shell 函数返回值
- shell函数返回值
- Shell函数返回值
- shell 函数返回值
- shell 函数 返回值
- Linux Shell函数返回值
- Linux Shell函数返回值
- Linux Shell函数返回值
- Linux Shell函数返回值 .
- shell中函数返回值
- shell 函数返回值问题
- Linux Shell 函数返回值
- shell 函数的返回值
- shell 函数返回值问题
- 【Shell】shell带有返回值的函数
- shell 函数返回值的方法
- shell 函数的参数和返回值
- Spring Boot:(四)统一日志处理
- C++中的argc和argv
- 【Python】【矩阵】协方差矩阵;特征值;特征向量
- HDU-3371 Connect the Cities
- 动态测试方法运行时间的工具BTrace的简单应用
- shell 函数 返回值
- baidu
- 一个简单的进程间mutex和读写锁类
- 【LintCode-167】链表求和(Java实现)
- 《Python学习手册》学习笔记(22)之第22章模块代码编写基础(关键词:编程语言/Python/模块/import/from)
- js数组复制的方法
- Daily Practice_Cloud Computer
- Leetcode Minimum path sum
- 设计模式-单利模式