mirror of https://github.com/tLDP/LDP
25 lines
615 B
Bash
25 lines
615 B
Bash
#!/bin/bash
|
|
# recurse.sh
|
|
|
|
# Can a script recursively call itself?
|
|
# Yes, but this is of little or no practical use
|
|
#+ except perhaps as a "proof of concept".
|
|
|
|
RANGE=10
|
|
MAXVAL=9
|
|
|
|
i=$RANDOM
|
|
let "i %= $RANGE" # Generate a random number between 0 and $MAXVAL.
|
|
|
|
if [ "$i" -lt "$MAXVAL" ]
|
|
then
|
|
echo "i = $i"
|
|
./$0 # Script recursively spawns a new instance of itself.
|
|
fi # Each child script does the same, until
|
|
#+ a generated $i equals $MAXVAL.
|
|
|
|
# Using a "while" loop instead of an "if/then" test causes problems.
|
|
# Exercise for the reader: Explain why.
|
|
|
|
exit 0
|