You are on page 1of 3

Prev

Advanced Bash-Scripting Guide: Chapter 23. Functions

Next

23.3. Recursion Without Local Variables


A function may recursively call itself even without use of local variables. !a"ple 23-#$. %he %o&ers o' (anoi
#! /bin/bash # # The Towers Of Hanoi # Bash script # Copyright (C) 2000 Amit ingh! A"" #ights #eser$e%! # http&//hanoi!'erne"threa%!com # # (ast teste% )n%er bash $ersion 2!0*b!0(+,)-re"ease # # .se% in /A%$ance% Bash cripting 0)i%e/ #1 with permission of script a)thor! # "ight"y mo%ifie% an% commente% by AB a)thor! #22222222222222222222222222222222222222222222222222222222222222222# # The Tower of Hanoi is a mathematica" p)33"e attrib)te% to #1 4%o)ar% ()cas5 a nineteenth-cent)ry 6rench mathematician! # # There are three $ertica" posts set in a base! # The first post has a set of ann)"ar rings stac'e% on it! # These rings are f"at %is's with a ho"e %ri""e% o)t of the center5 #1 so they can s"ip o$er the posts! # The rings ha$e %ifferent %iameters5 an% they stac' in %escen%ing #1 or%er5 accor%ing to si3e! # The sma""est ring is on top5 an% the "argest on the bottom! # # The tas' is to transfer the stac' of rings #1 to one of the other posts! # 7o) can mo$e on"y one ring at a time to another post! # 7o) are permitte% to mo$e rings bac' to the origina" post! # 7o) may p"ace a sma""er ring atop a "arger one5 #1 b)t 8not8 $ice $ersa! # Again5 it is forbi%%en to p"ace a "arger ring atop a sma""er one! # # 6or a sma"" n)mber of rings5 on"y a few mo$es are re9)ire%! #1 6or each a%%itiona" ring5 #1 the re9)ire% n)mber of mo$es appro:imate"y %o)b"es5 #1 an% the /strategy/ becomes increasing"y comp"icate%! # # 6or more information5 see http&//hanoi!'erne"threa%!com! # # # !!! !!! !!! # ; ; ; ; ; ; # <;<;< ; ; ; ;

# ;<<<<<; ; ; ; ; # ;<<<<<<<; ; ; ; ; # ;<<<<<<<<<; ; ; ; ; # ;<<<<<<<<<<<; ; ; ; ; # ; ; ; ; ; ; # !--------------------------------------------------------------! # ;88888888888888888888888888888888888888888888888888888888888888; # #+ #2 #, # #22222222222222222222222222222222222222222222222222222222222222222# 4<=O>A#A?2@@ # =o parameter passe% to script! 4<BAA>A#A?2@B # C""ega" n)mber of %is's passe% to script! ?o$es2 # 0"oba" $ariab"e ho"%ing n)mber of mo$es! # ?o%ifications to origina" script! %ohanoi() D # #ec)rsi$e f)nction! case E+ in 0) FF 8) %ohanoi /E((E+-+))/ E2 EG E, echo mo$e E2 /--H/ E, "et /?o$es 12 +/ # ?o%ification to origina" script! %ohanoi /E((E+-+))/ EG E, E2 FF esac I case E# in +) case E((E+H0)) in # ?)st ha$e at "east one %is'! +) %ohanoi E+ + , 2 echo /Tota" mo$es 2 E?o$es/ e:it 0F FF 8) echo /E0& i""ega" $a")e for n)mber of %is's/F e:it E4<BAA>A#A?F FF esac FF 8) echo /)sage& E0 =/ echo / Jhere K/=K/ is the n)mber of %is's!/ e:it E4<=O>A#A?F FF esac # # # # # 4:ercises& --------+) Jo)"% comman%s beyon% this point e$er be e:ec)te%L Jhy notL (4asy) 2) 4:p"ain the wor'ings of the wor'ings of the /%ohanoi/ f)nction!

(Aiffic)"t)

Prev Local ariables

Home !p

Next Aliases

You might also like