Imported from ../bash-2.02.tar.gz.
[platform/upstream/bash.git] / examples / scripts / hanoi.bash
1 # Towers of Hanoi in bash
2 #
3 # cribbed from the ksh93 book, example from exercises on page 85
4 #
5 # Chet Ramey
6 # chet@po.cwru.edu
7
8 hanoi() # n from to spare
9 {
10     typeset -i nm1=$1-1
11     ((nm1>0)) && hanoi $nm1 $2 $4 $3
12     echo "Move disc $2 to $3"
13     ((nm1>0)) && hanoi $nm1 $4 $3 $2
14 }
15
16 case $1 in
17 [1-9])
18       hanoi $1 1 2 3;;
19 *)    echo "${0##*/}: Argument must be from 1 to 9"
20       exit 1;;
21 esac