From 07176b086544847babf43a288b26cdda16c6f371 Mon Sep 17 00:00:00 2001 From: gferg <> Date: Mon, 21 Mar 2005 13:51:11 +0000 Subject: [PATCH] updated --- LDP/guide/docbook/abs-guide/Change.log | 112 ++++ LDP/guide/docbook/abs-guide/README | 1 + LDP/guide/docbook/abs-guide/abs-guide.sgml | 599 +++++++++++++----- LDP/guide/docbook/abs-guide/arith-ops.sh | 4 +- LDP/guide/docbook/abs-guide/arith-tests.sh | 1 + LDP/guide/docbook/abs-guide/blot-out.sh | 5 +- LDP/guide/docbook/abs-guide/cannon.sh | 24 +- LDP/guide/docbook/abs-guide/col-totaler.sh | 4 +- LDP/guide/docbook/abs-guide/ex13.sh | 2 +- LDP/guide/docbook/abs-guide/ex14.sh | 12 +- LDP/guide/docbook/abs-guide/ex16.sh | 6 +- LDP/guide/docbook/abs-guide/ex1a.sh | 2 +- LDP/guide/docbook/abs-guide/ex24.sh | 1 + LDP/guide/docbook/abs-guide/ex29.sh | 3 +- LDP/guide/docbook/abs-guide/ex30.sh | 4 +- LDP/guide/docbook/abs-guide/ex36.sh | 1 + LDP/guide/docbook/abs-guide/ex44.sh | 4 +- LDP/guide/docbook/abs-guide/ex47.sh | 7 +- LDP/guide/docbook/abs-guide/ex57.sh | 1 - LDP/guide/docbook/abs-guide/ex6.sh | 4 +- LDP/guide/docbook/abs-guide/ex68.sh | 8 +- LDP/guide/docbook/abs-guide/ex78.sh | 3 +- LDP/guide/docbook/abs-guide/ex79.sh | 5 +- LDP/guide/docbook/abs-guide/ex9.sh | 14 +- LDP/guide/docbook/abs-guide/hexconvert.sh | 1 + LDP/guide/docbook/abs-guide/hypotenuse.sh | 1 + LDP/guide/docbook/abs-guide/is-spammer.sh | 2 +- LDP/guide/docbook/abs-guide/keypress.sh | 4 +- LDP/guide/docbook/abs-guide/length.sh | 7 +- LDP/guide/docbook/abs-guide/letter-count.sh | 95 ++- LDP/guide/docbook/abs-guide/life.sh | 6 +- LDP/guide/docbook/abs-guide/line-number.sh | 5 +- LDP/guide/docbook/abs-guide/obj-oriented.sh | 4 +- LDP/guide/docbook/abs-guide/param-sub.sh | 4 +- LDP/guide/docbook/abs-guide/redir2.sh | 25 +- LDP/guide/docbook/abs-guide/rnd.sh | 36 +- .../docbook/abs-guide/script-detector.sh | 10 + LDP/guide/docbook/abs-guide/tempfile-name.sh | 3 + LDP/guide/docbook/abs-guide/tree.sh | 6 +- LDP/guide/docbook/abs-guide/unalias.sh | 1 + .../docbook/abs-guide/unprotect_literal.sh | 2 +- LDP/guide/docbook/abs-guide/unset.sh | 2 +- LDP/guide/docbook/abs-guide/upperconv.sh | 2 +- LDP/guide/docbook/abs-guide/usb.sh | 1 + LDP/guide/docbook/abs-guide/viewdata.sh | 9 +- LDP/guide/docbook/abs-guide/weirdvars.sh | 4 +- LDP/guide/docbook/abs-guide/wgetter2.bash | 36 +- LDP/guide/docbook/abs-guide/what.sh | 1 + 48 files changed, 768 insertions(+), 326 deletions(-) diff --git a/LDP/guide/docbook/abs-guide/Change.log b/LDP/guide/docbook/abs-guide/Change.log index 3e9f5ffb..54016d60 100644 --- a/LDP/guide/docbook/abs-guide/Change.log +++ b/LDP/guide/docbook/abs-guide/Change.log @@ -6,6 +6,118 @@ http://personal.riverusers.com/~thegrendel/Change.log ------------------------------------------------------------------------ +Version 3.3 +Raspberry release, 03/20/05 + +1) In the "Starting Off With a Sha-Bang" chapter: + Amended footnote [3] to mention a cat / here document. + (Thank you, Fabian Kreutz.) + Fixed typo in "ex1a.sh" example. + (Thank you, Preetam.) + Added Larry Wall epigraph. + Changed "Important" block to "Tip," and added "generalizing scripts" + paragraph. + +2) In "Special Characters" chapter: + At "#" entry, noted that if comment follows a command on a line, + then whitespace preceding the "#" is necessary. + (Thanks, Le Wen.) + +3) In "Basic Commands" section of "External Commands" Chapter: + Greatly expanded discussion of "chattr" entry. + +4) In "File and Archiving Commands" section of "External Commands" Chapter: + Added "rpm -qf" tip at "rpm" entry. + +5) In "Text Processing" section of "External Commands" Chapter: + Added "enscript" entry. + At "egrep" and "fgrep" entries. + added symbolic link note + added "dict-lookup.sh" example script. + Updated "wc" entry. + +6) In "Communications Commands" section of "External Commands" chapter: + Added "netconfig" entry. + +7) In "Math Commands" section of "External Commands" chapter: + Fixup of "hexconvert.sh" example script + (had left out initialization of E_NOARGS variable) + (Thanks, Stefano Palmeri.) + +8) In "Internal Commands and Builtins" chapter: + At "forking/spawning" sidebar: + Added "spawn.sh" example + +9) In "Job Control Commands" section of "Internal Commands and Builtins" + chapter: + Corrected note discussing zombie processes. + (Thank you, Alan Sundell.) + +10) In "Here Documents" chapter: + Removed reference to 'telnet' -- since it's generally not a good idea. + +11) In "Of Zeroes and Nulls" chapter + Added/revised discussion of /dev/zero. + +12) In the "Shell Wrappers" section of "Miscellany" chapter: + Added footnote giving examples of system utilities that are really + shell wrappers. + Added "logging-wrapper.sh" example. + +13) In "Indirect References to Variables" section of "Variables Revisited" chapter: + Expanded Nils Radtke's example of building dynamic variable names. + +14) In "Parameter Substitution" section of "Variables Revisited" chapter: + Fixed up "ex6.sh" example to make it less ambiguous, + per suggestion of "Der Schwadde" . . . ). + +15) In "Miscellaneous Commands" section of "External Commands" Chapter: + At "dd" entry," added lowercase conversion in-line example. + At "jot/seq" entry, added "letter-count.sh" example script. + (Thanks, Stefano Palmeri.) + +16) In "System and Administrative Commands" chapter: + At "lockfile" entry, fixed typo in inline example. + (Thanks, Andreas Abraham.) + At "fuser" entry, added more material. + +17) In "Gotchas" chapter: + At "Mixing up '=' and '-eq' entry, fixed typo. + (Thanks, Andreas Abraham.) + +18) In "Colorizing Scripts" section of "Miscellany" chapter: + Added "horserace.sh" example. + (Thanks, Stefano Palmeri.) + +19) In "Redirecting Code Blocks" section of "I/O Redirection" chapter: + Added code snippet and commentary to redir2.sh example. + (Thank you, Bruno de Oliveira Schneider.) + +20) In "Copyright" chapter: + Added second URL for French translation. + +21) In the "Sed and Awk Micro-primer" appendix: + Fixed "letter-count.sh" example to work with gawk, ver. 3.1.3. + (Thanks to Stefano Palmeri for pointing out the need for a fixup.) + Renamed filename to "letter-count2.sh" to accommodate alternate + version of script which will appear prior to this one in the text. + +22) In "Contributed Scripts" appendix: + Fixups to "wgetter2.bash" script. + Added "bashpodder.sh" script. + (Thank you, Linc Fessenden.) + +23) In "Writing Scripts" section of "Exercises" appendix: + In "Intermediate sub-section": + Added "Enforcing Disk Quotas" exercise. + +24) Various miscellaneous fixups and enhancements: + In example scripts. + In citations of book titles (inserted tag). + + + + Version 3.2 Blueberry release, 02/06/05 diff --git a/LDP/guide/docbook/abs-guide/README b/LDP/guide/docbook/abs-guide/README index 670f9fb2..ac57b6c9 100644 --- a/LDP/guide/docbook/abs-guide/README +++ b/LDP/guide/docbook/abs-guide/README @@ -52,4 +52,5 @@ dev-tcp.sh (line 14) archiveweblogs.sh (comment in line 4) multiple-processes.sh (line 61) directory-info.sh (lines 36 and 166) +catscripts.sh (lines 12 and 21) is_spammer.bash (comments on various lines) diff --git a/LDP/guide/docbook/abs-guide/abs-guide.sgml b/LDP/guide/docbook/abs-guide/abs-guide.sgml index a995c260..e927ca3b 100644 --- a/LDP/guide/docbook/abs-guide/abs-guide.sgml +++ b/LDP/guide/docbook/abs-guide/abs-guide.sgml @@ -284,6 +284,7 @@ Uncomment line below to generate index. + @@ -293,6 +294,7 @@ Uncomment line below to generate index. + @@ -308,9 +310,13 @@ Uncomment line below to generate index. + + + + @@ -332,19 +338,12 @@ Uncomment line below to generate index. - 3.2 - 06 February 2005 + 3.3 + 20 March 2005 - - 3.0 - 03 Oct 2004 - mc - 'LOGANBERRY' release: Major update. - - 3.1 14 Nov 2004 @@ -359,6 +358,13 @@ Uncomment line below to generate index. 'BLUEBERRY' release: Minor update. + + 3.3 + 20 Mar 2005 + mc + 'RASPBERRY' release: Important Update. + + @@ -370,7 +376,7 @@ Uncomment line below to generate index. the while sneaking in little snippets of UNIX wisdom and lore. It serves as a textbook, a manual for self-study, and a reference and - source of knowledge on shell scripting techniques. The exercises + source of knowledge on shell scripting techniques. The exercises and heavily-commented examples invite active reader participation, under the premise that the only way to really learn scripting is to write scripts. @@ -379,7 +385,7 @@ Uncomment line below to generate index. introduction to programming concepts. + url="http://personal.riverusers.com/~thegrendel/abs-guide-3.3.tar.bz2"> The latest update of this document, as an archived, bzip2-ed tarball including both the SGML source and rendered HTML, may @@ -527,13 +533,13 @@ Uncomment line below to generate index. prototyping the application as a shell script might still be a useful development step. - We will be using Bash, an acronym for Bourne-Again - shell and a pun on Stephen Bourne's now classic Bourne - shell. Bash has become a de facto - standard for shell scripting on all flavors of UNIX. Most of - the principles dealt with in this book apply equally well to - scripting with other shells, such as the Korn Shell, from which - Bash derives some of its features, + We will be using Bash, an acronym for + Bourne-Again shell and a pun on Stephen Bourne's + now classic Bourne shell. Bash has become a de + facto standard for shell scripting on all flavors of + UNIX. Most of the principles dealt with in this book apply equally + well to scripting with other shells, such as the Korn Shell, + from which Bash derives some of its features, Many of the features of ksh88, and even a few from the updated ksh93 @@ -582,6 +588,11 @@ Uncomment line below to generate index. Starting Off With a Sha-Bang + + Larry Wall + Shell programming is a 1950s juke box . . . + + In the simplest case, a script is nothing more than a list of system commands stored in a file. At the very least, this saves the effort of retyping that particular sequence of commands each time @@ -700,7 +711,10 @@ echo "This line will never print (betcha!)." exit $WHATEVER # Doesn't matter. The script will not exit here. Also, try starting a README file with a #!/bin/more, and making it executable. - The result is a self-listing documentation file. + The result is a self-listing documentation file. (A here document using + cat is possibly a better alternative + -- see ). Using #!/bin/sh, the default Bourne shell @@ -738,23 +752,38 @@ exit $WHATEVER # Doesn't matter. The script will not exit here. - + This tutorial encourages a modular approach to constructing a script. Make note of and collect boilerplate code snippets that might be useful - in future scripts. Eventually you can build a quite extensive + in future scripts. Eventually you can build quite an extensive library of nifty routines. As an example, the following script prolog tests whether the script has been invoked with the correct number of parameters. - if [ $# -ne $Number_of_expected_args ] + E_WRONG_ARGS=65 +script_parameters="-a -h -m -z" +# -a = all, -h = help, etc. + +if [ $# -ne $Number_of_expected_args ] then - echo "Usage: `basename $0` script_parameters" + echo "Usage: `basename $0` $script_parameters" + # `basename $0` is the script's filename. exit $E_WRONG_ARGS fi - - + + Many times, you will write a script that carries out one + particular task. The first script in this chapter is an + example of this. Later, it might occur to you to generalize + the script to do other, similar tasks. Replacing the literal + (hard-wired) constants by variables is a step in + that direction, as is replacing repetitive code blocks by functions. + + + + Invoking the script @@ -883,9 +912,10 @@ fi # This line is a comment. - Comments may also occur at the end of a command. + Comments may also occur following the end of a command. - echo "A comment will follow." # Comment here. + echo "A comment will follow." # Comment here. +# ^ Note whitespace before # Comments may also follow whitespace at the beginning @@ -4720,7 +4750,7 @@ home=/home/bozo - <command>zmost</command> + <command>zmore</command> &ex14; @@ -8093,7 +8123,42 @@ remoteNet=$(eval "echo \$$(echo isdnMyProviderRemoteNet)") remoteNet=$(eval "echo \$isdnMyProviderRemoteNet") remoteNet=$(eval "echo $isdnMyProviderRemoteNet") -echo "$remoteNet" # 172.16.0.100 +echo "$remoteNet" # 172.16.0.100 + +# ================================================================ + +# And, it gets even better. + +# Consider the following snippet given a variable named getSparc, +#+ but no such variable getIa64: + +chkMirrorArchs () { + arch="$1"; + if [ "$(eval "echo \${$(echo get$(echo -ne $arch | + sed 's/^\(.\).*/\1/g' | tr 'a-z' 'A-Z'; echo $arch | + sed 's/^.\(.*\)/\1/g')):-false}")" = true ] + then + return 0; + else + return 1; + fi; +} + +getSparc="true" +unset getIa64 +chkMirrorArchs sparc +echo $? # 0 + # True + +chkMirrorArchs Ia64 +echo $? # 1 + # False + +# Notes: +# ----- +# Even the to-be-substituted variable name part is built explicitly. +# The parameters to the chkMirrorArchs calls are all lower case. +# The variable name is composed of two parts: "get" and "Sparc" . . . @@ -8130,7 +8195,7 @@ echo "$remoteNet" # 172.16.0.100 returns a pseudorandom True randomness, insofar as - it exists at all, is only to be found in certain + it exists at all, can only be found in certain incompletely understood natural phenomena such as radioactive decay. Computers can only simulate randomness, and computer-generated sequences of @@ -8212,19 +8277,21 @@ rnumber=$(((RANDOM%30/3+1)*3)) The /dev/urandom device-file provides - a means of generating much more random + a method of generating much more random pseudorandom numbers than the $RANDOM variable. dd if=/dev/urandom of=targetfile bs=1 count=XX creates a file of well-scattered - pseudorandom numbers. However, assigning these numbers to a - variable in a script requires a workaround, such as filtering - through od (as in above example and - ) or using dd - (see ). + pseudorandom numbers. However, assigning these numbers + to a variable in a script requires a workaround, such as + filtering through od (as in + above example and ), or using dd (see ), + or even piping to md5sum + (see ). - There are also other means of generating pseudorandom + There are also other ways to generate pseudorandom numbers in a script. Awk provides a convenient means of doing this. @@ -8976,6 +9043,11 @@ esac parent process is still executing. + + A script that forks off multiple instances of itself + &spawnscr; + + Generally, a Bash builtin does not fork a subprocess when it executes within a script. An external system command or filter in @@ -10495,11 +10567,12 @@ wait usually terminate a process that stubbornly refuses to die with a plain kill. Sometimes, a kill -15 works. A zombie - process, that is, a process whose parent has terminated, cannot be - killed (you can't kill something that is already dead), - but init will generally clean it up - sooner or later. + process, that is, a child process that has + terminated, but that the parent + process has not (yet) killed, cannot be killed by a + logged-on user -- you can't kill something that is already + dead -- but init will generally clean + it up sooner or later. @@ -10780,7 +10853,7 @@ tr a-z A-Z < filename # Same effect, but starts one less process, reverses each line of a file, and outputs to - stdout. This is not the same effect + stdout. This does not have the same effect as tac, as it preserves the order of the lines, but flips each one around. @@ -10993,10 +11066,49 @@ chmod u+s filename chattr - Change file attributes. This has the same effect - as chmod above, but with a - different invocation syntax, and it works only on an - ext2 filesystem. + + Change file attributes. This is analogous to + chmod above, but with different options + and a different invocation syntax, and it works only on + an ext2 filesystem. + + One particularly interesting chattr + option is . A chattr +i + filename marks the file + as immutable. The file cannot be modified, linked to, + or deleted , not even by root. This + file attribute can be set or removed only by root. In a + similar fashion, the option marks the + file as append only. + + + +root# chattr +i file1.txt + + +root# rm file1.txt + +rm: remove write-protected regular file `file1.txt'? y + rm: cannot remove `file1.txt': Operation not permitted + + + + If a file has the (secure) + attribute set, then when it is deleted its block is zeroed out + on the disk. + + If a file has the (undelete) + attribute set, then when it is deleted, its contents can still + be retrieved (undeleted). + + If a file has the (compress) + attribute set, then it will automatically be compressed + on writes to disk, and uncompressed on reads. + + The file attributes set with + chattr do not show in a file listing + (ls -l). + @@ -12308,18 +12420,29 @@ Here is some text. -- - egrep - (extended grep) is the same + - extended grep - is the same as grep -E. This uses a somewhat different, extended set of Regular Expressions, which can make the search a bit more flexible. - fgrep (fast - grep) is the same as grep - -F. It does a literal string search (no regular - expressions), which usually speeds things up a bit. + fgrep - fast grep + - is the same as grep -F. It does + a literal string search (no Regular Expressions), which + usually speeds things up a bit. + + On some Linux distros, egrep and + fgrep are symbolic links to, or aliases for + grep, but invoked with the + and options, + respectively. + + + Looking up definitions in <citetitle + pubwork="book">Webster's 1913 Dictionary</citetitle> + &dictlookup; + agrep (approximate grep) extends the capabilities of @@ -12328,7 +12451,6 @@ Here is some text. from the resulting matches. This utility is not part of the core Linux distribution. - To search compressed files, use zgrep, zegrep, or @@ -12423,13 +12545,14 @@ Here is some text. wc gives a word count on a file or I/O stream: - bash $ wc /usr/doc/sed-3.02/README -20 127 838 /usr/doc/sed-3.02/README -[20 lines 127 words 838 characters] + bash $ wc /usr/share/doc/sed-4.1.2/README +13 70 447 README +[13 lines 70 words 447 characters] wc -w gives only the word count. wc -l gives only the line count. - wc -c gives only the character count. + wc -c gives only the byte count. + wc -m gives only the character count. wc -L gives only the length of the longest line. Using wc to count how many @@ -12875,6 +12998,23 @@ tr -d 0-9 <filename + + enscript + + enscript + + + command + PostScript + + + Utility for converting plain text file to PostScript + For example, enscript filename.txt -p filename.ps + produces the PostScript output file + filename.ps. + + + groff tbl @@ -13119,8 +13259,21 @@ tr -d 0-9 <filename usually suffices to install a package, though there are many more options available. + - An rpm -qa gives a + rpm -qf identifies which package a + file originates from. + + + +bash$ rpm -qf /bin/ls +coreutils-5.2.1-31 + + + + + + rpm -qa gives a complete list of all installed rpm packages on a given system. An rpm -qa package_name lists only the package(s) corresponding to @@ -14029,7 +14182,8 @@ gzip -cd patchXX.gz | patch -p0 &fileintegrity; - See also for a creative use of + See also and for creative uses of the md5sum command. @@ -14502,7 +14656,6 @@ echo "tempfile name = $tempfile" bash$ ping localhost PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data. - Warning: time of day goes back, taking countermeasures. 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=709 usec 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=286 usec @@ -14783,7 +14936,8 @@ wget -r ftp://ftp.xyz24.net/~bozo/project_files/ -O $SAVEFILE "efetch; - See also . + See also and . @@ -14923,6 +15077,22 @@ wget -r ftp://ftp.xyz24.net/~bozo/project_files/ -O $SAVEFILE + + netconfig + + netconfig + + + command + network + + + A command-line utility for configuring a network adapter + (using DHCP). This command is native to Red Hat centric Linux + distros. + + + @@ -15375,6 +15545,12 @@ LIMIT_STRING Using <command>seq</command> to generate loop arguments &ex53; + + + Letter Count" + &lettercount; + + @@ -15613,9 +15789,10 @@ LIMIT_STRING For an excellent overview of this topic, see Andy Vaught's article, Introduction - to Named Pipes, in the September, 1997 issue - of Linux - Journal. + to Named Pipes, in the September, 1997 issue of + Linux + Journal. Typically, one process writes to the FIFO, and the other reads from it. See . @@ -15663,8 +15840,8 @@ LIMIT_STRING copies a file (or stdin/stdout), but with conversions. Possible conversions are ASCII/EBCDIC, - EBCDIC (pronounced - ebb-sid-ic) is an acronym for Extended + EBCDIC (pronounced + ebb-sid-ick) is an acronym for Extended Binary Coded Decimal Interchange Code. This is an IBM data format no longer in much use. A bizarre application of the option @@ -15684,6 +15861,13 @@ cat $file_encrypted | dd conv=swab,ascii > $file_plaintext lists the conversion and other options that this powerful utility takes. + + # Converting a file to all uppercase: + +dd if=$filename conv=ucase > $filename.uppercase +# lcase # For lower case conversion + + A script that copies itself &selfcopy; @@ -15897,9 +16081,10 @@ random001=`md5sum $0 | awk '{print $1}'` macro expansion facilities. The April, 2002 issue of Linux - Journal has a very nice article on - m4 and its uses. + url="http://www.linuxjournal.com">Linux Journal + has a very nice article on m4 and + its uses. Using m4 @@ -16938,7 +17123,7 @@ setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ< - nmap + nmap nmap @@ -17608,13 +17793,24 @@ Average: all 6.33 1.70 14.71 0.00 77.26 + + bash$ fuser -u /usr/bin/vim +/usr/bin/vim: 3207e(bozo) + + + +bash$ fuser -u /dev/null +/dev/null: 3009(bozo) 3010(bozo) 3197(bozo) 3199(bozo) + + + One important application for fuser is when physically inserting or removing storage media, such as CD ROM disks or USB flash drives. Sometimes trying a umount fails with a device is busy error message. This means that some user(s) and/or process(es) are accessing - the device. An fuser -um /device_name + the device. An fuser -um /dev/device_name will clear up the mystery, so you can kill any relevant processes. @@ -17632,6 +17828,28 @@ Average: all 6.33 1.70 14.71 0.00 77.26 + The fuser command, invoked with the + option identifies the processes + accessing a port. This + is especially useful in combination with nmap. + + + root# nmap localhost.localdomain +PORT STATE SERVICE + 25/tcp open smtp + + + +root# fuser -un tcp 25 +25/tcp: 2095(root) + +root# ps ax | grep 2095 | grep -v grep +2095 ? Ss 0:00 sendmail: accepting connections + + + + @@ -17971,8 +18189,9 @@ mount /mnt/cdrom Withers' article, Creating CDs, in the October, 1999 issue of Linux - Journal. + url="http://www.linuxjournal.com">Linux + Journal. @@ -18448,7 +18667,7 @@ mount -o loop /dev/loop0 /mnt # Mount it. appname=xyzip # Application "xyzip" created lock file "/var/lock/xyzip.lock". -if [ -e "/var/lock/$appname.lock ] +if [ -e "/var/lock/$appname.lock" ] then ... @@ -19767,10 +19986,10 @@ exec 3>&- # Now close it for the remainder of the s A here document is a special-purpose code block. It uses a form of I/O - redirection to feed a command list to an interactive - program or command, such as ftp, - telnet, or - ex. + redirection to feed a command list to + an interactive program or a command, such as ftp, cat, + or ex. COMMAND <<InputComesFromHERE ... @@ -19821,11 +20040,15 @@ LimitString &ex70; + Analogous to ex scripts are cat + scripts. + Multi-line message using <command>cat</command> &ex71; + The option to mark a here document limit string (<<-LimitString) suppresses leading tabs (but not spaces) in the output. This may be useful in making @@ -20750,10 +20973,11 @@ echo a111b | gawk '/a1+b/' for illustrations of this. The standard reference on this complex topic is Friedl's - Mastering Regular Expressions. Sed & - Awk, by Dougherty and Robbins also gives a very lucid - treatment of REs. See the for more - information on these books. + Mastering Regular + Expressions. Sed & + Awk, by Dougherty and Robbins also gives a very + lucid treatment of REs. See the for + more information on these books. @@ -21750,7 +21974,7 @@ func () { global_var=37 # Visible only within the function block #+ before the function has been called. -} # END OF FUNCTION +} # END OF FUNCTION echo "global_var = $global_var" # global_var = # Function "func" has not yet been called, @@ -22796,10 +23020,10 @@ ln -s /dev/null ~/.netscape/cookies Uses of /dev/zero Like /dev/null, - /dev/zero is a pseudo file, - but it actually contains nulls (binary zeros, not - the ASCII kind). Output written to it disappears, and - it is fairly difficult to actually read the nulls in + /dev/zero is a pseudo file, but + it actually produces a stream of nulls (binary zeros, + not the ASCII kind). Output written to it disappears, + and it is fairly difficult to actually read the nulls from /dev/zero, though it can be done with od or a hex editor. The chief use for /dev/zero is in creating an @@ -22815,14 +23039,17 @@ ln -s /dev/null ~/.netscape/cookies is to zero out a file of a designated size for a special purpose, such as mounting a filesystem on a loopback device - (see ) or securely deleting a file - (see ). + (see ) or securely + deleting a file (see ). Creating a ramdisk &ramdisk; + In addition to all the above, + /dev/zero is needed by ELF binaries. + @@ -23406,7 +23633,7 @@ echo "uninitialized_var = $uninitialized_var" if [ "$a" -eq 273 ] # If $a is an integer. # Sometimes you can mix up -eq and = without adverse consequences. -# However... +# However . . . a=273.0 # Not an integer. @@ -23425,7 +23652,7 @@ fi # Comparison does not work. if [ "$a" -eq 273.0 ] then - echo "a = $a' + echo "a = $a" fi # Aborts with an error message. # test.sh: [: 273.0: integer expression expected @@ -24030,28 +24257,31 @@ fi A wrapper is a shell script that embeds a system command or utility, that saves a set of parameters - passed to that command. Wrapping a script around a complex - command line simplifies invoking it. This is expecially - useful with sed and Quite a number of Linux utilities are, in fact, + shell wrappers. Some examples are + /usr/bin/pdf2ps, + /usr/bin/batch, and + /usr/X11R6/bin/xmkmf. + + Wrapping a script around a complex command line + simplifies invoking it. This is expecially useful + with sed and awk. - - A + + A sed - - - script - sed - - sed or + + script sed + sed or awk - - - script - awk + + script awk awk script would normally be invoked from the command line by a sed -e @@ -24078,6 +24308,11 @@ fi &ex4; + + A generic <command>shell wrapper</command> that writes to a logfile + &loggingwrapper; + + A <command>shell wrapper</command> around an awk script &coltotaler; @@ -24119,6 +24354,7 @@ fi + @@ -24184,8 +24420,9 @@ test "$city" \< Paris && echo "Yes, Paris is greater than $city" # Greater ASCI The ANSI - ANSI is, of course, the acronym for the American - National Standards Institute. + ANSI is, of course, the + acronym for the American National Standards + Institute. escape sequences set screen attributes, such as bold text, and color of foreground and background. DOS batch files commonly used @@ -24341,6 +24578,11 @@ test "$city" \< Paris && echo "Yes, Paris is greater than $city" # Greater ASCI &colorecho; + + A <quote>horserace</quote> game + &horserace; + + There is, however, a major problem with all this. ANSI escape sequences are emphatically non-portable. What works fine on some terminal @@ -25350,7 +25592,7 @@ fi - DocBook, The Definitive Guide, by Norman + DocBook, The Definitive Guide, by Norman Walsh and Leonard Muellner (O'Reilly, ISBN 1-56592-580-7). This is the standard reference for anyone attempting to write a document in Docbook SGML format. @@ -25464,23 +25706,23 @@ fi Others contributing scripts, making helpful suggestions, and pointing out errors were Gabor Kiss, Leopold Toetsch, Peter Tillier, Marcus Berglof, Tony Richardson, Nick Drage (script - ideas!), Rich Bartell, Jess Thrysoee, Adam Lazur, Bram - Moolenaar, Baris Cicek, Greg Keraunen, Keith Matthews, Sandro - Magi, Albert Reiner, Dim Segebart, Rory Winston, Lee Bigelow, - Wayne Pollock, jipe, Mark, - bojster, Ender, Emilio Conti, - Ian. D. Allen, Arun Giridhar, Dennis Leeuw, Dan Jacobson, Aurelio - Marinho Jargas, Edward Scholtz, Jean Helou, Chris Martin, Lee - Maschmeyer, Bruno Haible, Wilbert Berendsen, Sebastien Godard, - Bjön Eriksson, nyal, John MacDonald, Joshua - Tschida, Troy Engel, Manfred Schwarb, Amit Singh, Bill Gradwohl, - David Lombard, Jason Parker, Steve Parker, Bruce W. Clare, - William Park, Vernia Damiano, Mihai Maties, Jeremy Impson, - Ken Fuchs, Frank Wang, Sylvain Fourmanoit, Matthew Walker, - Kenny Stauffer, Filip Moritz, Andrzej Stefanski, Daniel Albers, - Stefano Palmeri, Nils Radtke, Jeroen Domburg, Alfredo Pironti, - Phil Braham, Little Monster (Alexis), and David - Lawyer (himself an author of four HOWTOs). + ideas!), Rich Bartell, Jess Thrysoee, Adam Lazur, Bram Moolenaar, + Baris Cicek, Greg Keraunen, Keith Matthews, Sandro Magi, Albert + Reiner, Dim Segebart, Rory Winston, Lee Bigelow, Wayne Pollock, + jipe, Mark, bojster, + Ender, Emilio Conti, Ian. D. Allen, Arun + Giridhar, Dennis Leeuw, Dan Jacobson, Aurelio Marinho Jargas, + Edward Scholtz, Jean Helou, Chris Martin, Lee Maschmeyer, Bruno + Haible, Wilbert Berendsen, Sebastien Godard, Bjön Eriksson, + nyal, John MacDonald, Joshua Tschida, Troy Engel, + Manfred Schwarb, Amit Singh, Bill Gradwohl, David Lombard, + Jason Parker, Steve Parker, Bruce W. Clare, William Park, Vernia + Damiano, Mihai Maties, Jeremy Impson, Ken Fuchs, Frank Wang, + Sylvain Fourmanoit, Matthew Walker, Kenny Stauffer, Filip Moritz, + Andrzej Stefanski, Daniel Albers, Stefano Palmeri, Nils Radtke, + Jeroen Domburg, Alfredo Pironti, Phil Braham, Bruno de Oliveira + Schneider, Little Monster (Alexis), Linc Fessenden, + and David Lawyer (himself an author of four HOWTOs). My gratitude to Chet Ramey and Brian Fox for writing Bash, @@ -25922,11 +26164,12 @@ fi 0-596-00392-7 An array of seven UNIX books on CD ROM, including - UNIX Power Tools, Sed - and Awk, and Learning the Korn - Shell. A complete set of all the UNIX references - and tutorials you would ever need at about $130. Buy this one, - even if it means going into debt and not paying the rent. + UNIX Power Tools, + Sed and Awk, and Learning the Korn Shell. A complete + set of all the UNIX references and tutorials you would ever need + at about $130. Buy this one, even if it means going into debt + and not paying the rent. * @@ -25940,25 +26183,28 @@ fi Fioretti, Marco, Scripting for X - Productivity, LINUX JOURNAL, Issue 113, September, 2003, pp. - 86-9. + Productivity, Linux Journal, Issue 113, + September, 2003, pp. 86-9. Ben Okopnik's well-written introductory Bash - scripting articles in issues 53, 54, 55, 57, and 59 - of the Linux Gazette - , and his explanation of The Deep, Dark Secrets - of Bash in issue 56. + scripting articles in issues 53, 54, 55, 57, and + 59 of the Linux Gazette, and his + explanation of The Deep, Dark Secrets of Bash + in issue 56. Chet Ramey's bash - The GNU Shell, - a two-part series published in issues 3 and 4 of the Linux Journal, - July-August 1994. + a two-part series published in issues 3 and 4 of the + Linux Journal, July-August + 1994. @@ -25987,8 +26233,10 @@ fi Ed Schaefer's Shell Corner - in Unix Review. + url="http://www.unixreview.com/columns/schaefer/">Shell + Corner + in Unix Review. @@ -26157,12 +26405,13 @@ fi has an article in the November, 2004 issue of the Linux Gazette on - adding string functions to Bash, with a followup article - in the December issue, and yet another in - the January, 2005 issue. + url="http://www.linuxgazette.net">Linux Gazette + on adding string functions to Bash, with a followup + article in the December issue, and yet another + in the January, 2005 issue. @@ -26179,7 +26428,7 @@ fi - The excellent "Bash Reference Manual", by Chet Ramey and Brian Fox, + The excellent Bash Reference Manual, by Chet Ramey and Brian Fox, distributed as part of the "bash-2-doc" package (available as an rpm). See especially the instructive example scripts in this package. @@ -26439,6 +26688,11 @@ fi &wgetter2; + + A <quote>podcasting</quote> script + &bashpodder; + + To end this section, a review of the basics . . . and more. @@ -27386,6 +27640,7 @@ pattern=BEGIN + @@ -27444,9 +27699,9 @@ awk '{print $1 $5 $6}' $filename The following example illustrates how awk can add text-parsing tools to a shell script. - + Counting Letter Occurrences - &lettercount; + &lettercount2; For simpler examples of awk within shell scripts, see: @@ -29027,6 +29282,20 @@ done < `tail -f /var/log/messages` + + Enforcing Disk Quotas + + Write a script for a multi-user system that checks users' + disk usage. If a user surpasses the preset limit + (100 MB, for example) in her /home/username directory, + then the script will automatically send her a warning + e-mail. + The script will use the du and + mail commands. + + + Logged in User Information @@ -29653,10 +29922,10 @@ fairly detailed rundown on the Playfair Cipher and its solution methods. This document first appeared as a HOWTO in the late spring - of 2000. Since then, it has gone through many updates and + of 2000. Since then, it has gone through many updates and revisions. This book could not have been written without the - assistance of the Linux community, and especially of the Linux Documentation Project. + assistance of the Linux community, and especially of the volunteers of + the Linux Documentation Project. @@ -29815,6 +30084,11 @@ fairly detailed rundown on the Playfair Cipher and its solution methods.06 Feb 2005 BLUEBERRY release: Minor update. + + + 20 Mar 2005 + RASPBERRY release: Bugfixes, much material added. + @@ -29882,10 +30156,10 @@ fairly detailed rundown on the Playfair Cipher and its solution methods. Copyright - The Advanced Bash Scripting Guide is copyright 2000, by Mendel Cooper. The - author also asserts copyright on all previous versions of this - document. + The Advanced Bash Scripting + Guide is copyright + 2000, by Mendel Cooper. The author also asserts + copyright on all previous versions of this document. This blanket copyright recognizes and protects the rights of the contributors to this document. @@ -29979,9 +30253,10 @@ B. Distribution of the work or derivative of the work in any standard Hyun Jin Cha has done a Korean - translation of version 1.0.11 - of this book. Spanish, Portuguese, French, German, of version 1.0.11 of this book. Spanish, + Portuguese, French, + (another French), + German, Italian, Russian, diff --git a/LDP/guide/docbook/abs-guide/arith-ops.sh b/LDP/guide/docbook/abs-guide/arith-ops.sh index 6a7933ba..63c86082 100644 --- a/LDP/guide/docbook/abs-guide/arith-ops.sh +++ b/LDP/guide/docbook/abs-guide/arith-ops.sh @@ -23,13 +23,13 @@ echo -n "$n " : $[ n = $n + 1 ] # ":" necessary because otherwise Bash attempts #+ to interpret "$[ n = $n + 1 ]" as a command. -# Works even if "n" was initialized as a string. +# Works even if "n" was initialized as a string. echo -n "$n " n=$[ $n + 1 ] # Works even if "n" was initialized as a string. #* Avoid this type of construct, since it is obsolete and nonportable. -# Thanks, Stephane Chazelas. +# Thanks, Stephane Chazelas. echo -n "$n " # Now for C-style increment operators. diff --git a/LDP/guide/docbook/abs-guide/arith-tests.sh b/LDP/guide/docbook/abs-guide/arith-tests.sh index 30a9e340..a803b89a 100644 --- a/LDP/guide/docbook/abs-guide/arith-tests.sh +++ b/LDP/guide/docbook/abs-guide/arith-tests.sh @@ -27,6 +27,7 @@ echo "Exit status of \"(( 1 / 2 ))\" is $?." # Rounded off to 0. # 1 (( 1 / 0 )) 2>/dev/null # Illegal division by 0. +# ^^^^^^^^^^^ echo "Exit status of \"(( 1 / 0 ))\" is $?." # 1 # What effect does the "2>/dev/null" have? diff --git a/LDP/guide/docbook/abs-guide/blot-out.sh b/LDP/guide/docbook/abs-guide/blot-out.sh index 9c15f9dc..73c0aaa1 100644 --- a/LDP/guide/docbook/abs-guide/blot-out.sh +++ b/LDP/guide/docbook/abs-guide/blot-out.sh @@ -76,7 +76,8 @@ exit 0 # The file cannot not be "undeleted" or retrieved by normal methods. # However . . . -#+ this simple method would *not* likely withstand forensic analysis. +#+ this simple method would *not* likely withstand +#+ sophisticated forensic analysis. # This script may not play well with a journaled file system. # Exercise: Fix it so it does. @@ -90,4 +91,4 @@ exit 0 # For an in-depth analysis on the topic of file deletion and security, #+ see Peter Gutmann's paper, #+ "Secure Deletion of Data From Magnetic and Solid-State Memory". -# http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html +# http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html diff --git a/LDP/guide/docbook/abs-guide/cannon.sh b/LDP/guide/docbook/abs-guide/cannon.sh index 91d40efd..4909bd68 100644 --- a/LDP/guide/docbook/abs-guide/cannon.sh +++ b/LDP/guide/docbook/abs-guide/cannon.sh @@ -1,26 +1,26 @@ #!/bin/bash # cannon.sh: Approximating PI by firing cannonballs. -# This is a very simple instance of a "Monte Carlo" simulation, +# This is a very simple instance of a "Monte Carlo" simulation: #+ a mathematical model of a real-life event, #+ using pseudorandom numbers to emulate random chance. # Consider a perfectly square plot of land, 10000 units on a side. # This land has a perfectly circular lake in its center, #+ with a diameter of 10000 units. -# The plot is actually all water, except for the four corners. +# The plot is actually mostly water, except for land in the four corners. # (Think of it as a square with an inscribed circle.) # -# Let us fire iron cannonballs from an old-style cannon -#+ at the square of land. -# All the shots impact somewhere on the plot of land, +# We will fire iron cannonballs from an old-style cannon +#+ at the square. +# All the shots impact somewhere on the square, #+ either in the lake or on the dry corners. -# Since the lake takes up most of the land area, +# Since the lake takes up most of the area, #+ most of the shots will SPLASH! into the water. # Just a few shots will THUD! into solid ground -#+ in the four corners of the land. +#+ in the four corners of the square. # -# If we take enough random, unaimed shots at the plot of land, +# If we take enough random, unaimed shots at the square, #+ Then the ratio of SPLASHES to total shots will approximate #+ the value of PI/4. # @@ -32,10 +32,10 @@ # Theoretically, the more shots taken, the better the fit. # However, a shell script, as opposed to a compiled language #+ with floating-point math built in, requires a few compromises. -# This tends to lower the accuracy of the simulation, unfortunately. +# This tends to lower the accuracy of the simulation, of course. -DIMENSION=10000 # Length of each side of the plot of land. +DIMENSION=10000 # Length of each side of the plot. # Also sets ceiling for random integers generated. MAXSHOTS=1000 # Fire this many shots. @@ -86,8 +86,8 @@ do printf "Xc = %4d " $xCoord printf "Yc = %4d " $yCoord printf "Distance = %5d " $distance # Distance from - #+ center of lake, - # the "origin," + #+ center of lake -- + # the "origin" -- #+ coordinate (0,0). if [ "$distance" -le "$DIMENSION" ] diff --git a/LDP/guide/docbook/abs-guide/col-totaler.sh b/LDP/guide/docbook/abs-guide/col-totaler.sh index b9b7da3b..75443f09 100644 --- a/LDP/guide/docbook/abs-guide/col-totaler.sh +++ b/LDP/guide/docbook/abs-guide/col-totaler.sh @@ -17,7 +17,7 @@ column_number=$2 # Passing shell variables to the awk part of the script is a bit tricky. # See the awk documentation for more details. -# A multi-line awk script is invoked by awk ' ..... ' +# A multi-line awk script is invoked by: awk ' ..... ' # Begin awk script. @@ -36,7 +36,7 @@ END { # It may not be safe to pass shell variables to an embedded awk script, -# so Stephane Chazelas proposes the following alternative: +#+ so Stephane Chazelas proposes the following alternative: # --------------------------------------- # awk -v column_number="$column_number" ' # { total += $column_number diff --git a/LDP/guide/docbook/abs-guide/ex13.sh b/LDP/guide/docbook/abs-guide/ex13.sh index 858f6187..a51b1f5c 100644 --- a/LDP/guide/docbook/abs-guide/ex13.sh +++ b/LDP/guide/docbook/abs-guide/ex13.sh @@ -9,7 +9,7 @@ b=5 # Bash permits integer operations and comparisons on variables #+ whose value consists of all-integer characters. -# Caution advised. +# Caution advised, however. echo diff --git a/LDP/guide/docbook/abs-guide/ex14.sh b/LDP/guide/docbook/abs-guide/ex14.sh index decd7b22..77a42c4a 100644 --- a/LDP/guide/docbook/abs-guide/ex14.sh +++ b/LDP/guide/docbook/abs-guide/ex14.sh @@ -1,13 +1,14 @@ #!/bin/bash +# zmore -#View gzipped files with 'most' +#View gzipped files with 'more' NOARGS=65 NOTFOUND=66 NOTGZIP=67 if [ $# -eq 0 ] # same effect as: if [ -z "$1" ] -# $1 can exist, but be empty: zmost "" arg2 arg3 +# $1 can exist, but be empty: zmore "" arg2 arg3 then echo "Usage: `basename $0` filename" >&2 # Error message to stderr. @@ -31,11 +32,10 @@ then exit $NOTGZIP fi -zcat $1 | most +zcat $1 | more -# Uses the file viewer 'most' (similar to 'less'). -# Later versions of 'most' have file decompression capabilities. -# May substitute 'more' or 'less', if desired. +# Uses the filter 'more.' +# May substitute 'less', if desired. exit $? # Script returns exit status of pipe. diff --git a/LDP/guide/docbook/abs-guide/ex16.sh b/LDP/guide/docbook/abs-guide/ex16.sh index 478a1c75..e84fe08a 100644 --- a/LDP/guide/docbook/abs-guide/ex16.sh +++ b/LDP/guide/docbook/abs-guide/ex16.sh @@ -9,10 +9,10 @@ echo $b a=`echo Hello!` # Assigns result of 'echo' command to 'a' echo $a -# Note that using an exclamation mark (!) in command substitution -#+ will not work from the command line, +# Note that including an exclamation mark (!) within a +#+ command substitution construct #+ will not work from the command line, #+ since this triggers the Bash "history mechanism." -# Within a script, however, the history functions are disabled. +# Inside a script, however, the history functions are disabled. a=`ls -l` # Assigns result of 'ls -l' command to 'a' echo $a # Unquoted, however, removes tabs and newlines. diff --git a/LDP/guide/docbook/abs-guide/ex1a.sh b/LDP/guide/docbook/abs-guide/ex1a.sh index a3340e49..0d1fc587 100644 --- a/LDP/guide/docbook/abs-guide/ex1a.sh +++ b/LDP/guide/docbook/abs-guide/ex1a.sh @@ -6,7 +6,7 @@ # Run as root, of course. # Insert code here to print error message and exit if not root. -LOG_DIR=var/log +LOG_DIR=/var/log # Variables are better than hard-coded values. cd $LOG_DIR diff --git a/LDP/guide/docbook/abs-guide/ex24.sh b/LDP/guide/docbook/abs-guide/ex24.sh index f0c40b1f..aedd2627 100644 --- a/LDP/guide/docbook/abs-guide/ex24.sh +++ b/LDP/guide/docbook/abs-guide/ex24.sh @@ -1,4 +1,5 @@ #!/bin/bash +# Faxing (must have 'fax' installed). EXPECTED_ARGS=2 E_BADARGS=65 diff --git a/LDP/guide/docbook/abs-guide/ex29.sh b/LDP/guide/docbook/abs-guide/ex29.sh index 7ee674c1..b67e2ce7 100644 --- a/LDP/guide/docbook/abs-guide/ex29.sh +++ b/LDP/guide/docbook/abs-guide/ex29.sh @@ -16,12 +16,13 @@ esac # Allows ranges of characters in [square brackets], #+ the tests for lowercase and uppercase characters were #+ [a-z] and [A-Z]. # This no longer works in certain locales and/or Linux distros. +# POSIX is more portable. # Thanks to Frank Wang for pointing this out. # Exercise: # -------- # As the script stands, it accepts a single keystroke, then terminates. -# Change the script so it accepts continuous input, +# Change the script so it accepts repeated input, #+ reports on each keystroke, and terminates only when "X" is hit. # Hint: enclose everything in a "while" loop. diff --git a/LDP/guide/docbook/abs-guide/ex30.sh b/LDP/guide/docbook/abs-guide/ex30.sh index 04e73804..ae84571d 100644 --- a/LDP/guide/docbook/abs-guide/ex30.sh +++ b/LDP/guide/docbook/abs-guide/ex30.sh @@ -40,7 +40,7 @@ case "$person" in echo "(212) 533-2814" echo "(212) 533-9972 fax" echo "milliej@loisaida.com" - echo "Girlfriend" + echo "Ex-girlfriend" echo "Birthday: Feb. 11" ;; @@ -59,7 +59,7 @@ echo # Exercise: # -------- -# Change the script so it accepts continuous input, +# Change the script so it accepts multiple inputs, #+ instead of terminating after displaying just one address. exit 0 diff --git a/LDP/guide/docbook/abs-guide/ex36.sh b/LDP/guide/docbook/abs-guide/ex36.sh index fc29e6c8..e3fc7712 100644 --- a/LDP/guide/docbook/abs-guide/ex36.sh +++ b/LDP/guide/docbook/abs-guide/ex36.sh @@ -1,4 +1,5 @@ #!/bin/bash +# "Reading" variables. echo -n "Enter the value of variable 'var1': " # The -n option to echo suppresses newline. diff --git a/LDP/guide/docbook/abs-guide/ex44.sh b/LDP/guide/docbook/abs-guide/ex44.sh index 0b28fc00..56d290f7 100644 --- a/LDP/guide/docbook/abs-guide/ex44.sh +++ b/LDP/guide/docbook/abs-guide/ex44.sh @@ -11,7 +11,7 @@ $killppp # This variable is now a command. # The following operations must be done as root user. -chmod 666 /dev/ttyS3 # Must be read+write permissions, or else what? +chmod 666 /dev/ttyS3 # Restore read+write permissions, or else what? # Since doing a SIGKILL on ppp changed the permissions on the serial port, #+ we restore permissions to previous state. @@ -24,3 +24,5 @@ exit 0 # 1) Have script check whether root user is invoking it. # 2) Do a check on whether the process to be killed #+ is actually running before attempting to kill it. +# 3) Write an alternate version of this script based on 'fuser': +#+ if [ fuser -s /dev/modem ]; then . . . diff --git a/LDP/guide/docbook/abs-guide/ex47.sh b/LDP/guide/docbook/abs-guide/ex47.sh index bd3a7462..da037e05 100644 --- a/LDP/guide/docbook/abs-guide/ex47.sh +++ b/LDP/guide/docbook/abs-guide/ex47.sh @@ -13,9 +13,9 @@ echo printf "Pi to 9 decimal places = %1.9f" $PI # It even rounds off correctly. printf "\n" # Prints a line feed, - # equivalent to 'echo'. + # Equivalent to 'echo' . . . -printf "Constant = \t%d\n" $DecimalConstant # Inserts tab (\t) +printf "Constant = \t%d\n" $DecimalConstant # Inserts tab (\t). printf "%s %s \n" $Message1 $Message2 @@ -34,6 +34,7 @@ Msg=`printf "%s %s \n" $Message1 $Message2` echo $Msg; echo $Msg # As it happens, the 'sprintf' function can now be accessed -#+ as a loadable module to Bash, but this is not portable. +#+ as a loadable module to Bash, +#+ but this is not portable. exit 0 diff --git a/LDP/guide/docbook/abs-guide/ex57.sh b/LDP/guide/docbook/abs-guide/ex57.sh index 49c0a333..dc60b85f 100644 --- a/LDP/guide/docbook/abs-guide/ex57.sh +++ b/LDP/guide/docbook/abs-guide/ex57.sh @@ -1,6 +1,5 @@ #!/bin/bash # badname.sh - # Delete filenames in current directory containing bad characters. for filename in * diff --git a/LDP/guide/docbook/abs-guide/ex6.sh b/LDP/guide/docbook/abs-guide/ex6.sh index 7c5f4742..30d68b69 100644 --- a/LDP/guide/docbook/abs-guide/ex6.sh +++ b/LDP/guide/docbook/abs-guide/ex6.sh @@ -36,7 +36,7 @@ echo #+ then the script terminates with an error message. # You can specify the error message. -# : ${ZZXy23AB?"ZZXy23AB has not been set."} +# : ${variablename?"ERROR MESSAGE"} # Same result with: dummy_variable=${ZZXy23AB?} @@ -53,3 +53,5 @@ echo "You will not see this message, because script already terminated." HERE=0 exit $HERE # Will NOT exit here. + +# In fact, this script will return an exit status (echo $?) of 1. diff --git a/LDP/guide/docbook/abs-guide/ex68.sh b/LDP/guide/docbook/abs-guide/ex68.sh index 8776ddde..ba295a71 100644 --- a/LDP/guide/docbook/abs-guide/ex68.sh +++ b/LDP/guide/docbook/abs-guide/ex68.sh @@ -9,14 +9,14 @@ LOWER_LIMIT=1 # Starting with 1. UPPER_LIMIT=1000 # Up to 1000. -# (You may set this higher... if you have time on your hands.) +# (You may set this higher . . . if you have time on your hands.) PRIME=1 NON_PRIME=0 let SPLIT=UPPER_LIMIT/2 # Optimization: -# Need to test numbers only halfway to upper limit. +# Need to test numbers only halfway to upper limit (why?). declare -a Primes @@ -104,8 +104,8 @@ exit 0 -# ----------------------------------------------- # -# Code below line will not execute. +# -------------------------------------------------------- # +# Code below line will not execute, because of 'exit.' # This improved version of the Sieve, by Stephane Chazelas, #+ executes somewhat faster. diff --git a/LDP/guide/docbook/abs-guide/ex78.sh b/LDP/guide/docbook/abs-guide/ex78.sh index 2cc11a16..e8ceb096 100644 --- a/LDP/guide/docbook/abs-guide/ex78.sh +++ b/LDP/guide/docbook/abs-guide/ex78.sh @@ -22,6 +22,7 @@ echo "Value of t changed to ${!t}" # 387 # This is useful for referencing members of an array or table, #+ or for simulating a multi-dimensional array. -# An indexing option would have been nice (sigh). +# An indexing option (analogous to pointer arithmetic) +#+ would have been nice. Sigh. exit 0 diff --git a/LDP/guide/docbook/abs-guide/ex79.sh b/LDP/guide/docbook/abs-guide/ex79.sh index b00584ba..cdb44bd6 100644 --- a/LDP/guide/docbook/abs-guide/ex79.sh +++ b/LDP/guide/docbook/abs-guide/ex79.sh @@ -1,5 +1,4 @@ #!/bin/bash -# May need to be invoked with #!/bin/bash2 on older machines. # Cards: # Deals four random hands from a deck of cards. @@ -17,7 +16,7 @@ CARDS=52 declare -a Deck declare -a Suits declare -a Cards -# It would have been easier and more intuitive +# It would have been easier to implement and more intuitive #+ with a single, 3-dimensional array. # Perhaps a future version of Bash will support multidimensional arrays. @@ -77,6 +76,8 @@ seed_random () # Seed random number generator. seed=`eval date +%s` let "seed %= 32766" RANDOM=$seed +# What are some other methods +#+ of seeding the random number generator? } deal_cards () diff --git a/LDP/guide/docbook/abs-guide/ex9.sh b/LDP/guide/docbook/abs-guide/ex9.sh index a7a373f9..6601b1e8 100644 --- a/LDP/guide/docbook/abs-guide/ex9.sh +++ b/LDP/guide/docbook/abs-guide/ex9.sh @@ -7,11 +7,14 @@ hello=$a #------------------------------------------------------------------------- # No space permitted on either side of = sign when initializing variables. +# What happens if there is a space? # If "VARIABLE =value", +# ^ #+ script tries to run "VARIABLE" command with one argument, "=value". # If "VARIABLE= value", +# ^ #+ script tries to run "value" command with #+ the environmental variable "VARIABLE" set to "". #------------------------------------------------------------------------- @@ -31,11 +34,13 @@ hello="A B C D" echo $hello # A B C D echo "$hello" # A B C D # As you see, echo $hello and echo "$hello" give different results. +# ^ ^ # Quoting a variable preserves whitespace. echo echo '$hello' # $hello +# ^ ^ # Variable referencing disabled by single quotes, #+ which causes the "$" to be interpreted literally. @@ -64,16 +69,18 @@ echo "var1=$var1 var2=$var2 var3=$var3" echo; echo numbers="one two three" +# ^ ^ other_numbers="1 2 3" -# If whitespace within a variable, then quotes necessary. +# ^ ^ +# If there is whitespace within a variable, then quotes are necessary. echo "numbers = $numbers" echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 echo echo "uninitialized_variable = $uninitialized_variable" # Uninitialized variable has null value (no value at all). -uninitialized_variable= # Declaring, but not initializing it - #+ (same as setting it to a null value, as above). +uninitialized_variable= # Declaring, but not initializing it -- + #+ same as setting it to a null value, as above. echo "uninitialized_variable = $uninitialized_variable" # It still has a null value. @@ -81,7 +88,6 @@ uninitialized_variable=23 # Set it. unset uninitialized_variable # Unset it. echo "uninitialized_variable = $uninitialized_variable" # It still has a null value. - echo exit 0 diff --git a/LDP/guide/docbook/abs-guide/hexconvert.sh b/LDP/guide/docbook/abs-guide/hexconvert.sh index 2f32cef7..fe2062a6 100644 --- a/LDP/guide/docbook/abs-guide/hexconvert.sh +++ b/LDP/guide/docbook/abs-guide/hexconvert.sh @@ -1,6 +1,7 @@ #!/bin/bash # hexconvert.sh: Convert a decimal number to hexadecimal. +E_NOARGS=65 # Command-line arg missing. BASE=16 # Hexadecimal. if [ -z "$1" ] diff --git a/LDP/guide/docbook/abs-guide/hypotenuse.sh b/LDP/guide/docbook/abs-guide/hypotenuse.sh index 4a9a6410..980f1795 100644 --- a/LDP/guide/docbook/abs-guide/hypotenuse.sh +++ b/LDP/guide/docbook/abs-guide/hypotenuse.sh @@ -16,6 +16,7 @@ AWKSCRIPT=' { printf( "%3.7f\n", sqrt($1*$1 + $2*$2) ) } ' # command(s) / parameters passed to awk +# Now, pipe the parameters to awk. echo -n "Hypotenuse of $1 and $2 = " echo $1 $2 | awk "$AWKSCRIPT" diff --git a/LDP/guide/docbook/abs-guide/is-spammer.sh b/LDP/guide/docbook/abs-guide/is-spammer.sh index 2a46003e..47e21d67 100644 --- a/LDP/guide/docbook/abs-guide/is-spammer.sh +++ b/LDP/guide/docbook/abs-guide/is-spammer.sh @@ -21,7 +21,7 @@ # ^^^^^^^^^^^ # Or the domain.name(s) from any e-mail address: # Really_Good_Offer@spammer.biz -# ^^^^^^^^^^^ +# # as the only argument to this script. #(PS: have your Inet connection running) # diff --git a/LDP/guide/docbook/abs-guide/keypress.sh b/LDP/guide/docbook/abs-guide/keypress.sh index a8994b1b..de1eaab3 100644 --- a/LDP/guide/docbook/abs-guide/keypress.sh +++ b/LDP/guide/docbook/abs-guide/keypress.sh @@ -1,9 +1,9 @@ #!/bin/bash -# keypress.sh: Detect a user keypress ("hot keyboard"). +# keypress.sh: Detect a user keypress ("hot keys"). echo -old_tty_settings=$(stty -g) # Save old settings. +old_tty_settings=$(stty -g) # Save old settings (why?). stty -icanon Keypress=$(head -c1) # or $(dd bs=1 count=1 2> /dev/null) # on non-GNU systems diff --git a/LDP/guide/docbook/abs-guide/length.sh b/LDP/guide/docbook/abs-guide/length.sh index 75e0989a..fbd34a12 100644 --- a/LDP/guide/docbook/abs-guide/length.sh +++ b/LDP/guide/docbook/abs-guide/length.sh @@ -5,14 +5,17 @@ E_NO_ARGS=65 if [ $# -eq 0 ] # Must have command-line args to demo script. then - echo "Invoke this script with one or more command-line arguments." + echo "Please invoke this script with one or more command-line arguments." exit $E_NO_ARGS fi var01=abcdEFGH28ij - echo "var01 = ${var01}" echo "Length of var01 = ${#var01}" +# Now, let's try embedding a space. +var02="abcd EFGH28ij" +echo "var02 = ${var02}" +echo "Length of var02 = ${#var02}" echo "Number of command-line arguments passed to script = ${#@}" echo "Number of command-line arguments passed to script = ${#*}" diff --git a/LDP/guide/docbook/abs-guide/letter-count.sh b/LDP/guide/docbook/abs-guide/letter-count.sh index 458aedff..88b15f28 100644 --- a/LDP/guide/docbook/abs-guide/letter-count.sh +++ b/LDP/guide/docbook/abs-guide/letter-count.sh @@ -1,60 +1,55 @@ -#! /bin/sh +#!/bin/bash # letter-count.sh: Counting letter occurrences in a text file. -# -# Script by nyal (nyal@voila.fr). -# Used with permission. -# Recommented by document author. +# Written by Stefano Palmeri. +# Used in ABS Guide with permission. +# Slightly modified by document author. + +MINARGS=2 # Script requires at least two arguments. +E_BADARGS=65 +FILE=$1 + +let LETTERS=$#-1 # How many letters specified (as command-line args). + # (Subtract 1 from number of command line args.) -INIT_TAB_AWK="" -# Parameter to initialize awk script. -count_case=0 -FILE_PARSE=$1 - -E_PARAMERR=65 - -usage() -{ - echo "Usage: letter-count.sh file letters" 2>&1 - # For example: ./letter-count.sh filename.txt a b c - exit $E_PARAMERR # Not enough arguments passed to script. +show_help(){ + echo + echo Usage: `basename $0` file letters + echo Note: `basename $0` arguments are case sensitive. + echo Example: `basename $0` foobar.txt G n U L i N U x. + echo } -if [ ! -f "$1" ] ; then - echo "$1: No such file." 2>&1 - usage # Print usage message and exit. -fi +# Checks number of arguments. +if [ $# -lt $MINARGS ]; then + echo + echo "Not enough arguments." + echo + show_help + exit $E_BADARGS +fi -if [ -z "$2" ] ; then - echo "$2: No letters specified." 2>&1 - usage -fi -shift # Letters specified. -for letter in `echo $@` # For each one . . . - do - INIT_TAB_AWK="$INIT_TAB_AWK tab_search[${count_case}] = \"$letter\"; final_tab[${count_case}] = 0; " - # Pass as parameter to awk script below. - count_case=`expr $count_case + 1` +# Checks if file exists. +if [ ! -f $FILE ]; then + echo "File \"$FILE\" does not exist." + exit $E_BADARGS +fi + + + +# Counts letter occurrences . +for n in `seq $LETTERS`; do + shift + if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then # Checks arg. + echo "$1" -\> `cat $FILE | tr -cd "$1" | wc -c` # Counting. + else + echo "$1 is not a single char." + fi done -# DEBUG: -# echo $INIT_TAB_AWK; - -cat $FILE_PARSE | -# Pipe the target file to the following awk script. - -# -------------------------------------------------------------------------------- -awk -v tab_search=0 -v final_tab=0 -v tab=0 -v nb_letter=0 -v chara=0 -v chara2=0 \ -"BEGIN { $INIT_TAB_AWK } \ -{ split(\$0, tab, \"\"); \ -for (chara in tab) \ -{ for (chara2 in tab_search) \ -{ if (tab_search[chara2] == tab[chara]) { final_tab[chara2]++ } } } } \ -END { for (chara in final_tab) \ -{ print tab_search[chara] \" => \" final_tab[chara] } }" -# -------------------------------------------------------------------------------- -# Nothing all that complicated, just . . . -#+ for-loops, if-tests, and a couple of specialized functions. - exit $? + +# This script has exactly the same functionality as letter-count2.sh, +#+ but executes faster. +# Why? diff --git a/LDP/guide/docbook/abs-guide/life.sh b/LDP/guide/docbook/abs-guide/life.sh index a876570a..0fb67a93 100644 --- a/LDP/guide/docbook/abs-guide/life.sh +++ b/LDP/guide/docbook/abs-guide/life.sh @@ -61,7 +61,7 @@ FALSE=1 ALIVE=0 DEAD=1 -avar= # Global; holds current generation. +avar= # Global; holds current generation. generation=0 # Initialize generation count. # ================================================================= @@ -276,7 +276,7 @@ done avar=`echo ${array[@]}` # Convert array back to string variable. display "$avar" # Display it. echo; echo -echo "Generation $generation -- $alive alive" +echo "Generation $generation - $alive alive" if [ "$alive" -eq 0 ] then @@ -312,7 +312,7 @@ echo "=======================" Gen0=`echo ${initial[@]}` display "$Gen0" # Display only. echo; echo -echo "Generation $generation -- $alive alive" +echo "Generation $generation - $alive alive" # ------------------------------------------- diff --git a/LDP/guide/docbook/abs-guide/line-number.sh b/LDP/guide/docbook/abs-guide/line-number.sh index 5dda46b6..da3266e5 100644 --- a/LDP/guide/docbook/abs-guide/line-number.sh +++ b/LDP/guide/docbook/abs-guide/line-number.sh @@ -1,9 +1,10 @@ #!/bin/bash +# line-number.sh # This script echoes itself twice to stdout with its lines numbered. -# 'nl' sees this as line 3 since it does not number blank lines. -# 'cat -n' sees the above line as number 5. +# 'nl' sees this as line 4 since it does not number blank lines. +# 'cat -n' sees the above line as number 6. nl `basename $0` diff --git a/LDP/guide/docbook/abs-guide/obj-oriented.sh b/LDP/guide/docbook/abs-guide/obj-oriented.sh index 39b3a36e..07fcec8c 100644 --- a/LDP/guide/docbook/abs-guide/obj-oriented.sh +++ b/LDP/guide/docbook/abs-guide/obj-oriented.sh @@ -49,7 +49,7 @@ self.show_birthdate # Sat Mar 17 20:13:33 MST 1973 echo -# typeset -f -# to see the created functions (careful, it scrolls off the page). +# typeset -f +#+ to see the created functions (careful, it scrolls off the page). exit 0 diff --git a/LDP/guide/docbook/abs-guide/param-sub.sh b/LDP/guide/docbook/abs-guide/param-sub.sh index f8de3341..1f0ba144 100644 --- a/LDP/guide/docbook/abs-guide/param-sub.sh +++ b/LDP/guide/docbook/abs-guide/param-sub.sh @@ -24,9 +24,9 @@ echo "username2 = ${username2:-`whoami`}" # Compare to first instance, above. -# ============================================================= +# -# Reiterating: +# Once again: variable= # variable has been declared, but is set to null. diff --git a/LDP/guide/docbook/abs-guide/redir2.sh b/LDP/guide/docbook/abs-guide/redir2.sh index b8299773..53ee461c 100644 --- a/LDP/guide/docbook/abs-guide/redir2.sh +++ b/LDP/guide/docbook/abs-guide/redir2.sh @@ -1,4 +1,5 @@ #!/bin/bash +# redir2.sh if [ -z "$1" ] then @@ -23,12 +24,24 @@ done <"$Filename" # Redirects stdin to file $Filename. echo; echo "$count names read"; echo +exit 0 + # Note that in some older shell scripting languages, #+ the redirected loop would run as a subshell. -# Therefore, $count would return 0, the initialized value outside the loop. -# Bash and ksh avoid starting a subshell whenever possible, -# +so that this script, for example, runs correctly. -# -# Thanks to Heiner Steven for pointing this out. +# Therefore, $count would return 0, the initialized value outside the loop. +# Bash and ksh avoid starting a subshell *whenever possible*, +#+ so that this script, for example, runs correctly. +# (Thanks to Heiner Steven for pointing this out.) -exit 0 +# However . . . +# Bash *can* sometimes start a subshell in a *redirected* "while" loop. + +abc=hi +echo -e "1\n2\n3" | while read l + do abc="$l" + echo $abc + done +echo $abc + +# (Thanks, Bruno de Oliveira Schneider, for demonstrating this +#+ with the above snippet of code.) diff --git a/LDP/guide/docbook/abs-guide/rnd.sh b/LDP/guide/docbook/abs-guide/rnd.sh index 1fb04aab..c1350925 100644 --- a/LDP/guide/docbook/abs-guide/rnd.sh +++ b/LDP/guide/docbook/abs-guide/rnd.sh @@ -32,31 +32,31 @@ head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' # Assume output up to "sed" --------> | # is 0000000 1198195154\n -# sed begins reading characters: 0000000 1198195154\n. -# Here it finds a newline character, -# so it is ready to process the first line (0000000 1198195154). -# It looks at its <range><action>s. The first and only one is +# sed begins reading characters: 0000000 1198195154\n. +# Here it finds a newline character, +#+ so it is ready to process the first line (0000000 1198195154). +# It looks at its <range><action>s. The first and only one is # range action # 1 s/.* //p -# The line number is in the range, so it executes the action: -# tries to substitute the longest string ending with a space in the line -# ("0000000 ") with nothing (//), and if it succeeds, prints the result -# ("p" is a flag to the "s" command here, this is different from the "p" command). +# The line number is in the range, so it executes the action: +#+ tries to substitute the longest string ending with a space in the line +# ("0000000 ") with nothing (//), and if it succeeds, prints the result +# ("p" is a flag to the "s" command here, this is different from the "p" command). -# sed is now ready to continue reading its input. (Note that before -# continuing, if -n option had not been passed, sed would have printed -# the line once again). +# sed is now ready to continue reading its input. (Note that before +#+ continuing, if -n option had not been passed, sed would have printed +#+ the line once again). # Now, sed reads the remainder of the characters, and finds the end of the file. # It is now ready to process its 2nd line (which is also numbered '$' as # it's the last one). # It sees it is not matched by any <range>, so its job is done. -# In few word this sed commmand means: -# "On the first line only, remove any character up to the right-most space, -# then print it." +# In few word this sed commmand means: +# "On the first line only, remove any character up to the right-most space, +#+ then print it." # A better way to do this would have been: # sed -e 's/.* //;q' @@ -68,13 +68,13 @@ head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' # nothing (matches line) s/.* // # nothing (matches line) q (quit) -# Here, sed only reads its first line of input. -# It performs both actions, and prints the line (substituted) before quitting -# (because of the "q" action) since the "-n" option is not passed. +# Here, sed only reads its first line of input. +# It performs both actions, and prints the line (substituted) before quitting +#+ (because of the "q" action) since the "-n" option is not passed. # =================================================================== # -# A simpler altenative to the above 1-line script would be: +# An even simpler altenative to the above one-line script would be: # head -c4 /dev/urandom| od -An -tu4 exit 0 diff --git a/LDP/guide/docbook/abs-guide/script-detector.sh b/LDP/guide/docbook/abs-guide/script-detector.sh index 80fefb97..2852efff 100644 --- a/LDP/guide/docbook/abs-guide/script-detector.sh +++ b/LDP/guide/docbook/abs-guide/script-detector.sh @@ -18,3 +18,13 @@ do done exit 0 + +# Exercises: +# --------- +# 1) Modify this script to take as an optional argument +#+ the directory to scan for scripts +#+ (rather than just the current working directory). +# +# 2) As it stands, this script gives "false positives" for +#+ Perl, awk, and other scripting language scripts. +# Correct this. diff --git a/LDP/guide/docbook/abs-guide/tempfile-name.sh b/LDP/guide/docbook/abs-guide/tempfile-name.sh index 6f898d34..04b6f062 100644 --- a/LDP/guide/docbook/abs-guide/tempfile-name.sh +++ b/LDP/guide/docbook/abs-guide/tempfile-name.sh @@ -20,4 +20,7 @@ echo "Temp filename = "$temp_filename"" # sh tempfile-name.sh # Temp filename = temp.e19ea +# Compare this method of generating "unique" filenames +#+ with the 'date' method in ex51.sh. + exit 0 diff --git a/LDP/guide/docbook/abs-guide/tree.sh b/LDP/guide/docbook/abs-guide/tree.sh index a0dcc9dd..3e58949a 100644 --- a/LDP/guide/docbook/abs-guide/tree.sh +++ b/LDP/guide/docbook/abs-guide/tree.sh @@ -4,7 +4,7 @@ # Written by Rick Boivie. # Used with permission. # This is a revised and simplified version of a script -# by Jordi Sanfeliu (and patched by Ian Kjos). +#+ by Jordi Sanfeliu (and patched by Ian Kjos). # This script replaces the earlier version used in #+ previous releases of the Advanced Bash Scripting Guide. @@ -35,8 +35,8 @@ do echo "+---$dir" # ==> Display horizontal connector symbol... # ==> and print directory name. numdirs=`expr $numdirs + 1` # ==> Increment directory count. - if cd "$dir" ; then # ==> If can move to subdirectory... - search `expr $1 + 1` # with recursion ;-) + if cd "$dir" ; then # ==> If can move to subdirectory... + search `expr $1 + 1` # with recursion ;-) # ==> Function calls itself. cd .. fi diff --git a/LDP/guide/docbook/abs-guide/unalias.sh b/LDP/guide/docbook/abs-guide/unalias.sh index ed3d1715..6743cc2d 100644 --- a/LDP/guide/docbook/abs-guide/unalias.sh +++ b/LDP/guide/docbook/abs-guide/unalias.sh @@ -1,4 +1,5 @@ #!/bin/bash +# unalias.sh shopt -s expand_aliases # Enables alias expansion. diff --git a/LDP/guide/docbook/abs-guide/unprotect_literal.sh b/LDP/guide/docbook/abs-guide/unprotect_literal.sh index 79fb6fc7..3e864e64 100644 --- a/LDP/guide/docbook/abs-guide/unprotect_literal.sh +++ b/LDP/guide/docbook/abs-guide/unprotect_literal.sh @@ -101,7 +101,7 @@ echo $newestVar # Why in the world? # Setting the contents of a string variable containing character -#+ sequences that have a meaning to Bash is a general problem in +#+ sequences that have a meaning in Bash is a general problem in #+ script programming. # # This problem is now solved in eight lines of code diff --git a/LDP/guide/docbook/abs-guide/unset.sh b/LDP/guide/docbook/abs-guide/unset.sh index 376eeb2c..1c17a36f 100644 --- a/LDP/guide/docbook/abs-guide/unset.sh +++ b/LDP/guide/docbook/abs-guide/unset.sh @@ -5,7 +5,7 @@ variable=hello # Initialized. echo "variable = $variable" unset variable # Unset. - # Same effect as variable= + # Same effect as: variable= echo "(unset) variable = $variable" # $variable is null. exit 0 diff --git a/LDP/guide/docbook/abs-guide/upperconv.sh b/LDP/guide/docbook/abs-guide/upperconv.sh index dfaa5238..317601ee 100644 --- a/LDP/guide/docbook/abs-guide/upperconv.sh +++ b/LDP/guide/docbook/abs-guide/upperconv.sh @@ -11,7 +11,7 @@ then echo "Usage: $0 input-file output-file" exit $E_FILE_ACCESS fi # Will exit with same error - #+ even if input file ($1) not specified. + #+ even if input file ($1) not specified (why?). if [ -z "$2" ] then diff --git a/LDP/guide/docbook/abs-guide/usb.sh b/LDP/guide/docbook/abs-guide/usb.sh index b580046c..37c2aab6 100644 --- a/LDP/guide/docbook/abs-guide/usb.sh +++ b/LDP/guide/docbook/abs-guide/usb.sh @@ -5,6 +5,7 @@ # ==> # ==> Newer Linux distros (2004 or later) autodetect # ==> and install USB pen drives, and therefore don't need this script. +# ==> But, it's still instructive. # This code is free software covered by GNU GPL license version 2 or above. # Please refer to http://www.gnu.org/ for the full license text. diff --git a/LDP/guide/docbook/abs-guide/viewdata.sh b/LDP/guide/docbook/abs-guide/viewdata.sh index a509859c..04e5f20a 100644 --- a/LDP/guide/docbook/abs-guide/viewdata.sh +++ b/LDP/guide/docbook/abs-guide/viewdata.sh @@ -1,10 +1,11 @@ #!/bin/bash +# viewdata.sh # Conversion of VIEWDATA.BAT to shell script. DATAFILE=/home/bozo/datafiles/book-collection.data ARGNO=1 -# @ECHO OFF Command unnecessary here. +# @ECHO OFF Command unnecessary here. if [ $# -lt "$ARGNO" ] # IF !%1==! GOTO VIEWDATA then @@ -15,6 +16,6 @@ fi exit 0 # :EXIT0 -# GOTOs, labels, smoke-and-mirrors, and flimflam unnecessary. -# The converted script is short, sweet, and clean, -# which is more than can be said for the original. +# GOTOs, labels, smoke-and-mirrors, and flimflam unnecessary. +# The converted script is short, sweet, and clean, +#+ which is more than can be said for the original. diff --git a/LDP/guide/docbook/abs-guide/weirdvars.sh b/LDP/guide/docbook/abs-guide/weirdvars.sh index c1a9a24a..151ff412 100644 --- a/LDP/guide/docbook/abs-guide/weirdvars.sh +++ b/LDP/guide/docbook/abs-guide/weirdvars.sh @@ -8,9 +8,9 @@ echo "$var" # '(]\{}$" Doesn't make a difference. echo IFS='\' -echo $var # '(] {}$" \ converted to space. +echo $var # '(] {}$" \ converted to space. Why? echo "$var" # '(]\{}$" -# Examples above supplied by S.C. +# Examples above supplied by Stephane Chazelas. exit 0 diff --git a/LDP/guide/docbook/abs-guide/wgetter2.bash b/LDP/guide/docbook/abs-guide/wgetter2.bash index 97806b97..5dc023b8 100644 --- a/LDP/guide/docbook/abs-guide/wgetter2.bash +++ b/LDP/guide/docbook/abs-guide/wgetter2.bash @@ -23,6 +23,7 @@ # ======================================================================= # changelog: +# 07/02/2005. Fixups by Little Monster. # 02/02/2005. Minor additions by Little Monster. # (See after # +++++++++++ ) # 29/01/2005. Minor stylistic edits and cleanups by author of ABS Guide. @@ -236,50 +237,53 @@ list_func() # Gives the user the option to use the -i option to wget, #+ and a list of URLs. { while [ 1 ]; do - echo "Enter the name of the file containing URL's (press q to change your mind)." + echo "Enter the name of the file containing URL's (press q to change your +mind)." read urlfile - if [ ! -e $urlfile ] && [ $urlfile != q ]; then + if [ ! -e "$urlfile" ] && [ "$urlfile" != q ]; then # Look for a file, or the quit option. echo "That file does not exist!" - elif [ $urlfile = q ]; then # Check quit option. - echo "Not using a URL list." + elif [ "$urlfile" = q ]; then # Check quit option. + echo "Not using a url list." return else - echo "Using $urlfile." - echo "If you gave me URL's on the command line, I'll use those first." + echo "using $urlfile." + echo "If you gave me url's on the command line, I'll use those first." # Report wget standard behaviour to the user. - lister=" -i $urlfile" # This is what we will pass to wget. + lister=" -i $urlfile" # This is what we want to pass to wget. return fi done } -cookie_func() # Give the user the option to use a different cookie file. +cookie_func() # Give the user the option to use a different cookie file. { while [ 1 ]; do - echo "Change the cookies file. Press return if you don't want to change it." + echo "Change the cookies file. Press return if you don't want to change +it." read Cookies # NB: this is not the same as Cookie, earlier. # There is an 's' on the end. # Bit like chocolate chips. - if [ -z $Cookies ]; then # Escape clause for wusses. + if [ -z "$Cookies" ]; then # Escape clause for wusses. return - elif [ ! -e $Cookies ]; then - echo "File does not exist. Try again." # Keep 'em going . . . + elif [ ! -e "$Cookies" ]; then + echo "File does not exist. Try again." # Keep em going . . . else - CookiesON=" --load-cookies $Cookies" # File is good. Let's use it! + CookiesON=" --load-cookies $Cookies" # File is good -- let's use it! return fi done } + run_func() { if [ -z "$OPTARG" ]; then # Test to see if we used the in-line option or the query one. - if [ ! -d "$savePath" ]; then # In case directory doesn't exist . . . + if [ ! -d "$savePath" ]; then # In case directory doesn't exist . . . echo "$savePath does not appear to exist." echo "Please supply path and filename of saved wget commands:" read newFile @@ -289,6 +293,7 @@ if [ -z "$OPTARG" ]; then read newFile done + # ------------------------------------------------------------------------- # if [ -z ( grep wget ${newfile} ) ]; then # Assume they haven't got the right file and bail out. @@ -301,6 +306,7 @@ if [ -z "$OPTARG" ]; then # If anyone wants to fix it, feel free! # ------------------------------------------------------------------------- + filePath="${newFile}" else echo "Save path is $savePath" @@ -358,7 +364,7 @@ done shift $((OPTIND - 1)) # Do funky magic stuff with $#. -if [ -z "$1" ] && [ ! -z $lister ]; then +if [ -z "$1" ] && [ -z "$lister" ]; then # We should be left with at least one URL #+ on the command line, unless a list is #+ being used -- catch empty CL's. diff --git a/LDP/guide/docbook/abs-guide/what.sh b/LDP/guide/docbook/abs-guide/what.sh index b491e91a..043dcd61 100644 --- a/LDP/guide/docbook/abs-guide/what.sh +++ b/LDP/guide/docbook/abs-guide/what.sh @@ -11,6 +11,7 @@ do done exit 0 + # You may wish to redirect output of this script, like so: # ./what.sh >>whatis.db # or view it a page at a time on stdout,