Probuje zrozumiec dzialanie funkcji rekursywnej / rekurencyjnej.
https://bash.cyberciti.biz/guide/Recursive_function
#!/bin/bash
# fact.sh - Shell script to to find factorial of given command line arg
factorial(){
local i=$1
local f
declare -i i
declare -i f
# factorial() is called until the value of $f is returned and is it is <= 2
# This is called the recursion
[ $i -le 2 ] && echo $i || { f=$(( i - 1)); f=$(factorial $f); f=$(( f * i )); echo $f; }
}
# display usage
[ $# -eq 0 ] && { echo "Usage: $0 number"; exit 1; }
# call factorial
factorial $1
i
#!/bin/bash
# fact.sh - Shell script to to find factorial of given command line arg
factorial(){
local i=$1
local f
declare -i i
declare -i f
# factorial() is called until the value of $f is returned and is it is <= 2
# This is called the recursion
if [ $1 -lt 5 ]
then
echo $1
factorial $(($1 + 1)) ; echo $1
# echo $x
fi
}
# display usage
[ $# -eq 0 ] && { echo "Usage: $0 number"; exit 1; }
# call factorial
factorial $1
Czemu przy debugowaniu ten pierwszy ma kolejny dodatkowy plus przy uruchamianiu kolejnej funkcji, a ten dolny nie ?
$ bash -x ./t3 4
+ '[' 1 -eq 0 ']'
+ factorial 4
+ local i=4
+ local f
+ declare -i i
+ declare -i f
+ '[' 4 -le 2 ']'
+ f=3
++ factorial 3
++ local i=3
...
$ bash -x ./t2 1
+ '[' 1 -eq 0 ']'
+ factorial 1
+ local i=1
+ local f
+ declare -i i
+ declare -i f
+ '[' 1 -lt 5 ']'
+ echo 1
1
+ factorial 2
+ local i=2
+ local f
+ declare -i i
+ declare -i f
+ '[' 2 -lt 5 ']'
+ echo 2
2
...