idioms for returning multiple values in shell scripting

后端 未结 10 873
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 20:56

Are there any idioms for returning multiple values from a bash function within a script?

http://tldp.org/LDP/abs/html/assortedtips.html describes how to echo multipl

10条回答
  •  一整个雨季
    2020-12-29 21:30

    In order version of Bash which doesn't support nameref (introduced in Bash 4.3-alpha) I may define helper function in which the return value is assigned to the given variable. It's sort of like using eval to do the same kind of variable assignment.

    Example 1

    ##  Add two complex numbers and returns it.
    ##  re: real part, im: imaginary part.
    ##
    ##  Helper function named by the 5th positional parameter
    ##  have to have been defined before the function is called.
    complexAdd()
    {
        local re1="$1" im1="$2" re2="$3" im2="$4" fnName="$5" sumRe sumIm
    
        sumRe=$(($re1 + $re2))
        sumIm=$(($im1 + $im2))
    
        ##  Call the function and return 2 values.
        "$fnName" "$sumRe" "$sumIm"
    }
    
    main()
    {
        local fooRe='101' fooIm='37' barRe='55' barIm='123' bazRe bazIm quxRe quxIm
    
        ##  Define the function to receive mutiple return values
        ##  before calling complexAdd().
        retValAssign() { bazRe="$1"; bazIm="$2"; }
        ##  Call comlexAdd() for the first time.
        complexAdd "$fooRe" "$fooIm" "$barRe" "$barIm" 'retValAssign'
    
        ##  Redefine the function to receive mutiple return values.
        retValAssign() { quxRe="$1"; quxIm="$2"; }
        ##  Call comlexAdd() for the second time.
        complexAdd "$barRe" "$barIm" "$bazRe" "$bazIm" 'retValAssign'
    
        echo "foo = $fooRe + $fooIm i"
        echo "bar = $barRe + $barIm i"
        echo "baz = foo + bar = $bazRe + $bazIm i"
        echo "qux = bar + baz = $quxRe + $quxIm i"
    }
    
    main
    

    Example 2

    ##  Add two complex numbers and returns it.
    ##  re: real part, im: imaginary part.
    ##
    ##  Helper functions
    ##      getRetRe(), getRetIm(), setRetRe() and setRetIm()
    ##  have to have been defined before the function is called.
    complexAdd()
    {
        local re1="$1" im1="$2" re2="$3" im2="$4"
    
        setRetRe "$re1"
        setRetRe $(($(getRetRe) + $re2))
    
        setRetIm $(($im1 + $im2))
    }
    
    main()
    {
        local fooRe='101' fooIm='37' barRe='55' barIm='123' bazRe bazIm quxRe quxIm
    
        ##  Define getter and setter functions before calling complexAdd().
        getRetRe() { echo "$bazRe"; }
        getRetIm() { echo "$bazIm"; }
        setRetRe() { bazRe="$1"; }
        setRetIm() { bazIm="$1"; }
        ##  Call comlexAdd() for the first time.
        complexAdd "$fooRe" "$fooIm" "$barRe" "$barIm"
    
        ##  Redefine getter and setter functions.
        getRetRe() { echo "$quxRe"; }
        getRetIm() { echo "$quxIm"; }
        setRetRe() { quxRe="$1"; }
        setRetIm() { quxIm="$1"; }
        ##  Call comlexAdd() for the second time.
        complexAdd "$barRe" "$barIm" "$bazRe" "$bazIm"
    
        echo "foo = $fooRe + $fooIm i"
        echo "bar = $barRe + $barIm i"
        echo "baz = foo + bar = $bazRe + $bazIm i"
        echo "qux = bar + baz = $quxRe + $quxIm i"
    }
    
    main
    

提交回复
热议问题