⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recurnolocvar.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Recursion Without Local Variables</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Functions"HREF="functions.html"><LINKREL="PREVIOUS"TITLE="Local Variables"HREF="localvar.html"><LINKREL="NEXT"TITLE="Aliases"HREF="aliases.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="localvar.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 23. Functions</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="aliases.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="RECURNOLOCVAR">23.3. Recursion Without Local Variables</A></H1><P>A function may recursively call itself even without use of	      local variables.</P><DIVCLASS="EXAMPLE"><HR><ANAME="HANOI"></A><P><B>Example 23-14. The Towers of Hanoi</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#! /bin/bash   2&nbsp;#   3&nbsp;# The Towers Of Hanoi   4&nbsp;# Bash script   5&nbsp;# Copyright (C) 2000 Amit Singh. All Rights Reserved.   6&nbsp;# http://hanoi.kernelthread.com   7&nbsp;#   8&nbsp;# Last tested under bash version 2.05b.0(13)-release   9&nbsp;#  10&nbsp;#  Used in "Advanced Bash Scripting Guide"  11&nbsp;#+ with permission of script author.  12&nbsp;#  Slightly modified and commented by ABS author.  13&nbsp;  14&nbsp;#=================================================================#  15&nbsp;#  The Tower of Hanoi is a mathematical puzzle attributed to  16&nbsp;#+ Edouard Lucas, a nineteenth-century French mathematician.  17&nbsp;#  18&nbsp;#  There are three vertical posts set in a base.  19&nbsp;#  The first post has a set of annular rings stacked on it.  20&nbsp;#  These rings are flat disks with a hole drilled out of the center,  21&nbsp;#+ so they can slip over the posts.  22&nbsp;#  The rings have different diameters, and they stack in descending  23&nbsp;#+ order, according to size.  24&nbsp;#  The smallest ring is on top, and the largest on the bottom.  25&nbsp;#  26&nbsp;#  The task is to transfer the stack of rings  27&nbsp;#+ to one of the other posts.  28&nbsp;#  You can move only one ring at a time to another post.  29&nbsp;#  You are permitted to move rings back to the original post.  30&nbsp;#  You may place a smaller ring atop a larger one,  31&nbsp;#+ but *not* vice versa.  32&nbsp;#  Again, it is forbidden to place a larger ring atop a smaller one.  33&nbsp;#  34&nbsp;#  For a small number of rings, only a few moves are required.  35&nbsp;#+ For each additional ring,  36&nbsp;#+ the required number of moves approximately doubles,  37&nbsp;#+ and the "strategy" becomes increasingly complicated.  38&nbsp;#  39&nbsp;#  For more information, see http://hanoi.kernelthread.com.  40&nbsp;#  41&nbsp;#  42&nbsp;#         ...                   ...                    ...  43&nbsp;#         | |                   | |                    | |  44&nbsp;#        _|_|_                  | |                    | |  45&nbsp;#       |_____|                 | |                    | |  46&nbsp;#      |_______|                | |                    | |  47&nbsp;#     |_________|               | |                    | |  48&nbsp;#    |___________|              | |                    | |  49&nbsp;#   |             |             | |                    | |  50&nbsp;# .--------------------------------------------------------------.  51&nbsp;# |**************************************************************|  52&nbsp;#          #1                   #2                      #3  53&nbsp;#  54&nbsp;#=================================================================#  55&nbsp;  56&nbsp;  57&nbsp;E_NOPARAM=66  # No parameter passed to script.  58&nbsp;E_BADPARAM=67 # Illegal number of disks passed to script.  59&nbsp;Moves=        # Global variable holding number of moves.  60&nbsp;              # Modifications to original script.  61&nbsp;  62&nbsp;dohanoi() {   # Recursive function.  63&nbsp;    case $1 in  64&nbsp;    0)  65&nbsp;        ;;  66&nbsp;    *)  67&nbsp;        dohanoi "$(($1-1))" $2 $4 $3  68&nbsp;        echo move $2 "--&#62;" $3  69&nbsp;	let "Moves += 1"  # Modification to original script.  70&nbsp;        dohanoi "$(($1-1))" $4 $3 $2  71&nbsp;        ;;  72&nbsp;    esac  73&nbsp;}  74&nbsp;  75&nbsp;case $# in  76&nbsp;1)  77&nbsp;    case $(($1&#62;0)) in     # Must have at least one disk.  78&nbsp;    1)  79&nbsp;        dohanoi $1 1 3 2  80&nbsp;        echo "Total moves = $Moves"  81&nbsp;        exit 0;  82&nbsp;        ;;  83&nbsp;    *)  84&nbsp;        echo "$0: illegal value for number of disks";  85&nbsp;        exit $E_BADPARAM;  86&nbsp;        ;;  87&nbsp;    esac  88&nbsp;    ;;  89&nbsp;*)  90&nbsp;    echo "usage: $0 N"  91&nbsp;    echo "       Where \"N\" is the number of disks."  92&nbsp;    exit $E_NOPARAM;  93&nbsp;    ;;  94&nbsp;esac  95&nbsp;  96&nbsp;# Exercises:  97&nbsp;# ---------  98&nbsp;# 1) Would commands beyond this point ever be executed?  99&nbsp;#    Why not? (Easy) 100&nbsp;# 2) Explain the workings of the workings of the "dohanoi" function. 101&nbsp;#    (Difficult)</PRE></TD></TR></TABLE><HR></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="localvar.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="aliases.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Local Variables</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="functions.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Aliases</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -