kernelthread.com

Bourne (Again) Shell

The Towers of Hanoi as a BASH script.

The GNU page still says that: "Bash is the shell, or command language interpreter, that will appear in the GNU operating system." Well, prospects are not too bright for the GNU operating system, but Bash is a very popular shell nonetheless. Bash is an sh-compatible shell that incorporates useful features from ksh and csh.

Visit the GNU BASH home page for more information.

#! /bin/bash # # The Towers Of Hanoi # Bash script # Copyright (C) 2000 Amit Singh. All Rights Reserved. # http://hanoi.kernelthread.com # # Last tested under bash version 2.05b.0(13)-release # dohanoi() { case $1 in 0) ;; *) dohanoi "$(($1-1))" $2 $4 $3 echo move $2 "-->" $3 dohanoi "$(($1-1))" $4 $3 $2 ;; esac } case $# in 1) case $(($1>0)) in 1) dohanoi $1 1 3 2 exit 0; ;; *) echo "$0: illegal value for number of disks"; exit 2; ;; esac ;; *) echo "usage: $0 N" exit 1; ;; esac

Download

hanoi.bash