linux shell函数和返回值

文章介绍了shell函数的返回值限制(只能是0-255的状态值),如何通过echo传递非数字字符串,使用$(函数调用)和$()的区别,以及for循环和switchcase的用法。强调了函数返回的是状态码而非数值,可通过echo获取函数输出。
摘要由CSDN通过智能技术生成

shell函数可以有返回值,但是只能返回0-255作为状态值,不能返回字符串,字符串可以通过其他方式传递给调用者

1.shell函数的return

小于255的值

~/Desktop$ cat b.sh
getLastSize() {
size=2
return $size
}

getLastSize
lastSize=$?
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: 2

大于255的值

~/Desktop$ cat b.sh
getLastSize() {
size=256
return $size
}

getLastSize
lastSize=$?
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: 0

基本是对256取余的返回值

~/Desktop$ cat b.sh
getLastSize() {
size=513
return $size
}

getLastSize
lastSize=$?
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: 1

2.返回字符串

通过$(函数调用和参数),通过$()调用,函数中的echo不会打印到控制台,直接调用函数,则会调用控制台

~/Desktop$ cat b.sh
getLastSize() {
size=513
echo $size
}

lastSize=$(getLastSize)
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: 513

返回非纯数字的字符串

~/Desktop$ cat b.sh
getLastSize() {
size='aaa513'
echo $size
}

lastSize=$(getLastSize)
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: aaa513

多行echo

~/Desktop$ cat b.sh
getLastSize() {
echo 'hello world'
size='aaa513'
echo $size
}

lastSize=$(getLastSize)
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: hello world
aaa513

多行echo只获取最后一个输出

############################################
~/Desktop$ cat b.sh
getLastSize() {
echo 'hello world'
size='aaa513'
echo $size
}

lastSize=$(getLastSize | tail -1)
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: aaa513

#############################################
~/Desktop$ cat b.sh
getLastSize() {
echo 'hello world'
size='aaa513'
echo $size
}

lastSize=`getLastSize | tail -1`
echo "last size: $lastSize"

~/Desktop$ ./b.sh
last size: aaa513

3.函数参数

~/Desktop$ cat b.sh
function printArg(){
   echo '$*='$*
   echo '$@='$@
   echo '$#='$#
   echo '$0='$0
   echo "\$1=$1"
   echo '$2='$2
   echo '$$='$$
   echo $2+$3
}

printArg 'a' 'b' 'c' 1 2 3

echo 'printArg done'

ret=$(printArg 1 3 'a' 'b' 5 | tail -1)
echo 'printArg done'
echo "ret=$ret"

~/Desktop$ ./b.sh
$*=a b c 1 2 3
$@=a b c 1 2 3
$#=6
$0=./b.sh
$1=a
$2=b
$$=663215
b+c
printArg done
printArg done
ret=3+a

参数说明

   $0;# 获取执行命令:./b.sh
   $*;# 获取所有参数列表
   $@;# 获取所有参数列表
   $#;# 获取参数列表个数
   $1;# 获取第一个参数
   $2;# 获取第二个参数
   $$;# 获取进程id

 4.流程控制

for循环

~/Desktop$ cat b.sh
function testfor(){
	for (( x=1; x<$1; x++)); do
		echo get number: $x
	done
}

testfor 5

~/Desktop$ ./b.sh
get number: 1
get number: 2
get number: 3
get number: 4

switch case;在sell中是case in

~/Desktop$ cat b.sh
function getDaysInYearMonth(){
    days=0;
    year=$1;month=$2;
    case $month in
         4 | 6 | 9 | 11)
           days=30;;
         2)
           if [ $((year%4)) -eq 0 -a $((year%100)) -ne 0 -o $((year%400)) -eq 0 ];then
                  days=29;
           else
                  days=28;
           fi;;
         *)
           days=31;;
    esac;
    return $days;
}

getDaysInYearMonth 2020 2
days=$?
echo 2020/02 has day:$days

getDaysInYearMonth 2022 3
days=$?
echo 2022/03 has day:$days

~/Desktop$ ./b.sh
2020/02 has day:29
2022/03 has day:31

规则

# case val in
#     match1)
#        dosomething
#        doother;;
#    match2 | match2)
#        dosomething;;
#    *) #match all
#        dosomething;;
# esac;
#
#

 注意:

函数return的是状态码,不是数值

函数可以通过echo让调用者调用$()获取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值