本文介绍了Shell函数不会返回大于255的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   sum()
  {
     return $(($1+$2))

  }

   read a b
   sum $a $b
   echo $?

当我们传递a = 255的值和b = 36的值时,ans将是35,为什么?

when we pass the value for a=255 and for b=36 the ans will be 35 why?

推荐答案

每个人都指出,shell函数不能返回大于255的值.

As everybody pointed out, shell function cannot return a value greater that 255.

从函数中获取值的常见方法是将它们存储在变量中,如下所示:

The common way to get values out of functions is to store them in a variable like so:

#! /bin/sh

sum() {
    local __res=$3
    local res=$(($1 + $2))
    eval $__res="'$res'"
}


sum 25000 5000 total

echo $total

这篇关于Shell函数不会返回大于255的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:22