27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

# Thanks to Stephane Chazelas for providing this instructive example.<br />

exit 0<br />

[52] The return command is a Bash builtin.<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

[53] Herbert Mayer defines recursion as "...expressing an algorithm by using a simpler version of that same<br />

algorithm..." A recursive function is one that calls itself.<br />

[54] Too many levels of recursion may crash a script with a segfault.<br />

#!/bin/bash<br />

recursive_function ()<br />

{<br />

(( $1 < $2 )) && f $(( $1 + 1 )) $2;<br />

# As long as 1st parameter is less than 2nd,<br />

#+ increment 1st and recurse.<br />

}<br />

recursive_function 1 50000 # Recurse 50,000 levels!<br />

# Segfaults, of course.<br />

# Recursion this deep might cause even a C program to segfault,<br />

#+ by using up all the memory allotted to the stack.<br />

# Thanks, S.C.<br />

exit 0 # This script will not exit normally.<br />

[55] However, aliases do seem to expand positional parameters.<br />

[56] This does not apply to csh, tcsh, and other shells not related to or descended from the classic Bourne<br />

shell (sh).<br />

[57] The entries in /dev provide mount points for physical and virtual devices. These entries use very little<br />

drive space.<br />

Some devices, such as /dev/null, /dev/zero, and /dev/urandom are virtual. They are not<br />

actual physical devices and exist only in software.<br />

[58] A block device reads and/or writes data in chunks, or blocks, in contrast to a character device, which<br />

acesses data in character units. Examples of block devices are a hard drive and CD ROM drive. An<br />

example of a character device is a keyboard.<br />

[59] Certain system commands, such as procinfo, free, vmstat, lsdev, and uptime do this as well.<br />

[60] Rocky Bernstein's Bash debugger partially makes up for this lack.<br />

[61] By convention, signal 0 is assigned to exit.<br />

[62] Setting the suid permission on the script itself has no effect.<br />

[63] In this context, " magic numbers" have an entirely different meaning than the magic numbers used to<br />

designate file types.<br />

[64] ANSI is, of course, the acronym for the American National Standards Institute.<br />

[65] See Marius van Oers' article, Unix Shell Scripting Malware, and also the Denning reference in the<br />

bibliography.<br />

[66] Chet Ramey promises associative arrays (a Perl feature) in a future Bash release.<br />

[67] This is the notorious "flog it to death" technique.<br />

[68] Those who can, do. Those who can't... get an MCSE.<br />

[69] If no address range is specified, the default is all lines.<br />

Appendix J. Copyright 445

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!