diff --git a/LDP/guide/docbook/abs-guide/Change.log b/LDP/guide/docbook/abs-guide/Change.log index da5ad874..6275fa5b 100644 --- a/LDP/guide/docbook/abs-guide/Change.log +++ b/LDP/guide/docbook/abs-guide/Change.log @@ -6,6 +6,79 @@ http://personal.riverusers.com/~thegrendel/Change.log ------------------------------------------------------------------------ +Version 3.8 +Blaeberry release, 02/26/06 + +1) In "Special Characters" chapter: + Minor additions to leadin to "control characters" entries, + and to "Ctl-K" and "Ctl-L" entries. + +2) In "Introduction to Variables and Parameters" chapter: + Rewrote lead-in. + In "Variable Substitution" section: + Added inline example of difference between variable name and value. + Added escaped whitespace example to "ex9.sh" script. + +3) In "Basic Commands" section of "External Commands" Chapter: + At "cp" entry, added example of using -u option. + +4) At beginning "Regular Expressions" chapter, + added Stowe Boyd epigraph. + +5) "System and Administrative Commands" chapter: + In "Job Control" subsection, + Added "pgrep/pkill" entry. + At "ps" entry, mentioned "aux" options. + In "Filesystem" subsection, + At "lockfile" entry, more material, including usage example and + footnote. + Added "flock" entry. + +6) In "File and Archiving Commands" section of "External Commands" Chapter: + At "sha1sum" entry, added paragraph about security consultants' + misgivings. + More info at "cpio" entry. + +7) In "Parameter Substitution" section of "Variables Revisited" chapter: + Fixed minor typo at "${parameter:+alt_value}" example. + (Thank you, Jemshad O K) + +8) Partitioned "Security Section" of the "Miscellany" chapter + into two subsections. + Added subsection about "shc" utility for compiling script source. + +9) In "Loops" section of "Loops and Branches" chapter: + Applied fixup to "bin-grep.sh" example. + (Thank you, Anton Filippov.) + +10) In "Redirecting Code Blocks" section of "I/O Redirection" chapter: + Corrected annotation in final section of "redir2.sh" example. + (Thank you, Brian Onn.) + +11) In "Internal Commands and Builtins" chapter: + Added "revposparams.sh" example. + (Thank you, Dan Jacobson.) + More info at "nmap" entry. + +12) In "Text Processing" section of "External Commands" Chapter: + At "egrep" / "fgrep" entry, added note about boolean "|" operator. + +13) In "Here Strings" section of "Here Documents" chapter: + Added short inline usage example. + Added "mailbox_grep.sh" example. + (Thank you, Francisco Lobo, for both of the above.) + +14) Added "Bash Command-Line Options" section to (retitled) "Command + Line Options" appendix. + +15) In "Copyright" chapter: + Added anti-DRM provisions. + +16) Various minor fixups on example scripts. + + + + Version 3.7 Whortleberry release, 10/23/05 diff --git a/LDP/guide/docbook/abs-guide/abs-guide.sgml b/LDP/guide/docbook/abs-guide/abs-guide.sgml index a4a77fd5..b6f05afa 100644 --- a/LDP/guide/docbook/abs-guide/abs-guide.sgml +++ b/LDP/guide/docbook/abs-guide/abs-guide.sgml @@ -247,6 +247,7 @@ Uncomment line below to generate index. + @@ -327,6 +328,7 @@ Uncomment line below to generate index. + @@ -348,19 +350,12 @@ Uncomment line below to generate index. - 3.7 - 23 October 2005 + 3.8 + 26 February 2006 - - 3.5 - 04 June 2005 - mc - 'BOXBERRY' release: Important Update. - - 3.6 28 Aug 2005 @@ -375,6 +370,13 @@ Uncomment line below to generate index. 'WHORTLEBERRY' release: Bugfix Update. + + 3.8 + 26 Feb 2006 + mc + 'BLAEBERRY' release: Minor Update. + + @@ -395,7 +397,7 @@ Uncomment line below to generate index. introduction to programming concepts. + url="http://personal.riverusers.com/~thegrendel/abs-guide-3.8.tar.bz2"> The latest update of this document, as an archived, bzip2-ed tarball including both the SGML source and rendered HTML, may @@ -897,7 +899,6 @@ fi Basics - Special Characters @@ -2695,8 +2696,15 @@ echo $a # 28 change the behavior of the terminal or text display. + A control character is a CONTROL - + key combination. + + key combination (pressed + simultaneously). + + A control character may also + be written in octal or + hexadecimal notation, + following an escape. Control characters are not normally useful inside a script. @@ -2763,7 +2771,8 @@ echo; echo Ctl-J - Newline (line feed). + Newline (line feed). In a script, may also be expressed + in octal notation -- '\012' or in hexadecimal -- '\x0a'. @@ -2772,14 +2781,18 @@ echo; echo When typing text on the console or in an xterm window, Ctl-K erases from the character - under the cursor to end of line. + under the cursor to end of line. Within a script, + Ctl-K may behave differently, + as in Lee Lee Maschmeyer's example, below. Ctl-L - Formfeed (clear the terminal screen). This has - the same effect as the clear command. + Formfeed (clear the terminal screen). In a terminal, + this has the same effect as the clear command. When sent + to a printer, a Ctl-L causes + an advance to end of the paper sheet. @@ -2795,7 +2808,8 @@ echo >&2 # The '-s' makes anything typed silent, #+ so it is necessary to go to new line explicitly. read -n 1 -s -p $'Control-J leaves cursor on next line. \x0a' -echo >&2 # Control-J is linefeed. + # '0a' is the hex equivalent of Control-J, linefeed. +echo >&2 ### @@ -2925,12 +2939,14 @@ echo <Ctl-V><Ctl-J> Introduction to Variables and Parameters Variables are how programming and - scripting languages represent data. They appear in arithmetic - operations and manipulation of quantities, in string parsing, and - they are indispensable for working in the abstract with symbols -- - tokens that represent something else. A variable is nothing more - than a label assigned to a location or set - of locations in computer memory holding an item of data. + scripting languages represent data. A variable is nothing + more than a label, a name assigned to a + location or set of locations in computer memory holding an item + of data. + + Variables appear in arithmetic operations and manipulation of + quantities, and in string parsing. + Variable Substitution @@ -2959,10 +2975,23 @@ echo <Ctl-V><Ctl-J> variable1 is the name of a variable, then $variable1 is a reference to its value, - the data item it contains. The only time a - variable appears naked -- without - the $ prefix -- is when declared - or assigned, when unset, + the data item it contains. + + + bash$ variable=23 + + +bash$ echo variable +variable + +bash$ echo $variable +23 + + + + The only time a variable appears naked + -- without the $ prefix -- is when + declared or assigned, when unset, when exported, or in the special case of a variable representing a signal (see @@ -3012,8 +3041,8 @@ let "uninitialized += 5" # Add 5 to it. echo "$uninitialized" # 5 # Conclusion: -# An uninitialized variable has no value, however -#+ it acts as if it were 0 in an arithmetic operation. +# An uninitialized variable has no value, +#+ however it acts as if it were 0 in an arithmetic operation. # This is undocumented (and probably non-portable) behavior. See also . @@ -6039,10 +6068,13 @@ echo "FUNCNAME = $FUNCNAME" # FUNCNAME = bash$ echo $IFS | cat -vte $ +(Show tabs and display "$" at end-of-line.) + bash$ bash -c 'set w x y z; IFS=":-;"; echo "$*"' w:x:y:z +(Read commands from string and assign any arguments to pos params.) @@ -6058,8 +6090,10 @@ echo "FUNCNAME = $FUNCNAME" # FUNCNAME = (Thanks, S. C., for clarification and examples.) - See also for an instructive - example of using $IFS. + See also , , and + for instructive examples of using + $IFS. @@ -7804,7 +7838,7 @@ echo "a = $a" # a = # Different result from a=${param5+xyz} param6=123 -a=${param6+xyz} +a=${param6:+xyz} echo "a = $a" # a = xyz @@ -8653,9 +8687,10 @@ echo $? # 1 Omitting the in [list] part of a for loop causes the loop to operate - on $@ -- the list of arguments given - on the command line to the script. A particularly clever - illustration of this is . + on $@ -- the + positional parameters. A particularly clever + illustration of this is . See also . Missing <userinput>in [list]</userinput> in a @@ -9920,6 +9955,13 @@ while read f; do <programlisting>&ex34;</programlisting> </example> + <para>More fun with positional parameters.</para> + + <example id="revposparams"> + <title>Reversing the positional parameters + &revposparams; + + Invoking set without any options or arguments simply lists all the environmental and other variables @@ -11218,16 +11260,25 @@ tr a-z A-Z < filename # Same effect, but starts one less process, cp + + + This is the file copy command. cp file1 file2 copies file1 to file2, overwriting file2 if it already exists (see ). - Particularly useful are the - archive flag (for copying an entire directory tree) + + Particularly useful are the + archive flag (for copying an entire directory tree), + the update flag, and the and - recursive flags. + recursive flags. + cp -u source_dir/* dest_dir +# "Synchronize" dest_dir to source_dir +#+ by copying over all newer and not previously existing files. + @@ -12848,7 +12899,14 @@ Here is some text. as grep -E. This uses a somewhat different, extended set of Regular Expressions, which can make the search a bit more - flexible. + flexible. It also allows the boolean | + (or) operator. + bash $ egrep 'matches|Matches' file.txt +Line 1 matches. + Line 3 Matches. + Line 4 contains matches, but also Matches + + fgrep - fast grep - is the same as grep -F. It does @@ -13771,7 +13829,9 @@ function write_utf8_string { input and output) is rarely seen any more, having been supplanted by tar/gzip. It still - has its uses, such as moving a directory tree. + has its uses, such as moving a directory tree. With an + appropriate block size (for copying) specified, it + can be appreciably faster than tar. Using <command>cpio</command> to move a directory tree @@ -14637,11 +14697,18 @@ gzip -cd patchXX.gz | patch -p0 linkend="horserace"> for creative uses of the md5sum command. - + + + There have been reports that the 128-bit md5sum can be cracked, so the more secure 160-bit sha1sum is a welcome new addition to the checksum toolkit. + + + Some security consultants think that even + sha1sum can be compromised. So, what's + next -- a 512-bit checksum utility? bash$ md5sum testfile e181e2c8720c60522c4c4c981108e367 testfile @@ -14649,7 +14716,7 @@ gzip -cd patchXX.gz | patch -p0 bash$ sha1sum testfile 5d7425a9c08a66c3177f1e31286fa40986ffc996 testfile - + @@ -17723,9 +17790,11 @@ setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ< port scan - Network port scanner. This command scans a server to + Network mapper + and port scanner. This command scans a server to locate open ports and the services associated with those - ports. It is an important security tool for locking down + ports. It can also report information about packet filters and + firewalls. This is an important security tool for locking down a network against hacking attempts. #!/bin/bash @@ -18314,7 +18383,8 @@ Average: all 6.33 1.70 14.71 0.00 77.26Process Statistics: lists currently executing processes by owner and PID (process ID). This - is usually invoked with options, + is usually invoked with or + options, and may be piped to grep or sed to search for a specific process (see and + + + pgrep + pkill + + pgrep + + + command + process grep + + + pkill + + + command + process kill + + + + Combining the ps command + with grep or + kill. + + + bash$ ps a | grep mingetty +2212 tty2 Ss+ 0:00 /sbin/mingetty tty2 + 2213 tty3 Ss+ 0:00 /sbin/mingetty tty3 + 2214 tty4 Ss+ 0:00 /sbin/mingetty tty4 + 2215 tty5 Ss+ 0:00 /sbin/mingetty tty5 + 2216 tty6 Ss+ 0:00 /sbin/mingetty tty6 + 4849 pts/2 S+ 0:00 grep mingetty + + +bash$ pgrep mingetty +2212 mingetty + 2213 mingetty + 2214 mingetty + 2215 mingetty + 2216 mingetty + + + + + + + pstree @@ -19351,6 +19468,9 @@ mount -o loop /dev/loop0 /mnt # Mount it. in use by a particular process (busy), and this permits only restricted access (or no access) to other processes. + + lockfile /home/bozo/lockfiles/$0.lock +# Creates a write-protected lockfile prefixed with the name of the script. Lock files are used in such applications as protecting system mail folders from simultaneously being changed @@ -19364,7 +19484,12 @@ mount -o loop /dev/loop0 /mnt # Mount it. Normally, applications create and check for lock files in the /var/lock - directory. A script can test for the presence of a lock file by + directory. + Since only root + has write permission in the /var/lock directory, + a user script cannot set a lock file there. + A script can test for the presence of a lock file by something like the following. appname=xyzip # Application "xyzip" created lock file "/var/lock/xyzip.lock". @@ -19376,6 +19501,35 @@ then + + flock + + flock + + + command + lock file + + + + Much less useful than the lockfile + command is flock. It sets an + advisory lock on a file and then executes + a command while the lock is on. This is to prevent + any other process from setting a lock on that file until + completion of the specified command. + + flock $0 cat $0 > lockfile__$0 +# Set a lock on the script the above line appears in, +#+ while listing the script to stdout. + + Unlike lockfile, + flock does not + automatically create a lock file. + + + + mknod @@ -21044,11 +21198,35 @@ echo "This line had better not echo." # Follows an 'exit' command.stdin of COMMAND. + + String="This is a string of words." + +read -r -a Words <<< "$String" +# The -a option to "read" +#+ assigns the resulting values to successive members of an array. + +echo "First word in String is: ${Words[0]}" # This +echo "Second word in String is: ${Words[1]}" # is +echo "Third word in String is: ${Words[2]}" # a +echo "Fourth word in String is: ${Words[3]}" # string +echo "Fifth word in String is: ${Words[4]}" # of +echo "Sixth word in String is: ${Words[5]}" # words. +echo "Seventh word in String is: ${Words[6]}" # (null) + # Past end of $String. + +# Thank you, Francisco Lobo, for the suggestion. + + Prepending a line to a file &prependex; + + Parsing a mailbox + &mailboxgrep; + + Exercise: Find other uses for here strings. @@ -21114,6 +21292,12 @@ echo "This line had better not echo." # Follows an 'exit' command. Regular Expressions + + Stowe Boyd + . . . the intellectual activity associated with software + development is largely one of gaining insight. + + To fully utilize the power of shell scripting, you need to @@ -22072,6 +22256,8 @@ fi Restricted Shells + + <anchor id="disabledcommref">Disabled commands in restricted shells @@ -26058,6 +26244,9 @@ fi Security Issues + + Infected Shell Scripts + A brief warning about script security is appropriate. A shell script may contain a worm, trojan, or even a virus. @@ -26083,6 +26272,27 @@ fi look at and understand scripts may protect your system from being hacked or damaged. + + + + Hiding Shell Script Source + + For security purposes, it may be necessary to render a script + unreadable. If only there were a utility to create a stripped + binary executable from a script. Francisco Rosales' shc - + generic shell script compiler does exactly that. + + Unfortunately, according to an article in + the October, 2005 Linux Journal, + the binary can, in at least some cases, be decrypted to recover + the original script source. Still, this could be a useful + method of keeping scripts secure from all but the most skilled + hackers. + + + @@ -26331,8 +26541,9 @@ else # Or, ask for corrected input. fi - For another example of using the =~ - operator, see . + For additional examples of using the + =~ operator, see + and . @@ -26628,29 +26839,29 @@ fi errors and other corrections. Special thanks! 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, bojster, nyal, - Hobbit, Ender, Little - Monster (Alexis), Mark, 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, 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, Stefano Falsetto, Chris Morgan, - Walter Dnes, Linc Fessenden, Michael Iatrou, Pharis Monalo, - Jesse Gough, Fabian Kreutz, Mark Norman, Harald Koenig, Peter - Knowles, Mariusz Gniazdowski, Tedman Eng, and David Lawyer - (himself an author of four HOWTOs). + 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, bojster, + nyal, Hobbit, Ender, + Little Monster (Alexis), Mark, + 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, 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, Stefano Falsetto, + Chris Morgan, Walter Dnes, Linc Fessenden, Michael Iatrou, Pharis + Monalo, Jesse Gough, Fabian Kreutz, Mark Norman, Harald Koenig, + Peter Knowles, Francisco Lobo, Mariusz Gniazdowski, Tedman Eng, + and David Lawyer (himself an author of four HOWTOs). My gratitude to Chet Ramey and Brian Fox for writing Bash, @@ -29022,7 +29233,17 @@ exit 0 - + + Command-Line Options + + Many executables, whether binaries or script files, accept + options to modify their run-time behavior. For example: from + the command line, typing command -o + would invoke command, with option + . + + + Standard Command-Line Options Over time, there has evolved a loose standard for the @@ -29142,8 +29363,64 @@ exit 0 is available at http://www.gnu.org/prep/standards_19.html. + + + + + + + Bash Command-Line Options + + Bash itself has a number of command-line + options. Here are some of the more useful ones. + + + + + + Read commands from the following string and assign any + arguments to the positional + parameters. + + +bash$ bash -c 'set a b c d; IFS="+-;"; echo "$*"' +a+b+c+d + + + + + + + + Runs the shell, or a script, in restricted mode. + + + + + Forces Bash to conform to POSIX mode. + + + + + Display Bash version information and + exit. + + + + + End of options. Anything further on the command + line is an argument, not an option. + + + + + + + - + @@ -31262,6 +31539,11 @@ fairly detailed rundown on the Playfair Cipher and its solution methods.23 Oct 2005 WHORTLEBERRY release: Bugfixes, some materials added. + + + 26 Feb 2006 + BLAEBERRY release: Bugfixes, some materials added. + @@ -31352,9 +31634,14 @@ fairly detailed rundown on the Playfair Cipher and its solution methods. + Provision A, above, explicitly prohibits relabeling this document. An example of diff --git a/LDP/guide/docbook/abs-guide/bashpodder.sh b/LDP/guide/docbook/abs-guide/bashpodder.sh index 1e1ef274..a7378915 100644 --- a/LDP/guide/docbook/abs-guide/bashpodder.sh +++ b/LDP/guide/docbook/abs-guide/bashpodder.sh @@ -67,3 +67,11 @@ ls $datadir | grep -v m3u > $datadir/podcast.m3u exit 0 + +################################################# +For a different scripting approach to Podcasting, +see Phil Salkie's article, +"Internet Radio to Podcast with Shell Tools" +in the September, 2005 issue of LINUX JOURNAL, +http://www.linuxjournal.com/article/8171 +################################################# diff --git a/LDP/guide/docbook/abs-guide/bin-grep.sh b/LDP/guide/docbook/abs-guide/bin-grep.sh index 2f4008f9..9e84e86a 100644 --- a/LDP/guide/docbook/abs-guide/bin-grep.sh +++ b/LDP/guide/docbook/abs-guide/bin-grep.sh @@ -20,7 +20,8 @@ then fi -IFS="\n" # Per suggestion of Paulo Marcel Coelho Aragao. +IFS=$'\012' # Per suggestion of Anton Filippov. + # was: IFS="\n" for word in $( strings "$2" | grep "$1" ) # The "strings" command lists strings in binary files. # Output then piped to "grep", which tests for desired string. @@ -28,7 +29,7 @@ do echo $word done -# As S.C. points out, lines 23 - 29 could be replaced with the simpler +# As S.C. points out, lines 23 - 30 could be replaced with the simpler # strings "$2" | grep "$1" | tr -s "$IFS" '[\n*]' diff --git a/LDP/guide/docbook/abs-guide/ex48.sh b/LDP/guide/docbook/abs-guide/ex48.sh index b24148a8..6edd4055 100644 --- a/LDP/guide/docbook/abs-guide/ex48.sh +++ b/LDP/guide/docbook/abs-guide/ex48.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copying a directory tree using 'cpio.' +# Copying a directory tree using cpio. # Advantages of using 'cpio': # Speed of copying. It's faster than 'tar' with pipes. @@ -19,9 +19,10 @@ fi source=$1 destination=$2 + find "$source" -depth | cpio -admvp "$destination" # ^^^^^ ^^^^^ -# Read the 'find' and 'cpio' man page to decipher these options. +# Read the 'find' and 'cpio' man pages to decipher these options. # Exercise: diff --git a/LDP/guide/docbook/abs-guide/ex9.sh b/LDP/guide/docbook/abs-guide/ex9.sh index 134d41fa..4a70eb27 100644 --- a/LDP/guide/docbook/abs-guide/ex9.sh +++ b/LDP/guide/docbook/abs-guide/ex9.sh @@ -9,13 +9,13 @@ 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". +# "VARIABLE =value" +# ^ +#% Script tries to run "VARIABLE" command with one argument, "=value". -# If "VARIABLE= value", -# ^ -#+ script tries to run "value" command with +# "VARIABLE= value" +# ^ +#% Script tries to run "value" command with #+ the environmental variable "VARIABLE" set to "". #------------------------------------------------------------------------- @@ -34,8 +34,9 @@ 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 @@ -62,7 +63,7 @@ var1=21 var2=22 var3=$V3 echo echo "var1=$var1 var2=$var2 var3=$var3" -# May cause problems with older versions of "sh". +# May cause problems with older versions of "sh" . . . # -------------------------------------------------------------- @@ -74,9 +75,16 @@ other_numbers="1 2 3" # ^ ^ # If there is whitespace embedded within a variable, #+ then quotes are necessary. +# other_numbers=1 2 3 # Gives an error message. echo "numbers = $numbers" echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 -echo +# Escaping the whitespace also works. +mixed_bag=2\ ---\ Whatever +# ^ ^ Space after escape (\). + +echo "$mixed_bag" # 2 --- Whatever + +echo; echo echo "uninitialized_variable = $uninitialized_variable" # Uninitialized variable has null value (no value at all). diff --git a/LDP/guide/docbook/abs-guide/mailbox_grep.sh b/LDP/guide/docbook/abs-guide/mailbox_grep.sh new file mode 100644 index 00000000..3b052748 --- /dev/null +++ b/LDP/guide/docbook/abs-guide/mailbox_grep.sh @@ -0,0 +1,84 @@ +#!/bin/bash +# Script by Francisco Lobo, +#+ and slightly modified and commented by ABS Guide author. +# Used in ABS Guide with permission. (Thank you!) + +# This script will not run under Bash version < 3.0. + + +E_MISSING_ARG=67 +if [ -z "$1" ] +then + echo "Usage: $0 mailbox-file" + exit $E_MISSING_ARG +fi + +mbox_grep() # Parse mailbox file. +{ + declare -i body=0 match=0 + declare -a date sender + declare mail header value + + + while IFS= read -r mail +# ^^^^ Reset $IFS. +# Otherwise "read" will strip leading & trailing space from its input. + + do + if [[ $mail =~ "^From " ]] # Match "From" field in message. + then + (( body = 0 )) # "Zero out" variables. + (( match = 0 )) + unset date + + elif (( body )) + then + (( match )) + # echo "$mail" + # Uncomment above line if you want entire body of message to display. + + elif [[ $mail ]]; then + IFS=: read -r header value <<< "$mail" + # ^^^ "here string" + + case "$header" in + [Ff][Rr][Oo][Mm] ) [[ $value =~ "$2" ]] && (( match++ )) ;; + # Match "From" line. + [Dd][Aa][Tt][Ee] ) read -r -a date <<< "$value" ;; + # ^^^ + # Match "Date" line. + [Rr][Ee][Cc][Ee][Ii][Vv][Ee][Dd] ) read -r -a sender <<< "$value" ;; + # ^^^ + # Match IP Address (may be spoofed). + esac + + else + (( body++ )) + (( match )) && + echo "MESSAGE ${date:+of: ${date[*]} }" + # Entire $date array ^ + echo "IP address of sender: ${sender[1]}" + # Second field of "Received" line ^ + + fi + + + done < "$1" # Redirect stdout of file into loop. +} + + +mbox_grep "$1" # Send mailbox file to function. + +exit $? + +# Exercises: +# --------- +# 1) Break the single function, above, into multiple functions, +#+ for the sake of readability. +# 2) Add additional parsing to the script, checking for various keywords. + + + +$ mailbox_grep.sh scam_mail +--> MESSAGE of Thu, 5 Jan 2006 08:00:56 -0500 (EST) +--> IP address of sender: 196.3.62.4 diff --git a/LDP/guide/docbook/abs-guide/max.sh b/LDP/guide/docbook/abs-guide/max.sh index 6a0f1940..7a2ada13 100644 --- a/LDP/guide/docbook/abs-guide/max.sh +++ b/LDP/guide/docbook/abs-guide/max.sh @@ -3,6 +3,8 @@ E_PARAM_ERR=-198 # If less than 2 params passed to function. EQUAL=-199 # Return value if both params equal. +# Error values out of range of any +#+ params that might be fed to the function. max2 () # Returns larger of two numbers. { # Note: numbers compared must be less than 257. diff --git a/LDP/guide/docbook/abs-guide/max2.sh b/LDP/guide/docbook/abs-guide/max2.sh index 2531ce3e..dcaf0259 100644 --- a/LDP/guide/docbook/abs-guide/max2.sh +++ b/LDP/guide/docbook/abs-guide/max2.sh @@ -6,6 +6,7 @@ EQUAL=0 # Return value if both params equal. E_PARAM_ERR=-99999 # Not enough params passed to function. +# ^^^^^^ Out of range of any params that might be passed. max2 () # "Returns" larger of two numbers. { @@ -29,7 +30,7 @@ else fi echo $retval # Echoes (to stdout), rather than returning value. - + # Why? } diff --git a/LDP/guide/docbook/abs-guide/redir2.sh b/LDP/guide/docbook/abs-guide/redir2.sh index 53ee461c..8ff3f312 100644 --- a/LDP/guide/docbook/abs-guide/redir2.sh +++ b/LDP/guide/docbook/abs-guide/redir2.sh @@ -33,8 +33,9 @@ exit 0 #+ so that this script, for example, runs correctly. # (Thanks to Heiner Steven for pointing this out.) -# However . . . -# Bash *can* sometimes start a subshell in a *redirected* "while" loop. +# However . . . +# Bash *can* sometimes start a subshell in a PIPED "while-read" loop, +#+ as distinct from a REDIRECTED "while" loop. abc=hi echo -e "1\n2\n3" | while read l @@ -43,5 +44,6 @@ echo -e "1\n2\n3" | while read l done echo $abc -# (Thanks, Bruno de Oliveira Schneider, for demonstrating this -#+ with the above snippet of code.) +# Thanks, Bruno de Oliveira Schneider, for demonstrating this +#+ with the above snippet of code. +# And, thanks, Brian Onn, for correcting an annotation error. diff --git a/LDP/guide/docbook/abs-guide/revposparams.sh b/LDP/guide/docbook/abs-guide/revposparams.sh new file mode 100644 index 00000000..3219714a --- /dev/null +++ b/LDP/guide/docbook/abs-guide/revposparams.sh @@ -0,0 +1,65 @@ +#!/bin/bash +# revposparams.sh: Reverse positional parameters. +# Script by Dan Jacobson, with stylistic revisions by document author. + + +set a\ b c d\ e; +# ^ ^ Spaces escaped +# ^ ^ Spaces not escaped +OIFS=$IFS; IFS=:; +# ^ Saving old IFS and setting new one. + +echo + +until [ $# -eq 0 ] +do # Step through positional parameters. + echo "### k0 = "$k"" # Before + k=$1:$k; # Append each pos param to loop variable. +# ^ + echo "### k = "$k"" # After + echo + shift; +done + +set $k # Set new positional parameters. +echo - +echo $# # Count of positional parameters. +echo - +echo + +for i # Omitting the "in list" sets the variable -- i -- + #+ to the positional parameters. +do + echo $i # Display new positional parameters. +done + +IFS=$OIFS # Restore IFS. + +# Question: +# Is it necessary to set an new IFS, internal field separator, +#+ in order for this script to work properly? +# What happens if you don't? Try it. +# And, why use the new IFS -- a colon -- in line 17, +#+ to append to the loop variable? +# What is the purpose of this? + +exit 0 + +$ ./revposparams.sh + +### k0 = +### k = a b + +### k0 = a b +### k = c a b + +### k0 = c a b +### k = d e c a b + +- +3 +- + +d e +c +a b