source:
vendor/bash/3.1/examples/scripts/hanoi.bash
| Last change on this file was 3228, checked in by , 19 years ago | |
|---|---|
| File size: 400 bytes | |
| Rev | Line | |
|---|---|---|
| [3228] | 1 | # Towers of Hanoi in bash |
| 2 | # | |
| 3 | # cribbed from the ksh93 book, example from exercises on page 85 | |
| 4 | # | |
| 5 | # Chet Ramey | |
| 6 | # [email protected] | |
| 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 |
Note:
See TracBrowser
for help on using the repository browser.
