Debian Codegraph @

recurse.sh

raw symbols SCIP
1 #!/bin/bash
2 # recurse.sh
3
4 # Can a script recursively call itself?
5 # Yes, but is this of any practical use?
6 # (See the following.)
7
8 RANGE=10
9 MAXVAL=9
10
11 i=$RANDOM
12 let "i %= $RANGE" # Generate a random number between 0 and $RANGE - 1.
13
14 if [ "$i" -lt "$MAXVAL" ]
15 then
16 echo "i = $i"
17 ./$0 # Script recursively spawns a new instance of itself.
18 fi # Each child script does the same, until
19 #+ a generated $i equals $MAXVAL.
20
21 # Using a "while" loop instead of an "if/then" test causes problems.
22 # Explain why.
23
24 exit 0
25
26 # Note:
27 # ----
28 # This script must have execute permission for it to work properly.
29 # This is the case even if it is invoked by an "sh" command.
30 # Explain why.