This commit is contained in:
gferg 2006-03-06 14:21:00 +00:00
parent e614d9d757
commit 53465e6309
11 changed files with 625 additions and 93 deletions

View File

@ -6,6 +6,79 @@
http://personal.riverusers.com/~thegrendel/Change.log 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 Version 3.7
Whortleberry release, 10/23/05 Whortleberry release, 10/23/05

View File

@ -247,6 +247,7 @@ Uncomment line below to generate index.
<!ENTITY fileintegrity SYSTEM "file-integrity.sh"> <!ENTITY fileintegrity SYSTEM "file-integrity.sh">
<!ENTITY readnovar SYSTEM "read-novar.sh"> <!ENTITY readnovar SYSTEM "read-novar.sh">
<!ENTITY setpos SYSTEM "set-pos.sh"> <!ENTITY setpos SYSTEM "set-pos.sh">
<!ENTITY revposparams SYSTEM "revposparams.sh">
<!ENTITY badread SYSTEM "badread.sh"> <!ENTITY badread SYSTEM "badread.sh">
<!ENTITY selfexec SYSTEM "self-exec.sh"> <!ENTITY selfexec SYSTEM "self-exec.sh">
<!ENTITY selfdestruct SYSTEM "self-destruct.sh"> <!ENTITY selfdestruct SYSTEM "self-destruct.sh">
@ -327,6 +328,7 @@ Uncomment line below to generate index.
<!ENTITY prasc SYSTEM "pr-asc.sh"> <!ENTITY prasc SYSTEM "pr-asc.sh">
<!ENTITY whx SYSTEM "whx.sh"> <!ENTITY whx SYSTEM "whx.sh">
<!ENTITY spamlookup SYSTEM "spam-lookup.sh"> <!ENTITY spamlookup SYSTEM "spam-lookup.sh">
<!ENTITY mailboxgrep SYSTEM "mailbox_grep.sh">
<!ENTITY fc4upd SYSTEM "fc4upd.sh"> <!ENTITY fc4upd SYSTEM "fc4upd.sh">
<!ENTITY gen0data SYSTEM "gen0"> <!ENTITY gen0data SYSTEM "gen0">
<!ENTITY cdll SYSTEM "cdll"> <!ENTITY cdll SYSTEM "cdll">
@ -348,19 +350,12 @@ Uncomment line below to generate index.
</affiliation> </affiliation>
</author> </author>
<releaseinfo>3.7</releaseinfo> <releaseinfo>3.8</releaseinfo>
<pubdate>23 October 2005</pubdate> <pubdate>26 February 2006</pubdate>
<revhistory> <revhistory>
<revision>
<revnumber>3.5</revnumber>
<date>04 June 2005</date>
<authorinitials>mc</authorinitials>
<revremark>'BOXBERRY' release: Important Update.</revremark>
</revision>
<revision> <revision>
<revnumber>3.6</revnumber> <revnumber>3.6</revnumber>
<date>28 Aug 2005</date> <date>28 Aug 2005</date>
@ -375,6 +370,13 @@ Uncomment line below to generate index.
<revremark>'WHORTLEBERRY' release: Bugfix Update.</revremark> <revremark>'WHORTLEBERRY' release: Bugfix Update.</revremark>
</revision> </revision>
<revision>
<revnumber>3.8</revnumber>
<date>26 Feb 2006</date>
<authorinitials>mc</authorinitials>
<revremark>'BLAEBERRY' release: Minor Update.</revremark>
</revision>
</revhistory> </revhistory>
@ -395,7 +397,7 @@ Uncomment line below to generate index.
introduction to programming concepts.</para> introduction to programming concepts.</para>
<para><ulink <para><ulink
url="http://personal.riverusers.com/~thegrendel/abs-guide-3.7.tar.bz2"> url="http://personal.riverusers.com/~thegrendel/abs-guide-3.8.tar.bz2">
The latest update of this document</ulink>, as an archived, <link The latest update of this document</ulink>, as an archived, <link
linkend="bzipref">bzip2-ed</link> <quote>tarball</quote> linkend="bzipref">bzip2-ed</link> <quote>tarball</quote>
including both the SGML source and rendered HTML, may including both the SGML source and rendered HTML, may
@ -897,7 +899,6 @@ fi</programlisting>
<title>Basics</title> <title>Basics</title>
<chapter id="special-chars"> <chapter id="special-chars">
<title>Special Characters</title> <title>Special Characters</title>
@ -2695,8 +2696,15 @@ echo $a # 28</programlisting></para>
<para><anchor id="controlcharref"></para> <para><anchor id="controlcharref"></para>
<formalpara><title> change the behavior of the <formalpara><title> change the behavior of the
terminal or text display.</title> terminal or text display.</title>
<para>A control character is a <keycap>CONTROL</keycap> <para>A control character is a <keycap>CONTROL</keycap>
+ <keycap>key</keycap> combination.</para> + <keycap>key</keycap> combination (pressed
simultaneously).
A control character may also
be written in <firstterm>octal</firstterm> or
<firstterm>hexadecimal</firstterm> notation,
following an <firstterm>escape</firstterm>.</para>
</formalpara> </formalpara>
<para>Control characters are not normally useful inside a <para>Control characters are not normally useful inside a
script.</para> script.</para>
@ -2763,7 +2771,8 @@ echo; echo</programlisting>
<listitem> <listitem>
<para><userinput>Ctl-J</userinput></para> <para><userinput>Ctl-J</userinput></para>
<para>Newline (line feed).</para> <para>Newline (line feed). In a script, may also be expressed
in octal notation -- '\012' or in hexadecimal -- '\x0a'.</para>
</listitem> </listitem>
<listitem> <listitem>
@ -2772,14 +2781,18 @@ echo; echo</programlisting>
<para>When typing text on the console or in an <para>When typing text on the console or in an
<emphasis>xterm</emphasis> window, <emphasis>xterm</emphasis> window,
<userinput>Ctl-K</userinput> erases from the character <userinput>Ctl-K</userinput> erases from the character
under the cursor to end of line.</para> under the cursor to end of line. Within a script,
<userinput>Ctl-K</userinput> may behave differently,
as in Lee Lee Maschmeyer's example, below.</para>
</listitem> </listitem>
<listitem> <listitem>
<para><userinput>Ctl-L</userinput></para> <para><userinput>Ctl-L</userinput></para>
<para>Formfeed (clear the terminal screen). This has <para>Formfeed (clear the terminal screen). In a terminal,
the same effect as the <link this has the same effect as the <link
linkend="clearref">clear</link> command.</para> linkend="clearref">clear</link> command. When sent
to a printer, a <userinput>Ctl-L</userinput> causes
an advance to end of the paper sheet.</para>
</listitem> </listitem>
<listitem> <listitem>
@ -2795,7 +2808,8 @@ echo >&2 # The '-s' makes anything typed silent,
#+ so it is necessary to go to new line explicitly. #+ so it is necessary to go to new line explicitly.
read -n 1 -s -p $'Control-J leaves cursor on next line. \x0a' 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 &lt;Ctl-V&gt;&lt;Ctl-J&gt;</programlisting></para>
<title>Introduction to Variables and Parameters</title> <title>Introduction to Variables and Parameters</title>
<para><firstterm>Variables</firstterm> are how programming and <para><firstterm>Variables</firstterm> are how programming and
scripting languages represent data. They appear in arithmetic scripting languages represent data. A variable is nothing
operations and manipulation of quantities, in string parsing, and more than a <emphasis>label</emphasis>, a name assigned to a
they are indispensable for working in the abstract with symbols -- location or set of locations in computer memory holding an item
tokens that represent something else. A variable is nothing more of data.</para>
than a <emphasis>label</emphasis> assigned to a location or set
of locations in computer memory holding an item of data.</para> <para>Variables appear in arithmetic operations and manipulation of
quantities, and in string parsing.</para>
<sect1 id="varsubn"> <sect1 id="varsubn">
<title>Variable Substitution</title> <title>Variable Substitution</title>
@ -2959,10 +2975,23 @@ echo &lt;Ctl-V&gt;&lt;Ctl-J&gt;</programlisting></para>
<userinput>variable1</userinput> is the name of a <userinput>variable1</userinput> is the name of a
variable, then <userinput>$variable1</userinput> variable, then <userinput>$variable1</userinput>
is a reference to its <emphasis>value</emphasis>, is a reference to its <emphasis>value</emphasis>,
the data item it contains. The only time a the data item it contains.</para>
variable appears <quote>naked</quote> -- without
the <token>$</token> prefix -- is when declared <para>
or assigned, when <emphasis>unset</emphasis>, <screen><prompt>bash$ </prompt><userinput>variable=23</userinput>
<prompt>bash$ </prompt><userinput>echo variable</userinput>
<computeroutput>variable</computeroutput>
<prompt>bash$ </prompt><userinput>echo $variable</userinput>
<computeroutput>23</computeroutput></screen>
</para>
<para>The only time a variable appears <quote>naked</quote>
-- without the <token>$</token> prefix -- is when
declared or assigned, when <emphasis>unset</emphasis>,
when <link linkend="exportref">exported</link>, when <link linkend="exportref">exported</link>,
or in the special case of a variable representing or in the special case of a variable representing
a <link linkend="signald">signal</link> (see a <link linkend="signald">signal</link> (see
@ -3012,8 +3041,8 @@ let "uninitialized += 5" # Add 5 to it.
echo "$uninitialized" # 5 echo "$uninitialized" # 5
# Conclusion: # Conclusion:
# An uninitialized variable has no value, however # An uninitialized variable has no value,
#+ it acts as if it were 0 in an arithmetic operation. #+ however it acts as if it were 0 in an arithmetic operation.
# This is undocumented (and probably non-portable) behavior.</programlisting> # This is undocumented (and probably non-portable) behavior.</programlisting>
See also <xref linkend="selfsource">.</para> See also <xref linkend="selfsource">.</para>
@ -6039,10 +6068,13 @@ echo "FUNCNAME = $FUNCNAME" # FUNCNAME =
<para> <para>
<screen><prompt>bash$ </prompt><userinput>echo $IFS | cat -vte</userinput> <screen><prompt>bash$ </prompt><userinput>echo $IFS | cat -vte</userinput>
<computeroutput>$</computeroutput> <computeroutput>$</computeroutput>
<computeroutput>(Show tabs and display "$" at end-of-line.)</computeroutput>
<prompt>bash$ </prompt><userinput>bash -c 'set w x y z; IFS=":-;"; echo "$*"'</userinput> <prompt>bash$ </prompt><userinput>bash -c 'set w x y z; IFS=":-;"; echo "$*"'</userinput>
<computeroutput>w:x:y:z</computeroutput> <computeroutput>w:x:y:z</computeroutput>
<computeroutput>(Read commands from string and assign any arguments to pos params.)</computeroutput>
</screen> </screen>
</para> </para>
@ -6058,8 +6090,10 @@ echo "FUNCNAME = $FUNCNAME" # FUNCNAME =
<para>(Thanks, S. C., for clarification and examples.)</para> <para>(Thanks, S. C., for clarification and examples.)</para>
<para>See also <xref linkend="isspammer"> for an instructive <para>See also <xref linkend="isspammer">, <xref
example of using <varname>$IFS</varname>.</para> linkend="bingrep">, and <xref linkend="mailboxgrep">
for instructive examples of using
<varname>$IFS</varname>.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -7804,7 +7838,7 @@ echo "a = $a" # a =
# Different result from a=${param5+xyz} # Different result from a=${param5+xyz}
param6=123 param6=123
a=${param6+xyz} a=${param6:+xyz}
echo "a = $a" # a = xyz</programlisting></para> echo "a = $a" # a = xyz</programlisting></para>
</listitem> </listitem>
@ -8653,9 +8687,10 @@ echo $? # 1
<para>Omitting the <userinput>in [list]</userinput> part of a <para>Omitting the <userinput>in [list]</userinput> part of a
<firstterm>for loop</firstterm> causes the loop to operate <firstterm>for loop</firstterm> causes the loop to operate
on <token>$@</token> -- the list of arguments given on <token>$@</token> -- the <link linkend="posparamref">
on the command line to the script. A particularly clever positional parameters</link>. A particularly clever
illustration of this is <xref linkend="primes">.</para> illustration of this is <xref linkend="primes">. See also <xref
linkend="revposparams">.</para>
<example id="ex23"> <example id="ex23">
<title>Missing <userinput>in [list]</userinput> in a <title>Missing <userinput>in [list]</userinput> in a
@ -9920,6 +9955,13 @@ while read f; do
<programlisting>&ex34;</programlisting> <programlisting>&ex34;</programlisting>
</example> </example>
<para>More fun with positional parameters.</para>
<example id="revposparams">
<title>Reversing the positional parameters</title>
<programlisting>&revposparams;</programlisting>
</example>
<para>Invoking <command>set</command> without any options or <para>Invoking <command>set</command> without any options or
arguments simply lists all the <link arguments simply lists all the <link
linkend="envref">environmental</link> and other variables linkend="envref">environmental</link> and other variables
@ -11218,16 +11260,25 @@ tr a-z A-Z < filename # Same effect, but starts one less process,
<secondary>cp</secondary> <secondary>cp</secondary>
</indexterm> </indexterm>
<listitem> <listitem>
<para><anchor id="cpref"></para>
<para>This is the file copy command. <userinput>cp file1 <para>This is the file copy command. <userinput>cp file1
file2</userinput> copies <filename>file1</filename> file2</userinput> copies <filename>file1</filename>
to <filename>file2</filename>, overwriting to <filename>file2</filename>, overwriting
<filename>file2</filename> if it already exists (see <xref <filename>file2</filename> if it already exists (see <xref
linkend="ex42">).</para> linkend="ex42">).</para>
<tip><para>Particularly useful are the <option>-a</option> <tip>
archive flag (for copying an entire directory tree) <para>Particularly useful are the <option>-a</option>
archive flag (for copying an entire directory tree),
the <option>-u</option> update flag,
and the <option>-r</option> and <option>-R</option> and the <option>-r</option> and <option>-R</option>
recursive flags.</para></tip> recursive flags.</para>
<para><programlisting>cp -u source_dir/* dest_dir
# "Synchronize" dest_dir to source_dir
#+ by copying over all newer and not previously existing files.</programlisting></para>
</tip>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -12848,7 +12899,14 @@ Here is some text.</programlisting>
as <command>grep -E</command>. This uses a somewhat as <command>grep -E</command>. This uses a somewhat
different, extended set of <link linkend="regexref">Regular different, extended set of <link linkend="regexref">Regular
Expressions</link>, which can make the search a bit more Expressions</link>, which can make the search a bit more
flexible.</para> flexible. It also allows the boolean |
(<firstterm>or</firstterm>) operator.
<screen><prompt>bash $ </prompt><userinput>egrep 'matches|Matches' file.txt</userinput>
<computeroutput>Line 1 matches.
Line 3 Matches.
Line 4 contains matches, but also Matches</computeroutput>
</screen>
</para>
<para><command>fgrep</command> - <emphasis>fast grep</emphasis> <para><command>fgrep</command> - <emphasis>fast grep</emphasis>
- is the same as <command>grep -F</command>. It does - is the same as <command>grep -F</command>. It does
@ -13771,7 +13829,9 @@ function write_utf8_string {
<command>i</command>nput and <command>o</command>utput) <command>i</command>nput and <command>o</command>utput)
is rarely seen any more, having been supplanted by is rarely seen any more, having been supplanted by
<command>tar</command>/<command>gzip</command>. It still <command>tar</command>/<command>gzip</command>. It still
has its uses, such as moving a directory tree.</para> has its uses, such as moving a directory tree. With an
appropriate block size (for copying) specified, it
can be appreciably faster than <command>tar</command>.</para>
<example id="ex48"> <example id="ex48">
<title>Using <command>cpio</command> to move a directory tree</title> <title>Using <command>cpio</command> to move a directory tree</title>
@ -14637,11 +14697,18 @@ gzip -cd patchXX.gz | patch -p0
linkend="horserace"> for creative uses of linkend="horserace"> for creative uses of
the <command>md5sum</command> command.</para> the <command>md5sum</command> command.</para>
<note><para> <note>
<para>
There have been reports that the 128-bit There have been reports that the 128-bit
<command>md5sum</command> can be cracked, so the more secure <command>md5sum</command> can be cracked, so the more secure
160-bit <command>sha1sum</command> is a welcome new addition 160-bit <command>sha1sum</command> is a welcome new addition
to the checksum toolkit. to the checksum toolkit.
</para>
<para>Some security consultants think that even
<command>sha1sum</command> can be compromised. So, what's
next -- a 512-bit checksum utility?</para>
<screen><prompt>bash$ </prompt><userinput>md5sum testfile</userinput> <screen><prompt>bash$ </prompt><userinput>md5sum testfile</userinput>
<computeroutput>e181e2c8720c60522c4c4c981108e367 testfile</computeroutput> <computeroutput>e181e2c8720c60522c4c4c981108e367 testfile</computeroutput>
@ -14649,7 +14716,7 @@ gzip -cd patchXX.gz | patch -p0
<prompt>bash$ </prompt><userinput>sha1sum testfile</userinput> <prompt>bash$ </prompt><userinput>sha1sum testfile</userinput>
<computeroutput>5d7425a9c08a66c3177f1e31286fa40986ffc996 testfile</computeroutput> <computeroutput>5d7425a9c08a66c3177f1e31286fa40986ffc996 testfile</computeroutput>
</screen></para></note> </screen></note>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -17723,9 +17790,11 @@ setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ</programlisting><
<secondary>port scan</secondary> <secondary>port scan</secondary>
</indexterm> </indexterm>
<listitem> <listitem>
<para>Network port scanner. This command scans a server to <para><command>N</command>etwork <command>map</command>per
and port scanner. This command scans a server to
locate open ports and the services associated with those 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.</para> a network against hacking attempts.</para>
<para><programlisting>#!/bin/bash <para><programlisting>#!/bin/bash
@ -18314,7 +18383,8 @@ Average: all 6.33 1.70 14.71 0.00 77.26</compute
<para><replaceable>P</replaceable>rocess <para><replaceable>P</replaceable>rocess
<replaceable>S</replaceable>tatistics: lists currently <replaceable>S</replaceable>tatistics: lists currently
executing processes by owner and PID (process ID). This executing processes by owner and PID (process ID). This
is usually invoked with <option>ax</option> options, is usually invoked with <option>ax</option> or
<option>aux</option> options,
and may be piped to <link linkend="grepref">grep</link> and may be piped to <link linkend="grepref">grep</link>
or <link linkend="sedref">sed</link> to search for a or <link linkend="sedref">sed</link> to search for a
specific process (see <xref linkend="ex44"> and <xref specific process (see <xref linkend="ex44"> and <xref
@ -18330,6 +18400,53 @@ Average: all 6.33 1.70 14.71 0.00 77.26</compute
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><command>pgrep</command></term>
<term><command>pkill</command></term>
<indexterm>
<primary>pgrep</primary>
</indexterm>
<indexterm>
<primary>command</primary>
<secondary>process grep</secondary>
</indexterm>
<indexterm>
<primary>pkill</primary>
</indexterm>
<indexterm>
<primary>command</primary>
<secondary>process kill</secondary>
</indexterm>
<listitem>
<para>Combining the <command>ps</command> command
with <link linkend="grepref">grep</link> or
<link linkend="killref">kill</link>.</para>
<para>
<screen><prompt>bash$ </prompt><userinput>ps a | grep mingetty</userinput>
<computeroutput>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</computeroutput>
<prompt>bash$ </prompt><userinput>pgrep mingetty</userinput>
<computeroutput>2212 mingetty
2213 mingetty
2214 mingetty
2215 mingetty
2216 mingetty</computeroutput>
</screen>
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><command>pstree</command></term> <term><command>pstree</command></term>
<indexterm> <indexterm>
@ -19351,6 +19468,9 @@ mount -o loop /dev/loop0 /mnt # Mount it.
in use by a particular process (<quote>busy</quote>), and in use by a particular process (<quote>busy</quote>), and
this permits only restricted access (or no access) to other this permits only restricted access (or no access) to other
processes.</para> processes.</para>
<para><programlisting>lockfile /home/bozo/lockfiles/$0.lock
# Creates a write-protected lockfile prefixed with the name of the script.</programlisting></para>
<para>Lock files are used in such applications as protecting <para>Lock files are used in such applications as protecting
system mail folders from simultaneously being changed system mail folders from simultaneously being changed
@ -19364,7 +19484,12 @@ mount -o loop /dev/loop0 /mnt # Mount it.
<para>Normally, applications create and check for lock files <para>Normally, applications create and check for lock files
in the <filename class="directory">/var/lock</filename> in the <filename class="directory">/var/lock</filename>
directory. A script can test for the presence of a lock file by directory.
<footnote><para>Since only <firstterm>root</firstterm>
has write permission in the <filename
class="directory">/var/lock</filename> directory,
a user script cannot set a lock file there.</para></footnote>
A script can test for the presence of a lock file by
something like the following. something like the following.
<programlisting>appname=xyzip <programlisting>appname=xyzip
# Application "xyzip" created lock file "/var/lock/xyzip.lock". # Application "xyzip" created lock file "/var/lock/xyzip.lock".
@ -19376,6 +19501,35 @@ then
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><command>flock</command></term>
<indexterm>
<primary>flock</primary>
</indexterm>
<indexterm>
<primary>command</primary>
<secondary>lock file</secondary>
</indexterm>
<listitem>
<para>Much less useful than the <command>lockfile</command>
command is <command>flock</command>. It sets an
<quote>advisory</quote> 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.</para>
<para><programlisting>flock $0 cat $0 > lockfile__$0
# Set a lock on the script the above line appears in,
#+ while listing the script to stdout.</programlisting></para>
<note><para>Unlike <command>lockfile</command>,
<command>flock</command> does <emphasis>not</emphasis>
automatically create a lock file.</para></note>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><command>mknod</command></term> <term><command>mknod</command></term>
<indexterm> <indexterm>
@ -21044,11 +21198,35 @@ echo "This line had better not echo." # Follows an 'exit' command.</programlist
is expanded and fed to the <filename>stdin</filename> of is expanded and fed to the <filename>stdin</filename> of
<varname>COMMAND</varname>.</para> <varname>COMMAND</varname>.</para>
<para>
<programlisting>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.</programlisting>
</para>
<example id="prependex"> <example id="prependex">
<title>Prepending a line to a file</title> <title>Prepending a line to a file</title>
<programlisting>&prependex;</programlisting> <programlisting>&prependex;</programlisting>
</example> </example>
<example id="mailboxgrep">
<title>Parsing a mailbox</title>
<programlisting>&mailboxgrep;</programlisting>
</example>
<para>Exercise: Find other uses for <emphasis>here <para>Exercise: Find other uses for <emphasis>here
strings</emphasis>.</para> strings</emphasis>.</para>
@ -21114,6 +21292,12 @@ echo "This line had better not echo." # Follows an 'exit' command.</programlist
<chapter id="regexp"> <chapter id="regexp">
<title>Regular Expressions</title> <title>Regular Expressions</title>
<epigraph>
<attribution>Stowe Boyd</attribution>
<para>. . . the intellectual activity associated with software
development is largely one of gaining insight.</para>
</epigraph>
<para><anchor id="regexref"></para> <para><anchor id="regexref"></para>
<para>To fully utilize the power of shell scripting, you need to <para>To fully utilize the power of shell scripting, you need to
@ -22072,6 +22256,8 @@ fi
<chapter id="restricted-sh"> <chapter id="restricted-sh">
<title>Restricted Shells</title> <title>Restricted Shells</title>
<para><anchor id="restrictedshref"></para>
<variablelist id="disabledcommref0"> <variablelist id="disabledcommref0">
<title><anchor id="disabledcommref">Disabled commands in restricted <title><anchor id="disabledcommref">Disabled commands in restricted
shells</title> shells</title>
@ -26058,6 +26244,9 @@ fi
<sect1 id="securityissues"> <sect1 id="securityissues">
<title>Security Issues</title> <title>Security Issues</title>
<sect2 id="infectedscripts">
<title>Infected Shell Scripts</title>
<para>A brief warning about script security is appropriate. <para>A brief warning about script security is appropriate.
A shell script may contain a <emphasis>worm</emphasis>, A shell script may contain a <emphasis>worm</emphasis>,
<emphasis>trojan</emphasis>, or even a <emphasis>virus</emphasis>. <emphasis>trojan</emphasis>, or even a <emphasis>virus</emphasis>.
@ -26083,6 +26272,27 @@ fi
look at and understand scripts may protect your system from look at and understand scripts may protect your system from
being hacked or damaged.</para> being hacked or damaged.</para>
</sect2> <!-- Infected Shell Scripts -->
<sect2 id="hidingsource">
<title>Hiding Shell Script Source</title>
<para>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' <ulink
url="http://www.datsi.fi.upm.es/~frosal/sources/">shc -
generic shell script compiler</ulink> does exactly that.</para>
<para>Unfortunately, according to <ulink
url="http://www.linuxjournal.com/article/8256">an article in
the October, 2005 <emphasis>Linux Journal</emphasis></ulink>,
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.</para>
</sect2> <!-- Hiding Shell Script Source -->
</sect1> <!-- Security --> </sect1> <!-- Security -->
@ -26331,8 +26541,9 @@ else
# Or, ask for corrected input. # Or, ask for corrected input.
fi</programlisting></para> fi</programlisting></para>
<para>For another example of using the <command>=~</command> <para>For additional examples of using the
operator, see <xref linkend="whx">.</para> <command>=~</command> operator, see <xref linkend="whx">
and <xref linkend="mailboxgrep">.</para>
</listitem> </listitem>
@ -26628,29 +26839,29 @@ fi</programlisting></para>
errors and other corrections. Special thanks!</para> errors and other corrections. Special thanks!</para>
<para>Others contributing scripts, making helpful suggestions, and <para>Others contributing scripts, making helpful suggestions, and
pointing out errors were Gabor Kiss, Leopold Toetsch, Peter pointing out errors were Gabor Kiss, Leopold Toetsch,
Tillier, Marcus Berglof, Tony Richardson, Nick Drage (script Peter Tillier, Marcus Berglof, Tony Richardson, Nick Drage
ideas!), Rich Bartell, Jess Thrysoee, Adam Lazur, Bram Moolenaar, (script ideas!), Rich Bartell, Jess Thrysoee, Adam Lazur, Bram
Baris Cicek, Greg Keraunen, Keith Matthews, Sandro Magi, Albert Moolenaar, Baris Cicek, Greg Keraunen, Keith Matthews, Sandro
Reiner, Dim Segebart, Rory Winston, Lee Bigelow, Wayne Pollock, Magi, Albert Reiner, Dim Segebart, Rory Winston, Lee Bigelow,
<quote>jipe,</quote> <quote>bojster,</quote> <quote>nyal,</quote> Wayne Pollock, <quote>jipe,</quote> <quote>bojster,</quote>
<quote>Hobbit,</quote> <quote>Ender,</quote> <quote>Little <quote>nyal,</quote> <quote>Hobbit,</quote> <quote>Ender,</quote>
Monster</quote> (Alexis), <quote>Mark,</quote> Emilio Conti, <quote>Little Monster</quote> (Alexis), <quote>Mark,</quote>
Ian. D. Allen, Arun Giridhar, Dennis Leeuw, Dan Jacobson, Aurelio Emilio Conti, Ian. D. Allen, Arun Giridhar, Dennis Leeuw, Dan
Marinho Jargas, Edward Scholtz, Jean Helou, Chris Martin, Jacobson, Aurelio Marinho Jargas, Edward Scholtz, Jean Helou,
Lee Maschmeyer, Bruno Haible, Wilbert Berendsen, Sebastien Chris Martin, Lee Maschmeyer, Bruno Haible, Wilbert Berendsen,
Godard, Bj&ouml;n Eriksson, John MacDonald, Joshua Tschida, Sebastien Godard, Bj&ouml;n Eriksson, John MacDonald, Joshua
Troy Engel, Manfred Schwarb, Amit Singh, Bill Gradwohl, David Tschida, Troy Engel, Manfred Schwarb, Amit Singh, Bill Gradwohl,
Lombard, Jason Parker, Steve Parker, Bruce W. Clare, William David Lombard, Jason Parker, Steve Parker, Bruce W. Clare,
Park, Vernia Damiano, Mihai Maties, Jeremy Impson, Ken Fuchs, William Park, Vernia Damiano, Mihai Maties, Jeremy Impson,
Frank Wang, Sylvain Fourmanoit, Matthew Walker, Kenny Stauffer, Ken Fuchs, Frank Wang, Sylvain Fourmanoit, Matthew Walker,
Filip Moritz, Andrzej Stefanski, Daniel Albers, Stefano Palmeri, Kenny Stauffer, Filip Moritz, Andrzej Stefanski, Daniel Albers,
Nils Radtke, Jeroen Domburg, Alfredo Pironti, Phil Braham, Stefano Palmeri, Nils Radtke, Jeroen Domburg, Alfredo Pironti,
Bruno de Oliveira Schneider, Stefano Falsetto, Chris Morgan, Phil Braham, Bruno de Oliveira Schneider, Stefano Falsetto,
Walter Dnes, Linc Fessenden, Michael Iatrou, Pharis Monalo, Chris Morgan, Walter Dnes, Linc Fessenden, Michael Iatrou, Pharis
Jesse Gough, Fabian Kreutz, Mark Norman, Harald Koenig, Peter Monalo, Jesse Gough, Fabian Kreutz, Mark Norman, Harald Koenig,
Knowles, Mariusz Gniazdowski, Tedman Eng, and David Lawyer Peter Knowles, Francisco Lobo, Mariusz Gniazdowski, Tedman Eng,
(himself an author of four HOWTOs).</para> and David Lawyer (himself an author of four HOWTOs).</para>
<para>My gratitude to <ulink url="mailto:chet@po.cwru.edu">Chet <para>My gratitude to <ulink url="mailto:chet@po.cwru.edu">Chet
Ramey</ulink> and Brian Fox for writing <firstterm>Bash</firstterm>, Ramey</ulink> and Brian Fox for writing <firstterm>Bash</firstterm>,
@ -29022,7 +29233,17 @@ exit 0</programlisting>
<!-- A Detailed Introduction to I/O and I/O Redirection --> <!-- A Detailed Introduction to I/O and I/O Redirection -->
<appendix id="standard-options"> <appendix id="command-line-options">
<title>Command-Line Options</title>
<para>Many executables, whether binaries or script files, accept
options to modify their run-time behavior. For example: from
the command line, typing <command>command -o</command>
would invoke <emphasis>command</emphasis>, with option
<option>o</option>.</para>
<sect1 id="standard-options">
<title>Standard Command-Line Options</title> <title>Standard Command-Line Options</title>
<para>Over time, there has evolved a loose standard for the <para>Over time, there has evolved a loose standard for the
@ -29142,8 +29363,64 @@ exit 0</programlisting>
is available at <ulink is available at <ulink
url="http://www.gnu.org/prep/standards_19.html">http://www.gnu.org/prep/standards_19.html</ulink>.</para> url="http://www.gnu.org/prep/standards_19.html">http://www.gnu.org/prep/standards_19.html</ulink>.</para>
</sect1>
<sect1 id="bash-options">
<title>Bash Command-Line Options</title>
<para><firstterm>Bash</firstterm> itself has a number of command-line
options. Here are some of the more useful ones.</para>
<itemizedlist id="bash-commline-opts">
<listitem>
<para><option>-c</option></para>
<para><emphasis>Read commands from the following string and assign any
arguments to the <link linkend="posparamref">positional
parameters</link>.</emphasis></para>
<para>
<screen>
<prompt>bash$ </prompt><userinput>bash -c 'set a b c d; IFS="+-;"; echo "$*"'</userinput>
<computeroutput>a+b+c+d</computeroutput>
</screen>
</para>
</listitem>
<listitem>
<para><option>-r</option></para>
<para><option>--restricted</option></para>
<para><emphasis>Runs the shell, or a script, in <link
linkend="restrictedshref">restricted mode</link>.</emphasis></para>
</listitem>
<listitem>
<para><option>--posix</option></para>
<para><emphasis>Forces Bash to conform to <link
linkend="posix2ref">POSIX</link> mode.</emphasis></para>
</listitem>
<listitem>
<para><option>--version</option></para>
<para><emphasis>Display Bash version information and
exit.</emphasis></para>
</listitem>
<listitem>
<para><option>--</option></para>
<para><emphasis>End of options. Anything further on the command
line is an argument, not an option.</emphasis></para>
</listitem>
</itemizedlist>
</sect1>
</appendix> </appendix>
<!-- End Standard Command-Line Options appendix --> <!-- End Command-Line Options appendix -->
<appendix id="files"> <appendix id="files">
@ -31262,6 +31539,11 @@ fairly detailed rundown on the Playfair Cipher and its solution methods.</progra
<entry>23 Oct 2005</entry> <entry>23 Oct 2005</entry>
<entry>WHORTLEBERRY release: Bugfixes, some materials added.</entry> <entry>WHORTLEBERRY release: Bugfixes, some materials added.</entry>
</row> </row>
<row>
<entry><option>3.8</option></entry>
<entry>26 Feb 2006</entry>
<entry>BLAEBERRY release: Bugfixes, some materials added.</entry>
</row>
</tbody> </tbody>
</tgroup> </tgroup>
</table> </table>
@ -31352,9 +31634,14 @@ fairly detailed rundown on the Playfair Cipher and its solution methods.</progra
to take over custodianship of the document and name a new maintainer, to take over custodianship of the document and name a new maintainer,
who would then have the right to update and modify the document. who would then have the right to update and modify the document.
B. Distribution of the work or derivative of the work in any standard B. This document may NOT be distributed encrypted or with any form of
DRM (Digital Rights Management) embedded in it. Nor may this document
be bundled with other DRM-ed works.
C. Distribution of the work or derivative of the work in any standard
(paper) book form is prohibited unless prior permission is obtained from (paper) book form is prohibited unless prior permission is obtained from
the copyright holder.</programlisting></para> the copyright holder.</programlisting></para>
<para><emphasis>Provision A</emphasis>, above, explicitly prohibits <para><emphasis>Provision A</emphasis>, above, explicitly prohibits
<emphasis>relabeling</emphasis> this document. An example of <emphasis>relabeling</emphasis> this document. An example of

View File

@ -67,3 +67,11 @@ ls $datadir | grep -v m3u > $datadir/podcast.m3u
exit 0 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
#################################################

View File

@ -20,7 +20,8 @@ then
fi 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" ) for word in $( strings "$2" | grep "$1" )
# The "strings" command lists strings in binary files. # The "strings" command lists strings in binary files.
# Output then piped to "grep", which tests for desired string. # Output then piped to "grep", which tests for desired string.
@ -28,7 +29,7 @@ do
echo $word echo $word
done 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*]' # strings "$2" | grep "$1" | tr -s "$IFS" '[\n*]'

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# Copying a directory tree using 'cpio.' # Copying a directory tree using cpio.
# Advantages of using 'cpio': # Advantages of using 'cpio':
# Speed of copying. It's faster than 'tar' with pipes. # Speed of copying. It's faster than 'tar' with pipes.
@ -19,9 +19,10 @@ fi
source=$1 source=$1
destination=$2 destination=$2
find "$source" -depth | cpio -admvp "$destination" 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: # Exercise:

View File

@ -9,13 +9,13 @@ hello=$a
# No space permitted on either side of = sign when initializing variables. # No space permitted on either side of = sign when initializing variables.
# What happens if there is a space? # What happens if there is a space?
# If "VARIABLE =value", # "VARIABLE =value"
# ^ # ^
#+ script tries to run "VARIABLE" command with one argument, "=value". #% Script tries to run "VARIABLE" command with one argument, "=value".
# If "VARIABLE= value", # "VARIABLE= value"
# ^ # ^
#+ script tries to run "value" command with #% Script tries to run "value" command with
#+ the environmental variable "VARIABLE" set to "". #+ 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
echo "$hello" # A B C D echo "$hello" # A B C D
# As you see, echo $hello and echo "$hello" give different results. # As you see, echo $hello and echo "$hello" give different results.
# ^ ^ # =======================================
# Quoting a variable preserves whitespace. # Quoting a variable preserves whitespace.
# =======================================
echo echo
@ -62,7 +63,7 @@ var1=21 var2=22 var3=$V3
echo echo
echo "var1=$var1 var2=$var2 var3=$var3" 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, # If there is whitespace embedded within a variable,
#+ then quotes are necessary. #+ then quotes are necessary.
# other_numbers=1 2 3 # Gives an error message.
echo "numbers = $numbers" echo "numbers = $numbers"
echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 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" echo "uninitialized_variable = $uninitialized_variable"
# Uninitialized variable has null value (no value at all). # Uninitialized variable has null value (no value at all).

View File

@ -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

View File

@ -3,6 +3,8 @@
E_PARAM_ERR=-198 # If less than 2 params passed to function. E_PARAM_ERR=-198 # If less than 2 params passed to function.
EQUAL=-199 # Return value if both params equal. 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. max2 () # Returns larger of two numbers.
{ # Note: numbers compared must be less than 257. { # Note: numbers compared must be less than 257.

View File

@ -6,6 +6,7 @@
EQUAL=0 # Return value if both params equal. EQUAL=0 # Return value if both params equal.
E_PARAM_ERR=-99999 # Not enough params passed to function. 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. max2 () # "Returns" larger of two numbers.
{ {
@ -29,7 +30,7 @@ else
fi fi
echo $retval # Echoes (to stdout), rather than returning value. echo $retval # Echoes (to stdout), rather than returning value.
# Why?
} }

View File

@ -33,8 +33,9 @@ exit 0
#+ so that this script, for example, runs correctly. #+ so that this script, for example, runs correctly.
# (Thanks to Heiner Steven for pointing this out.) # (Thanks to Heiner Steven for pointing this out.)
# However . . . # However . . .
# Bash *can* sometimes start a subshell in a *redirected* "while" loop. # Bash *can* sometimes start a subshell in a PIPED "while-read" loop,
#+ as distinct from a REDIRECTED "while" loop.
abc=hi abc=hi
echo -e "1\n2\n3" | while read l echo -e "1\n2\n3" | while read l
@ -43,5 +44,6 @@ echo -e "1\n2\n3" | while read l
done done
echo $abc echo $abc
# (Thanks, Bruno de Oliveira Schneider, for demonstrating this # Thanks, Bruno de Oliveira Schneider, for demonstrating this
#+ with the above snippet of code.) #+ with the above snippet of code.
# And, thanks, Brian Onn, for correcting an annotation error.

View File

@ -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