diff --git a/LDP/guide/docbook/abs-guide/Change.log b/LDP/guide/docbook/abs-guide/Change.log index f86cce7e..2a0e0664 100644 --- a/LDP/guide/docbook/abs-guide/Change.log +++ b/LDP/guide/docbook/abs-guide/Change.log @@ -7,8 +7,8 @@ ================================================================== - Current version = 6.6 - Dated 11/27/12 + Current version = 10 + Dated 03/10/14 http://bash.deta.in/abs-guide-latest.tar.bz2 http://bash.deta.in/abs-guide.pdf @@ -16,25 +16,28 @@ Announcements - Version 6.6 released. + Version 10 released. - The ABS Guide no longer supports Sun Solaris and Apple versions - of UNIX, due to predatory corporate litigation policies. - If you're a Solaris and/or Apple person, then customer service - may be your friend, but the author of the ABS Guide is not. + The ABS Guide is now in the Public Domain. + The author will no longer support or update this document. ****************************************************************** It has come to the attention of the author that unauthorized electronic and print editions of this book are being sold commercially on itunes and elsewhere. These are illegal and pirated editions produced without the author's permission, and readers of this book - are cautioned not to purchase them. + are advised not to purchase them. If you possess a printed copy of + this book, then be aware that its copyright is invalid, and that + therefore you have the right to copy or reproduce it. ****************************************************************** Honor roll of supporters. - The following persons have made unsolicited monetary contributions - to the ABS Guide author. + The following persons have made much welcomed, though unsolicited + monetary contributions to the ABS Guide author. Matthias Gutfeldt + . . . + . . . + . . . A big Thank You! ****************************************************************** @@ -42,6 +45,63 @@ ==================================================================== +Version 10, PublicDomain release. + +1) In "Miscellany" chapter: + In "Optimizations" section, + Quite a number of significant additions and examples, per Erik + Brandsberg. + (Thank you!) + In "Shell Wrappers" section, + Added "ex56py.sh" example of embedded Python script. + Added "speech.sh" example of a talking script. + +2) In "Another Look At Variables" chapter + Made reference to $USERNAME more ambiguous, per note from Dominique + Brazziel + At "PS4" entry, + Added Erik Brandsberg's suggestion for embedding diagnostic info. + (Thank you!) + +3) In "Loops" section of "Loops and Branches" chapter: + Added note concerning omitting do/done with curly-bracket + delimited command block. + (Thank you, YongYe!) + Added example of parameterized [list] in a for-loop. + +4) In "External Commands" chapter: + At "diff" entry, added note about return code 2 on failed binary + file comparison. + At "dd" entry, added example script for preparing a bootable SD card + for the Raspberry Pi. + At "paste" entry, added short example. + Added entry for "xz" in File and Archiving Commands subsection. + +5) In "Here Documents" chapter: + Fixup to multi-line variable example, to eliminate error message. + (Thank you, Ken B!) + +6) Fixed some example scripts to remove quoting of matched variable with + =~ operator. + +7) In "Parameter Substitution and Expansion" Table, + Made minor changes in because Bash updates changed the behavior of certain + parameter operators, and made not of same. Grrr! + +8) In "History Commands" appendix: + Note about enabling the history mechanism within a script, + per comment by David (dhw). + +9) In "Contributed Scripts" appendix: + Added "sedappend.sh" script example of inserting text in a file + using sed. + +10) In the Bibliography: + Added an entry for Daniel Goldman's excellent ebook introduction to sed. + +10) Fixed and/or updated some more scripts. + + Version 6.6, Ytterbiumberry* release diff --git a/LDP/guide/docbook/abs-guide/INDEX00.sgml b/LDP/guide/docbook/abs-guide/INDEX00.sgml index 0a22dab7..8e22cf0a 100644 --- a/LDP/guide/docbook/abs-guide/INDEX00.sgml +++ b/LDP/guide/docbook/abs-guide/INDEX00.sgml @@ -840,7 +840,10 @@ Arrays Associative arrays - + + more efficient + than conventional arrays + Bracket notation Concatenating, @@ -1008,13 +1011,15 @@ Blocks of code + + Iterating / looping + Redirection - Script - example: - redirecting output of a a code block + Script example: + Redirecting output of a a code + block Bootable flash drives, creating @@ -1254,6 +1259,10 @@ Random access on a data stream + Raspberry Pi, + script for preparing a bootable SD + card + Swapfiles, initializing Thread on @@ -1675,6 +1684,8 @@ Podcasting Poem + Speech + generation Towers of Hanoi Graphic @@ -2659,6 +2670,16 @@ + Raspberry Pi (single-board computer) + + + + Script for preparing a bootable SD card + + + + @@ -3297,7 +3318,10 @@ Array, as return value from - a function + a function + Associative array + more efficient + than a numerically-indexed array Capturing the return value of a function, using @@ -3375,6 +3399,9 @@ linkend="setvaremb">Setting script variable to a block of embedded sed or awk code + Speeding up script execution by disabling unicode + Subshell variable, accessing outside the subshell @@ -3451,7 +3478,14 @@ uname, output system information Unicode, encoding standard - for representing letters and symbols + for representing letters and symbols + + Disabling unicode + to optimize script + + + Uninitialized variables uniq, filter to remove duplicate lines from a sorted file diff --git a/LDP/guide/docbook/abs-guide/README b/LDP/guide/docbook/abs-guide/README index 92e83560..27faa1f5 100644 --- a/LDP/guide/docbook/abs-guide/README +++ b/LDP/guide/docbook/abs-guide/README @@ -127,5 +127,6 @@ here-commsub.sh UseGetOpt.sh: line 4 (comment) UseGetOpt-2.sh: line 11 (comment) -bash64.sh - line 4 (comment) +bash64.sh: line 4 (comment) + +speech.sh: line 14 diff --git a/LDP/guide/docbook/abs-guide/abs-guide.sgml b/LDP/guide/docbook/abs-guide/abs-guide.sgml index bd9d3bb3..07ebd24f 100644 --- a/LDP/guide/docbook/abs-guide/abs-guide.sgml +++ b/LDP/guide/docbook/abs-guide/abs-guide.sgml @@ -66,6 +66,7 @@ + @@ -173,6 +174,7 @@ + @@ -290,6 +292,7 @@ + @@ -383,11 +386,15 @@ + + + + @@ -411,21 +418,14 @@ - 6.6 - 27 Nov 2012 + 10 + 10 Mar 2014 978-1-4357-5219-1 - - 6.4 - 30 Aug 2011 - mc - 'VORTEXBERRY' release - - 6.5 05 Apr 2012 @@ -440,6 +440,12 @@ 'YTTERBIUMBERRY' release + + 10 + 10 Mar 2014 + mc + 'PUBLICDOMAIN' release + @@ -447,7 +453,7 @@ This tutorial assumes no previous knowledge of - scripting or programming, but progresses rapidly toward an + scripting or programming, yet progresses rapidly toward an intermediate/advanced level of instruction . . . all the while sneaking in little nuggets of UNIX wisdom and lore. It @@ -460,6 +466,9 @@ This book is suitable for classroom use as a general introduction to programming concepts. + This document is herewith granted to the Public Domain. + No copyright! + @@ -484,7 +493,7 @@ The shell is a command interpreter. More than just the insulating layer between the operating system kernel and the user, - it's also a fairly powerful programming language. A shell program, + it's also a fairly powerful programming language. A shell program, called a script, is an easy-to-use tool for building applications by gluing together system calls, tools, utilities, and compiled binaries. Virtually the @@ -1640,7 +1649,7 @@ fi linkend="fstrangeref">acceptable as a function name. :() { - echo "The name of this function is "$FUNCNAME" + echo "The name of this function is "$FUNCNAME" " # Why use a colon as a function name? # It's a way of obfuscating your code. } @@ -1650,7 +1659,10 @@ fi # The name of this function is : This is not portable - behavior, and therefore not a recommended practice. + behavior, and therefore not a recommended practice. + In fact, more recent releases of Bash do not permit + this usage. An underscore _ works, + though. @@ -2281,7 +2293,14 @@ echo "First line is $firstline; second line is $secondline" # Won't work. # Thanks, S.C. - + + + It is possible to iterate a code block + using a non-standard + for-loop. + + @@ -5792,7 +5811,9 @@ echo "Input" | show_input_type # PIPE directories. If set on a directory, it restricts write permission. Setting the sticky bit adds a t - to the permissions on the file or directory listing. + to the permissions on the file or directory listing. + This restricts altering or deleting specific files + in that directory to the owner of those files. @@ -8104,8 +8125,16 @@ echo "Last command argument processed = $last_cmd_arg" The quartenary prompt, shown at the beginning of each line of output when invoking a script with the - -x option. - It displays as +. + -x [verbose trace] + option. It displays as + +. + + As a debugging aid, it may be useful to embed diagnostic + information in $PS4. +P4='$(read time junk < /proc/$$/schedstat; echo "@@@ $time @@@ " )' +# Per suggestion by Erik Brandsberg. +set -x +# Various commands follow ... @@ -8360,9 +8389,10 @@ echo "Your favorite song is $song." $TERM, $USER, and $USERNAME are not Bash builtins. These are, - however, often set as environmental - variables in one of the Bash startup files. environmental variables in + one of the Bash or + login startup files. $SHELL, the name of the user's login shell, may be set from /etc/passwd or in an init @@ -10459,6 +10489,15 @@ echo $c # something_else &fileinfo; + + The [list] in a + for loop may be parameterized. + + + Operating on a parameterized file list + &fileinfo01; + + If the [list] in a @@ -10590,6 +10629,41 @@ done &ex24; + The + keywords + do and done delineate + the for-loop command block. However, + these may, in certain contexts, be omitted by framing the + command block within curly + brackets + + for((n=1; n<=10; n++)) +# No do! +{ + echo -n "* $n *" +} +# No done! + + +# Outputs: +# * 1 ** 2 ** 3 ** 4 ** 5 ** 6 ** 7 ** 8 ** 9 ** 10 * +# And, echo $? returns 0, so Bash does not register an error. + + +echo + + +# But, note that in a classic for-loop: for n in [list] ... +#+ a terminal semicolon is required. + +for n in 1 2 3 +{ echo -n "$n "; } +# ^ + + +# Thank you, YongYe, for pointing this out. + + @@ -12114,6 +12188,7 @@ let "z += 3" # Quotes permit the use of spaces in variable assignment. write xargs xrandr + xz yacc yes zcat @@ -15925,6 +16000,25 @@ done cut, useful for creating system log files. + + + + bash$ cat items +alphabet blocks + building blocks + cables + +bash$ cat prices +$1.00/dozen + $2.50 ea. + $3.75 + +bash$ paste items prices +alphabet blocks $1.00/dozen + building blocks $2.50 ea. + cables $3.75 + + @@ -16458,7 +16552,7 @@ Here is some text. word Linux in the main source file for this book. bash$ grep Linux abs-book.sgml | wc -l -50 +138 @@ -16589,7 +16683,7 @@ tr -d 0-9 <filename # jabh.sh x="wftedskaebjgdBstbdbsmnjgz" -echo $x | tr "a-z" "oh, turtleneck Phrase Jar!" +echo $x | tr "a-z" 'oh, turtleneck Phrase Jar!' # Based on the Wikipedia "Just another Perl hacker" article. @@ -17167,22 +17261,24 @@ function write_utf8_string { Shell archiving utility. - The text files in a shell archive are concatenated - without compression, and the resultant archive - is essentially a shell script, complete with - #!/bin/sh header, containing all the - necessary unarchiving commands, as well as the files - themselves. Shar archives - still show up in Usenet newsgroups, but otherwise - shar has been replaced by - tar/gzip. The - unshar command unpacks - shar archives. - The mailshar command - is a Bash script that uses shar to - concatenate multiple files into a single one for e-mailing. - This script supports compression and uuencoding. + The text and/or binary files in a shell archive are + concatenated without compression, and the resultant + archive is essentially a shell script, complete with + #!/bin/sh header, containing all the + necessary unarchiving commands, as well as the files + themselves. Unprintable binary characters in the target + file(s) are converted to printable ASCII characters in the + output shar file. Shar + archives still show up in Usenet newsgroups, + but otherwise shar has been replaced + by tar/gzip. + The unshar command unpacks + shar archives. The + mailshar command is a Bash script that + uses shar to concatenate multiple files + into a single one for e-mailing. + This script supports compression and uuencoding. @@ -17599,6 +17695,43 @@ pax -rf daily_backup.pax ~/bsd-server/files + + xz + unxz + xzcat + + xz + + + command + xz + + + unxz + + + command + unxz + + + xzcat + + + command + xzcat + + + A new high-efficiency compression tool, backward compatible + with lzma, and with an invocation + syntax similar to gzip. For + more information, see the Wikipedia + entry. + + + + + @@ -17964,11 +18097,14 @@ file $DIRECTORY/* | fgrep $KEYWORD wdiff, xdiff, and mgdiff. - The diff command returns an exit - status of 0 if the compared files are identical, and 1 if - they differ. This permits use of diff - in a test construct within a shell script (see - below). + The diff + command returns an exit status of 0 + if the compared files are identical, and + 1 if they differ (or + 2 when binary + files are being compared). This permits use of + diff in a test construct within a shell + script (see below). A common use for diff is generating @@ -20679,10 +20815,16 @@ dd if=$filename conv=ucase > $filename.uppercase Likewise, dd - can create bootable flash drives. - dd if=image.iso of=/dev/sdb - See - Marlow's Bootable USB Keys site. + can create bootable flash drives and SD cards. + + dd if=image.iso of=/dev/sdb + + + + Preparing a bootable SD card for the + <emphasis>Raspberry Pi</emphasis> + &rpsdcard; + @@ -25791,7 +25933,8 @@ done variable=$(cat <<SETVAR This variable runs over multiple lines. -SETVAR) +SETVAR +) echo "$variable" @@ -28073,7 +28216,7 @@ drwxr-xr-x 40 bozo bozo 2048 Feb 6 14:04 .. execute). An interesting use of a two-condition and - list from an ealy version of YongYe's from an early version of YongYe's Tetris game script: @@ -28192,8 +28335,7 @@ done # But NOT false && ( true || echo false ) # (nothing echoed) -# Note left-to-right grouping and evaluation of statements, -#+ since the logic operators "&&" and "||" have equal precedence. +# Note left-to-right grouping and evaluation of statements. # It's usually best to avoid such complexities. @@ -31006,9 +31148,29 @@ fi + It is, of course, possible to embed even more exotic scripting + languages within shell wrappers. Python, + for example ... + + + + Python embedded in a <firstterm>Bash</firstterm> script + &ex56py; + + + Wrapping a script around mplayer + and the Google's translation server, you can create something + that talks back to you. + + + + A script that speaks + &speech0; + + One interesting example of a complex shell wrapper is Martin Matusiak's undvd + url="http://sourceforge.net/projects/undvd/">undvd script, which provides an easy-to-use command-line interface to the complex mencoder @@ -31323,6 +31485,175 @@ grep "$word" "$file" The cat command seems especially prone to overuse in scripts. + + Disabling certain Bash options can speed up scripts. + As Erik Brandsberg points out: + If you don't need Unicode support, you can + get potentially a 2x or more improvement in speed by + simply setting the LC_ALL variable. + + export LC_ALL=C + + [specifies the locale as ANSI C, + thereby disabling Unicode support] + +[In an example script ...] + +Without [Unicode support]: +erik@erik-desktop:~/capture$ time ./cap-ngrep.sh +live2.pcap > out.txt + + real 0m20.483s + user 1m34.470s + sys 0m12.869s + +With [Unicode support]: +erik@erik-desktop:~/capture$ time ./cap-ngrep.sh +live2.pcap > out.txt + + real 0m50.232s + user 3m51.118s + sys 0m11.221s + +A large part of the overhead that is optimized is, I believe, +regex match using [[ string =~ REGEX ]], +but it may help with other portions of the code as well. +I hadn't [seen it] mentioned that this optimization helped +with Bash, but I had seen it helped with "grep," +so why not try? + + + Certain operators, notably expr, are very inefficient + and might be replaced by double + parentheses arithmetic expansion. + See . + + +Math tests + +math via $(( )) +real 0m0.294s +user 0m0.288s +sys 0m0.008s + +math via expr: +real 1m17.879s # Much slower! +user 0m3.600s +sys 0m8.765s + +math via let: +real 0m0.364s +user 0m0.372s +sys 0m0.000s + + + Condition testing + constructs in scripts deserve close scrutiny. Substitute + case for if-then constructs and combine tests + when possible, to minimize script execution time. Again, + refer to . + + + Test using "case" construct: +real 0m0.329s +user 0m0.320s +sys 0m0.000s + + +Test with if [], no quotes: +real 0m0.438s +user 0m0.432s +sys 0m0.008s + + +Test with if [], quotes: +real 0m0.476s +user 0m0.452s +sys 0m0.024s + + +Test with if [], using -eq: +real 0m0.457s +user 0m0.456s +sys 0m0.000s + + + + Erik Brandsberg recommends using associative arrays in preference to + conventional numeric-indexed arrays in most cases. When + overwriting values in a numeric array, there is a significant + performance penalty vs. associative arrays. Running a test + script confirms this. See . + + + Assignment tests + +Assigning a simple variable +real 0m0.418s +user 0m0.416s +sys 0m0.004s + +Assigning a numeric index array entry +real 0m0.582s +user 0m0.564s +sys 0m0.016s + +Overwriting a numeric index array entry +real 0m21.931s +user 0m21.913s +sys 0m0.016s + +Linear reading of numeric index array +real 0m0.422s +user 0m0.416s +sys 0m0.004s + +Assigning an associative array entry +real 0m1.800s +user 0m1.796s +sys 0m0.004s + +Overwriting an associative array entry +real 0m1.798s +user 0m1.784s +sys 0m0.012s + +Linear reading an associative array entry +real 0m0.420s +user 0m0.420s +sys 0m0.000s + +Assigning a random number to a simple variable +real 0m0.402s +user 0m0.388s +sys 0m0.016s + +Assigning a sparse numeric index array entry randomly into 64k cells +real 0m12.678s +user 0m12.649s +sys 0m0.028s + +Reading sparse numeric index array entry +real 0m0.087s +user 0m0.084s +sys 0m0.000s + +Assigning a sparse associative array entry randomly into 64k cells +real 0m0.698s +user 0m0.696s +sys 0m0.004s + +Reading sparse associative index array entry +real 0m0.083s +user 0m0.084s +sys 0m0.000s + + + Use the time and times tools to profile computation-intensive commands. Consider rewriting time-critical @@ -32680,7 +33011,10 @@ echo $a # 6 limited support for associative arrays. It's a bare-bones implementation, and it lacks the much of the functionality of such - arrays in other programming languages. + arrays in other programming languages. Note, however, + that associative arrays in + Bash seem to execute faster and more efficiently than + numerically-indexed arrays. @@ -32751,7 +33085,6 @@ echo $a # 6 which now is made publicly available." This means it can be explicitly invoked in a script, rather than just being a behind-the-scenes mechanism used by Bash. - See http://linux010.blogspot.com/2008/12/bash-process-substitution.html. @@ -33184,6 +33517,7 @@ echo -e "\xE2\x98\xB0" echo -e '\u220F' # PI (Greek letter and mathematical symbol) echo -e '\u0416' # Capital "ZHE" (Cyrillic letter) echo -e '\u2708' # Airplane (Dingbat font) symbol +echo -e '\u2622' # Radioactivity trefoil echo -e "The amplifier circuit requires a 100 \u2126 pull-up resistor." @@ -33375,16 +33709,28 @@ echo HOWTO. Of late, he has been trying his (heavy) hand at fiction: Dave Dawson - Over Berlin (excerpt). He also has a few + Over Berlin (First Installment) +Dave Dawson + Over Berlin (Second Installment) and +Dave Dawson + Over Berlin (Third Installment) + +. He also has a few Instructables (here, here, here, -and here + url="http://www.instructables.com/id/Binguino-An-Arduino-based-Bingo-Number-Generato/">here, +here, + +here, and +here to his (dis)credit. A Linux user since 1995 (Slackware 2.2, kernel 1.2.1), @@ -33421,15 +33767,9 @@ and Where to Go For Help The author - will infrequently, if not too busy (and in a good mood), - answer general scripting questions. - E-mails from certain spam-infested TLDs - (61, 202, 211, 218, 220, etc.) will be trapped by spam - filters and deleted unread. - If you have a problem getting a particular script - to work, you would be well advised to post to the comp.os.unix.shell Usenet - newsgroup. + is no longer supporting or updating this document. He will not + answer questions about this book or about general scripting topics. + If you need assistance with a schoolwork assignment, @@ -33486,8 +33826,9 @@ and - Update: upgraded to a T60 Thinkpad - running Mandriva 2010. No longer starving <g>. + Update: upgraded to a T61 Thinkpad + running Mandriva 2011. No longer starving <g>, + but not too proud to accept donations. @@ -33667,9 +34008,10 @@ and Omair Eshkenazi, Geoff Lee, Graham Ewart, JuanJo Ciarlante, Cliff Bamford, Nathan Coulter, Ramses Rodriguez Martinez, Evgeniy Ivanov, Craig Barnes, George Dimitriu, Kevin LeBlanc, - Antonio Macchi, Tomas Pospisek, David Wheeler, Andreas Kühne, - Pádraig Brady, Joseph Steinhauser, and David Lawyer - (himself an author of four HOWTOs). + Antonio Macchi, Tomas Pospisek, David Wheeler, Erik Brandsberg, + YongYe, Andreas Kühne, Pádraig Brady, Joseph + Steinhauser, and David Lawyer (himself an author of four + HOWTOs). My gratitude to Chet Ramey and Brian Fox for writing Bash, @@ -33770,9 +34112,34 @@ and Covers much of the same material as the ABS Guide, though in a different style. * + + + + + DanielGoldman + + <ulink url="http://www.sed-book.com/">Definitive Guide + to Sed</ulink> + 1st edition + + 2013 + + + This ebook is an excellent introduction to + sed. Rather than being a conversion from + a printed volume, it was specifically designed and formatted + for viewing on an ebook reader. Well-written, informative, + and useful as a reference as well as a tutorial. Highly + recommended. + * + + + + + DaleDougherty @@ -33790,7 +34157,7 @@ and Unfolding the full power of shell scripting requires at least a passing familiarity with sed - and awk. This is the standard + and awk. This is the classic tutorial. It includes an excellent introduction to Regular Expressions. Recommended. * @@ -35037,6 +35404,12 @@ url="http://nixshell.wordpress.com/2011/07/13/arcade-games-written-in-a-shell-sc &base64; + + Inserting text in a file using + <firstterm>sed</firstterm> + &sedappend; + + The Gronsfeld Cipher &gronsfeld; @@ -35055,6 +35428,17 @@ url="http://nixshell.wordpress.com/2011/07/13/arcade-games-written-in-a-shell-sc &basicsreviewed; + + Testing execution times of various commands + &testexectime; + + + + Associative arrays vs. conventional arrays (execution + times) + &assocarrtest; + + @@ -35409,47 +35793,49 @@ url="http://nixshell.wordpress.com/2011/07/13/arcade-games-written-in-a-shell-sc - - If var not set, evaluate expression + + If var not set, evaluate expression as $DEFAULT * - + If var not set or is empty, - evaluate expression as $DEFAULT + evaluate expression as + $DEFAULT * - + If var not set, evaluate expression as $DEFAULT * - - If var not set, evaluate expression + + If var not set or is empty, evaluate expression as $DEFAULT * - + If var set, evaluate expression as $OTHER, otherwise as null string - + If var set, evaluate expression as $OTHER, otherwise as null string - + If var not set, print $ERR_MSG and abort script with an exit status of 1.* - + If var not set, print $ERR_MSG and abort script with an exit status of 1.* @@ -35472,6 +35858,8 @@ url="http://nixshell.wordpress.com/2011/07/13/arcade-games-written-in-a-shell-sc * If var is set, evaluate the expression as $var with no side-effects. + # Note that some of the above behavior + of operators has changed from earlier versions of Bash. @@ -35795,7 +36183,7 @@ url="http://nixshell.wordpress.com/2011/07/13/arcade-games-written-in-a-shell-sc One important difference between the utilities is that while shell scripts can easily pass arguments to sed, it - is more complicated for awk (see + is more cumbersome for awk (see and ). @@ -35874,6 +36262,13 @@ url="http://nixshell.wordpress.com/2011/07/13/arcade-games-written-in-a-shell-sc address-range (equivalent of tr) + + + insert + Insert pattern at address indicated in file Filename. + Usually used with + in-place option. + global @@ -35975,6 +36370,16 @@ pattern=BEGIN Compress all consecutive sequences of zeroes into a single zero. + + + Prints "How far are you along?" as first line, + "Working on it" as second. + + + + Inserts 'Linux is great.' at line 5 of the file + file.txt. + Delete all lines containing GUI. @@ -36054,12 +36459,13 @@ pattern=BEGIN + For a more extensive treatment of sed, - check the appropriate references in the . + refer to the pertinent references + in the . @@ -36411,7 +36817,9 @@ correctly. Here are some of those templates that work correctly: Misuse of shell builtins (according to Bash documentation) empty_function() {} Missing keyword - or command + or command, or permission problem (and diff return code + on a failed binary file comparison). @@ -37441,7 +37849,13 @@ history # No output. var=$(history); echo "$var" # $var is empty. -# History commands disabled within a script. +# History commands are, by default, disabled within a script. +# However, as dhw points out, +#+ set -o history +#+ enables the history mechanism. + +set -o history +var=$(history); echo "$var" # 1 var=$(history) @@ -37914,10 +38328,10 @@ var=$(history); echo "$var" # $var is empty. Ted Davis' Shell - Scripts on the PC site has a set of comprehensive + Scripts on the PC site had a set of comprehensive tutorials on the old-fashioned art of batch file - programming. Certain of his ingenious techniques could conceivably - have relevance for shell scripts. + programming. Unfortunately the page has vanished without a + trace. @@ -39516,6 +39930,11 @@ thegrendel@theriver.com 27 Nov 2012 YTTERBIUMBERRY release: Minor update. + + + 10 Mar 2014 + YTTERBIUMBERRY release: License change. + @@ -39593,128 +40012,81 @@ thegrendel@theriver.com Copyright The Advanced Bash Scripting - Guide is - copyright - 2000, by Mendel Cooper. - The author also asserts copyright on all previous versions of - this document. No other person or entity holds legal - copyright to this work. - The author intends that this book be released - into the Public Domain after a period of 14 years from - initial publication, i.e., in 2014. In the early years - of the American republic this was the duration statutorily - granted to a copyrighted work. + Guide is herewith granted to the PUBLIC DOMAIN. + This has the following implications and consequences. - This blanket copyright recognizes and protects the rights of all - contributors to this document. + +A. All previous releases of the Advanced Bash Scripting Guide + are as well granted to the Public Domain. - This document may only be distributed subject to the terms - and conditions set forth in the Open Publication - License (version 1.0 or later), http://www.opencontent.org/openpub/. - The following license options also apply. - +A1. All printed editions, whether authorized by the author or not, + are as well granted to the Public Domain. This legally overrides + any stated intention or wishes of the publishers. Any statement + of copyright is void and invalid. + THERE ARE NO EXCEPTIONS TO THIS. - A. Distribution of substantively modified versions of this document - is permitted only under the following provisions. +A2. Any release of the Advanced Bash Scripting Guide, whether in + electronic or print form is granted to the Public Domain by the + express directive of the author and previous copyright holder, Mendel + Cooper. No other person(s) or entities have ever held a valid copyright. -A1. The modified document must clearly indicate that it is derivative - of the original Advanced Bash Scripting Guide, and the original - author, Mendel Cooper, must be listed as the primary author. +B. As a Public Domain document, unlimited copying and distribution rights + are granted. There can be NO restrictions. If anyone has published or will + in the future publish an original or modified version of this document, + then only additional original material may be copyrighted. The core + work will remain in the Public Domain. -A2. The modified or derivative document must clearly indicate which portions - of the text differ or deviate from the original document. A notice must - be present, stating that the original author does not necessarily - endorse the changes to the original. - -A3. The modified or derivative document must be distributed under this - same license, and the original author's copyright, as applicable, - may not be modified. - -A4. This License Appendix is invariant, may not be modified, and may not - be omitted from any otherwise modified variants or derivatives of - this document. - - -B. This document, or any modified or derivative version thereof, may - NOT be distributed encrypted or with any form of DRM (Digital Rights - Management) or content-control mechanism embedded in it. Nor may this - document or any derivative thereof be bundled with other DRM-ed works. - -C. If this document (or any previous version or derivative thereof) - is made available on a Web or ftp site, then the file(s) must be - publicly accessible. No password or other access restrictions to - its download may be imposed. - -D. Distribution of the original work in any standard (paper) book form - requires explicit permission from the copyright holder. - -E. In the event that the author or maintainer of this document cannot - be contacted, the Linux Documentation Project is authorized to - take over custodianship of the document and name a new maintainer, - who would then have the right to update and modify the document. - - - Without explicit written permission - from the author, distributors and publishers (including on-line - publishers) are prohibited from imposing any additional conditions, + By law, distributors and publishers (including on-line + publishers) are prohibited from imposing any conditions, strictures, or provisions on this document, any previous versions, - or any derivative versions. As of this update, the author asserts + or any derivative versions. The author asserts that he has not entered into any contractual obligations that would alter the foregoing declarations. Essentially, you may freely distribute this book or any - derivative thereof in electronic form. - - If you display or distribute this document, any previous - versions thereof, or any derivatives thereof under any license - except the one above, then you are required to obtain the author's - written permission. Failure to do so may terminate your distribution - rights. + derivative thereof in electronic or printed form. If you have previously + purchased or are in possession of a printed copy of a current or + previous edition, you have the LEGAL RIGHT to copy and/or redistribute + it, regardless of any copyright notice. Any copyright notice is + void. - Additionally, the following waiver - of end-user rights applies: + Additionally, the author wishes to state his intention + that: -By copying or distributing this book you WAIVE THE RIGHT -to use the materials within, or any portion thereof, in a patent or copyright +If you copy or distribute this book, kindly DO NOT +use the materials within, or any portion thereof, in a patent or copyright lawsuit against the Open Source community, its developers, its distributors, or against any of its associated software or documentation including, but not limited to, the Linux kernel, Open Office, Samba, -and Wine. You further WAIVE THE RIGHT to use any of the materials within +and Wine. Kindly DO NOT use any of the materials within this book in testimony or depositions as a plaintiff's "expert witness" in any lawsuit against the Open Source community, any of its developers, its distributors, or any of its associated software or documentation. -Violation of this provision retroactively invalidates your license -to use or access this book and may subject you to legal sanctions. + - These are very liberal terms, and they should not hinder any + A Public Domain license essentially does not restrict ANY legitimate distribution or use of this book. The author especially encourages its (royalty-free!) use for classroom and instructional purposes. - Certain of the scripts contained in this document are, - where noted, in the Public Domain. These scripts are exempt from - the foregoing license and copyright restrictions. - - The print and other commercial rights to this book are - available. Please contact the author if - interested. To date, limited print rights (Lulu edition) - have been granted to Steve Glines and to no one - else. + + To date, limited print rights (Lulu edition) have been granted + to one individual and to no one else. Neither + that individual nor Lulu holds or ever has held a valid copyright. It has come to the attention of the author that unauthorized electronic and print editions of this book are being sold commercially on itunes and elsewhere. These are illegal - and pirated editions produced without the author's permission, and - readers of this book are strongly urged not to purchase - them. The free authorized edition is available here and on - mirror sites. + class=registered>itunes, amazon.com + and elsewhere. These are illegal and pirated editions produced + without the author's permission, and readers of this book are + strongly urged not to purchase them. In fact, these pirated editions are + now legal, but necessarily fall into the Public Domain, and any + copyright notices contained within them are invalid and void. The author produced this book in a manner consistent with the spirit of the LDP @@ -39749,7 +40121,7 @@ to use or access this book and may subject you to legal sanctions.Russian, Czech, Chinese, - Indonesian, Dutch, Romanian, and Bulgarian translations are also + Indonesian, Dutch, Romanian, Bulgarian, and Turkish translations are also available or in progress. If you wish to translate this document into another language, please feel free to do so, subject to the terms stated above. The author wishes to be notified of such diff --git a/LDP/guide/docbook/abs-guide/array-function.sh b/LDP/guide/docbook/abs-guide/array-function.sh index 48d7e0d8..b35a6e86 100644 --- a/LDP/guide/docbook/abs-guide/array-function.sh +++ b/LDP/guide/docbook/abs-guide/array-function.sh @@ -73,3 +73,6 @@ echo ${arr[@]} # 11 12 13 14 15 16 17 18 19 20 echo exit 0 + +# Nathan Coulter points out that passing arrays with elements containing +#+ whitespace breaks this example. diff --git a/LDP/guide/docbook/abs-guide/cards.sh b/LDP/guide/docbook/abs-guide/cards.sh index 51f53c98..b854c0c0 100644 --- a/LDP/guide/docbook/abs-guide/cards.sh +++ b/LDP/guide/docbook/abs-guide/cards.sh @@ -76,9 +76,7 @@ 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? -} +} # Consider other methods of seeding the random number generator. deal_cards () { @@ -95,7 +93,7 @@ do parse_card $t u=$cards_picked+1 - # Change back to 1-based indexing (temporarily). Why? + # Change back to 1-based indexing, temporarily. Why? let "u %= $CARDS_IN_SUIT" if [ "$u" -eq 0 ] # Nested if/then condition test. then diff --git a/LDP/guide/docbook/abs-guide/csubloop.sh b/LDP/guide/docbook/abs-guide/csubloop.sh index 0b8b47c6..abadf762 100644 --- a/LDP/guide/docbook/abs-guide/csubloop.sh +++ b/LDP/guide/docbook/abs-guide/csubloop.sh @@ -19,6 +19,6 @@ done` echo "variable2 = $variable2" # variable2 = 0123456789 # Demonstrates that it's possible to embed a loop -#+ within a variable declaration. +#+ inside a variable declaration. exit 0 diff --git a/LDP/guide/docbook/abs-guide/cvt.sh b/LDP/guide/docbook/abs-guide/cvt.sh index ce4cedf1..0432647d 100644 --- a/LDP/guide/docbook/abs-guide/cvt.sh +++ b/LDP/guide/docbook/abs-guide/cvt.sh @@ -37,3 +37,23 @@ exit 0 # As it stands, this script converts *all* the files in the current #+ working directory. # Modify it to work *only* on files with a ".mac" suffix. + + + +# *** And here's another way to do it. *** # + +#!/bin/bash +# Batch convert into different graphic formats. +# Assumes imagemagick installed (standard in most Linux distros). + +INFMT=png # Can be tif, jpg, gif, etc. +OUTFMT=pdf # Can be tif, jpg, gif, pdf, etc. + +for pic in *"$INFMT" +do + p2=$(ls "$pic" | sed -e s/\.$INFMT//) + # echo $p2 + convert "$pic" $p2.$OUTFMT + done + +exit $? diff --git a/LDP/guide/docbook/abs-guide/dialog.sh b/LDP/guide/docbook/abs-guide/dialog.sh index a2eb59a8..8dedad52 100644 --- a/LDP/guide/docbook/abs-guide/dialog.sh +++ b/LDP/guide/docbook/abs-guide/dialog.sh @@ -12,7 +12,7 @@ # Input error in dialog box. -E_INPUT=65 +E_INPUT=85 # Dimensions of display, input widgets. HEIGHT=50 WIDTH=60 diff --git a/LDP/guide/docbook/abs-guide/ex16.sh b/LDP/guide/docbook/abs-guide/ex16.sh index 6184d739..db54e897 100644 --- a/LDP/guide/docbook/abs-guide/ex16.sh +++ b/LDP/guide/docbook/abs-guide/ex16.sh @@ -12,7 +12,7 @@ echo $a # 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." -# Inside a script, however, the history functions are disabled. +# Inside a script, however, the history functions are disabled by default. a=`ls -l` # Assigns result of 'ls -l' command to 'a' echo $a # Unquoted, however, it removes tabs and newlines. diff --git a/LDP/guide/docbook/abs-guide/find-splitpara.sh b/LDP/guide/docbook/abs-guide/find-splitpara.sh index 0e5fed5a..af5d7871 100644 --- a/LDP/guide/docbook/abs-guide/find-splitpara.sh +++ b/LDP/guide/docbook/abs-guide/find-splitpara.sh @@ -5,11 +5,13 @@ ARGCOUNT=1 # Expect one arg. -E_WRONGARGS=65 +OFF=0 # Flag states. +ON=1 +E_WRONGARGS=85 file="$1" # Target filename. lineno=1 # Line number. Start at 1. -Flag=0 # Blank line flag. +Flag=$OFF # Blank line flag. if [ $# -ne "$ARGCOUNT" ] then @@ -22,18 +24,18 @@ file_read () # Scan file for pattern, then print line. while read line do - if [[ "$line" =~ ^[a-z] && $Flag -eq 1 ]] - then # Line begins with lc character, following blank line. + if [[ "$line" =~ ^[a-z] && $Flag -eq $ON ]] + then # Line begins with lowercase character, following blank line. echo -n "$lineno:: " echo "$line" fi - if [[ "$line" =~ "^$" ]] - then # If blank line, - Flag=1 #+ set flag. + if [[ "$line" =~ ^$ ]] + then # If blank line, + Flag=$ON #+ set flag. else - Flag=0 + Flag=$OFF fi ((lineno++)) diff --git a/LDP/guide/docbook/abs-guide/generate-script.sh b/LDP/guide/docbook/abs-guide/generate-script.sh index fedc9f4b..a39c88f8 100644 --- a/LDP/guide/docbook/abs-guide/generate-script.sh +++ b/LDP/guide/docbook/abs-guide/generate-script.sh @@ -43,7 +43,7 @@ else echo "Problem in creating file: \"$OUTFILE\"" fi -# This method can also be used for generating +# This method also works for generating #+ C programs, Perl programs, Python programs, Makefiles, #+ and the like. diff --git a/LDP/guide/docbook/abs-guide/mailbox_grep.sh b/LDP/guide/docbook/abs-guide/mailbox_grep.sh index afcfb1f1..fd57948f 100644 --- a/LDP/guide/docbook/abs-guide/mailbox_grep.sh +++ b/LDP/guide/docbook/abs-guide/mailbox_grep.sh @@ -25,7 +25,7 @@ mbox_grep() # Parse mailbox file. # Otherwise "read" will strip leading & trailing space from its input. do - if [[ $mail =~ "^From " ]] # Match "From" field in message. + if [[ $mail =~ ^From ]] # Match "From" field in message. then (( body = 0 )) # "Zero out" variables. (( match = 0 )) diff --git a/LDP/guide/docbook/abs-guide/msquare.sh b/LDP/guide/docbook/abs-guide/msquare.sh index 8d848ab0..502d8f08 100644 --- a/LDP/guide/docbook/abs-guide/msquare.sh +++ b/LDP/guide/docbook/abs-guide/msquare.sh @@ -12,7 +12,7 @@ # of integers in which all the rows, columns, # and *long* diagonals add up to the same number. # Being "square," the array has the same number -# of rows and columns. +# of rows and columns. That number is the "order." # An example of a magic square of order 3 is: # 8 1 6 # 3 5 7 @@ -29,8 +29,8 @@ declare -i square usage_message () { - echo "Usage: $0 square-size" - echo " ... where \"square-size\" is an ODD integer" + echo "Usage: $0 order" + echo " ... where \"order\" (square size) is an ODD integer" echo " in the range 3 - 31." # Actually works for squares up to order 159, #+ but large squares will not display pretty-printed in a term window. @@ -57,7 +57,7 @@ calculate () # Here's where the actual work gets done. square[$index]=cell_val; ((cell_val++)) done done -} # Plain math, no visualization required. +} # Plain math, visualization not required. print_square () # Output square, one row at a time. @@ -72,7 +72,7 @@ print_square () # Output square, one row at a time. do let "idx = $row * $dimension + $col" printf "%3d " "${square[idx]}"; echo -n " " - done # Displays up to 13-order neatly in 80-column term window. + done # Displays up to 13th order neatly in 80-column term window. echo # Newline after each row. done diff --git a/LDP/guide/docbook/abs-guide/secret-pw.sh b/LDP/guide/docbook/abs-guide/secret-pw.sh index f434a35e..bcb97063 100644 --- a/LDP/guide/docbook/abs-guide/secret-pw.sh +++ b/LDP/guide/docbook/abs-guide/secret-pw.sh @@ -12,6 +12,9 @@ echo && echo # Two line-feeds in an "and list." stty -echo # Turns off screen echo. +# May also be done with +# read -sp passwd +# A big Thank You to Leigh James for pointing this out. echo -n "Enter password again " read passwd diff --git a/LDP/guide/docbook/abs-guide/seeding-random.sh b/LDP/guide/docbook/abs-guide/seeding-random.sh index 1976a3f4..d588f2bb 100644 --- a/LDP/guide/docbook/abs-guide/seeding-random.sh +++ b/LDP/guide/docbook/abs-guide/seeding-random.sh @@ -1,34 +1,43 @@ #!/bin/bash # seeding-random.sh: Seeding the RANDOM variable. +# v 1.1, reldate 09 Feb 2013 MAXCOUNT=25 # How many numbers to generate. +SEED= random_numbers () { -count=0 +local count=0 +local number + while [ "$count" -lt "$MAXCOUNT" ] do number=$RANDOM echo -n "$number " - let "count += 1" + let "count++" done } echo; echo -RANDOM=1 # Setting RANDOM seeds the random number generator. +SEED=1 +RANDOM=$SEED # Setting RANDOM seeds the random number generator. +echo "Random seed = $SEED" random_numbers -echo; echo "Trying again with same random seed ..." -RANDOM=1 # Same seed for RANDOM . . . +RANDOM=$SEED # Same seed for RANDOM . . . +echo; echo "Again, with same random seed ..." +echo "Random seed = $SEED" random_numbers # . . . reproduces the exact same number series. # # When is it useful to duplicate a "random" series? echo; echo -RANDOM=2 # Trying again, but with a different seed . . . +SEED=2 +RANDOM=$SEED # Trying again, but with a different seed . . . +echo "Random seed = $SEED" random_numbers # . . . gives a different number series. echo; echo @@ -37,12 +46,14 @@ echo; echo # It is also possible to seed RANDOM from 'time' or 'date' commands. # Getting fancy... -SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }') +SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }'| sed s/^0*//) # Pseudo-random output fetched #+ from /dev/urandom (system pseudo-random device-file), #+ then converted to line of printable (octal) numbers by "od", -#+ finally "awk" retrieves just one number for SEED. +#+ then "awk" retrieves just one number for SEED, +#+ finally "sed" removes any leading zeros. RANDOM=$SEED +echo "Random seed = $SEED" random_numbers echo; echo diff --git a/LDP/guide/docbook/abs-guide/tohtml.sh b/LDP/guide/docbook/abs-guide/tohtml.sh index 6318edd1..e6f45850 100644 --- a/LDP/guide/docbook/abs-guide/tohtml.sh +++ b/LDP/guide/docbook/abs-guide/tohtml.sh @@ -79,7 +79,7 @@ process_text () continue # Skip the underscore test. else # Otherwise . . . - if [[ "$line" =~ "\[*jpg\]" ]] # Is a graphic? + if [[ "$line" =~ \[*jpg\] ]] # Is a graphic? then # Strip away brackets. temp=$( echo "$line" | sed -e 's/\[//' -e 's/\]//' ) line=""$CENTER" <img src="\"$IMGDIR"/$temp\"> "$END_CENTER" " diff --git a/LDP/guide/docbook/abs-guide/vartrace.sh b/LDP/guide/docbook/abs-guide/vartrace.sh index 89144086..8e7fd013 100644 --- a/LDP/guide/docbook/abs-guide/vartrace.sh +++ b/LDP/guide/docbook/abs-guide/vartrace.sh @@ -10,7 +10,7 @@ echo " Just initialized \$variable to $variable in line number $line." let "variable *= 3"; line=$LINENO echo " Just multiplied \$variable by 3 in line number $line." -exit +exit 0 # The "trap 'command1 . . . command2 . . .' DEBUG" construct is #+ more appropriate in the context of a complex script, diff --git a/LDP/guide/docbook/abs-guide/weirdvars.sh b/LDP/guide/docbook/abs-guide/weirdvars.sh index ad01de99..1d4120d5 100644 --- a/LDP/guide/docbook/abs-guide/weirdvars.sh +++ b/LDP/guide/docbook/abs-guide/weirdvars.sh @@ -42,5 +42,9 @@ echo "\$var1 = "$var1"" # $var1 = Two bits # Or, as Chris Hiestand points out ... -if [[ "$(du "$My_File1")" -gt "$(du "$My_File2")" ]]; then ... +if [[ "$(du "$My_File1")" -gt "$(du "$My_File2")" ]] +# ^ ^ ^ ^ ^ ^ ^ ^ +then + ... +fi # ************************************************************ # diff --git a/LDP/guide/docbook/abs-guide/whx.sh b/LDP/guide/docbook/abs-guide/whx.sh index 75b2133d..28eb70bb 100644 --- a/LDP/guide/docbook/abs-guide/whx.sh +++ b/LDP/guide/docbook/abs-guide/whx.sh @@ -27,7 +27,7 @@ then fi -if [[ "$1" =~ "[a-zA-Z][a-zA-Z]$" ]] # Ends in two alpha chars? +if [[ "$1" =~ [a-zA-Z][a-zA-Z]$ ]] # Ends in two alpha chars? then # It's a domain name && #+ must do host lookup. IPADDR=$(host -W $HOSTWAIT $1 | awk '{print $4}') @@ -57,21 +57,21 @@ then exit $E_NOHOST # Bail out. fi -if [[ "$IPADDR" =~ "^[;;]" ]] +if [[ "$IPADDR" =~ ^[;;] ]] # ;; Connection timed out; no servers could be reached. then echo "Host lookup timed out!" exit $E_TIMEOUT # Bail out. fi -if [[ "$IPADDR" =~ "[(NXDOMAIN)]$" ]] +if [[ "$IPADDR" =~ [(NXDOMAIN)]$ ]] # Host xxxxxxxxx.xxx not found: 3(NXDOMAIN) then echo "Host not found!" exit $E_NOHOST # Bail out. fi -if [[ "$IPADDR" =~ "[(SERVFAIL)]$" ]] +if [[ "$IPADDR" =~ [(SERVFAIL)]$ ]] # Host xxxxxxxxx.xxx not found: 2(SERVFAIL) then echo "Host not found!" diff --git a/LDP/guide/docbook/abs-guide/words.data b/LDP/guide/docbook/abs-guide/words.data index e9e3ddc3..67c25b8b 100644 --- a/LDP/guide/docbook/abs-guide/words.data +++ b/LDP/guide/docbook/abs-guide/words.data @@ -1,20 +1,25 @@ -making -nogood syzygy +nogood +filesystem alltold orangutan -doing +nekulturno madeupword -gargantuan +tasteless coredump -nicrosoft +hazmat ziggurat -what +haystack abracadbra advanced bash scripting guide +lowercase +bremsstrahlung abcdef +duplicity +kitsch +avoirdupois radicand end