Linux shell programming uses iterative functions to achieve simple factorial! !

2020/11/12
I am new to Linux shell programming, but I am not used to it. Today, I want to use an iterative function to find a simple factorial. After working for a few hours, I think it is necessary to write it down.


1. Title

Program to realize the factorial of n, n=10.

2. Engage with while

This one is relatively simple:

n=10
i=$n
ans=1
while [ $i -gt 1 ] 
do
        let ans=`expr $ans \* $i`
        let i=`expr $i - 1`
done
echo $n"! ="$ans

Of course, you can also use until, these two transformations are very easy, only the judgment conditions are different.

3. Iterative function

When I wrote iterative functions in other languages ​​before, I thought it was very simple, but I wrote it in a shell like this:

#!/bin/bash
function fac()
{
    
    
        n=$1
        if [ $n -eq 1 ]; then
                return 1
        else
                let m=`expr $n - 1`
                s=$(fac $m)
                return $(($n*$s))
        fi
}

echo $(fac 10)

Insert picture description here
Of course, this is not the initial version, this is the one that I have changed a lot of sides and reported the least errors at the end. After arriving here, I was puzzled, and finally found a blog. It suddenly dawned on me that the blog link is as follows:

https://www.cnblogs.com/duanxz/p/4661767.html

This predecessor's example is to make the function return a string, and I want the result returned by the function to multiply the value. The predecessor finally wrote:

The return value of the Shell function can only be an integer value, which is generally used to indicate whether the function is executed successfully or not. 0 indicates success, and other values ​​indicate failure. Therefore, it is not appropriate to use the function return value to return the result of the function execution. If you want to return a calculation result abruptly, such as a string, you will often get an error message: "numeric argument required".
If you must let the function return one or more values, you can define a global variable, the function assigns the calculation result to the global variable, and then by accessing the global variable elsewhere in the script, one or more executions of that function "return" can be obtained It turned out.
Summarize the methods for obtaining the return value of a function:
1) Use a variable to receive the return value of the function, and the function uses echo and other standard output to print out what is to be returned.
2) $?to the receiver function execution state, but $?to immediately after the function call.

Yes, use global variables:

#!/bin/bash
s=1  #全局变量
fac()
{
    
    
        n=$1
        if [ $n -eq 1 ] ; then
                s=$(($s*1))
        else
                let m=`expr $n - 1`
                s=$(($s*$n))
                fac $m
        fi
}
fac 10
echo $s

Insert picture description here
It is worth mentioning that the variable is a global variable by default-when the function body does not clearly indicate that the variable is a local variable ( local var), it is a global variable.


Summarize the methods for obtaining the return value of a function:
1) Use a variable to receive the return value of the function, and the function uses echo and other standard output to print out what is to be returned.
2) $?to the receiver function execution state, but $?to immediately after the function call.

Guess you like

Origin blog.csdn.net/Gou_Hailong/article/details/109662252