mirror of https://github.com/tLDP/LDP
updated
This commit is contained in:
parent
e614d9d757
commit
53465e6309
|
@ -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
|
||||
|
||||
|
|
|
@ -247,6 +247,7 @@ Uncomment line below to generate index.
|
|||
<!ENTITY fileintegrity SYSTEM "file-integrity.sh">
|
||||
<!ENTITY readnovar SYSTEM "read-novar.sh">
|
||||
<!ENTITY setpos SYSTEM "set-pos.sh">
|
||||
<!ENTITY revposparams SYSTEM "revposparams.sh">
|
||||
<!ENTITY badread SYSTEM "badread.sh">
|
||||
<!ENTITY selfexec SYSTEM "self-exec.sh">
|
||||
<!ENTITY selfdestruct SYSTEM "self-destruct.sh">
|
||||
|
@ -327,6 +328,7 @@ Uncomment line below to generate index.
|
|||
<!ENTITY prasc SYSTEM "pr-asc.sh">
|
||||
<!ENTITY whx SYSTEM "whx.sh">
|
||||
<!ENTITY spamlookup SYSTEM "spam-lookup.sh">
|
||||
<!ENTITY mailboxgrep SYSTEM "mailbox_grep.sh">
|
||||
<!ENTITY fc4upd SYSTEM "fc4upd.sh">
|
||||
<!ENTITY gen0data SYSTEM "gen0">
|
||||
<!ENTITY cdll SYSTEM "cdll">
|
||||
|
@ -348,19 +350,12 @@ Uncomment line below to generate index.
|
|||
</affiliation>
|
||||
</author>
|
||||
|
||||
<releaseinfo>3.7</releaseinfo>
|
||||
<pubdate>23 October 2005</pubdate>
|
||||
<releaseinfo>3.8</releaseinfo>
|
||||
<pubdate>26 February 2006</pubdate>
|
||||
|
||||
|
||||
<revhistory>
|
||||
|
||||
<revision>
|
||||
<revnumber>3.5</revnumber>
|
||||
<date>04 June 2005</date>
|
||||
<authorinitials>mc</authorinitials>
|
||||
<revremark>'BOXBERRY' release: Important Update.</revremark>
|
||||
</revision>
|
||||
|
||||
<revision>
|
||||
<revnumber>3.6</revnumber>
|
||||
<date>28 Aug 2005</date>
|
||||
|
@ -375,6 +370,13 @@ Uncomment line below to generate index.
|
|||
<revremark>'WHORTLEBERRY' release: Bugfix Update.</revremark>
|
||||
</revision>
|
||||
|
||||
<revision>
|
||||
<revnumber>3.8</revnumber>
|
||||
<date>26 Feb 2006</date>
|
||||
<authorinitials>mc</authorinitials>
|
||||
<revremark>'BLAEBERRY' release: Minor Update.</revremark>
|
||||
</revision>
|
||||
|
||||
</revhistory>
|
||||
|
||||
|
||||
|
@ -395,7 +397,7 @@ Uncomment line below to generate index.
|
|||
introduction to programming concepts.</para>
|
||||
|
||||
<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
|
||||
linkend="bzipref">bzip2-ed</link> <quote>tarball</quote>
|
||||
including both the SGML source and rendered HTML, may
|
||||
|
@ -897,7 +899,6 @@ fi</programlisting>
|
|||
<title>Basics</title>
|
||||
|
||||
|
||||
|
||||
<chapter id="special-chars">
|
||||
<title>Special Characters</title>
|
||||
|
||||
|
@ -2695,8 +2696,15 @@ echo $a # 28</programlisting></para>
|
|||
<para><anchor id="controlcharref"></para>
|
||||
<formalpara><title> change the behavior of the
|
||||
terminal or text display.</title>
|
||||
|
||||
<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>
|
||||
<para>Control characters are not normally useful inside a
|
||||
script.</para>
|
||||
|
@ -2763,7 +2771,8 @@ echo; echo</programlisting>
|
|||
|
||||
<listitem>
|
||||
<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>
|
||||
|
@ -2772,14 +2781,18 @@ echo; echo</programlisting>
|
|||
<para>When typing text on the console or in an
|
||||
<emphasis>xterm</emphasis> window,
|
||||
<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>
|
||||
<para><userinput>Ctl-L</userinput></para>
|
||||
<para>Formfeed (clear the terminal screen). This has
|
||||
the same effect as the <link
|
||||
linkend="clearref">clear</link> command.</para>
|
||||
<para>Formfeed (clear the terminal screen). In a terminal,
|
||||
this has the same effect as the <link
|
||||
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>
|
||||
|
@ -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></programlisting></para>
|
|||
<title>Introduction to Variables and Parameters</title>
|
||||
|
||||
<para><firstterm>Variables</firstterm> 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 <emphasis>label</emphasis> assigned to a location or set
|
||||
of locations in computer memory holding an item of data.</para>
|
||||
scripting languages represent data. A variable is nothing
|
||||
more than a <emphasis>label</emphasis>, a name 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">
|
||||
<title>Variable Substitution</title>
|
||||
|
@ -2959,10 +2975,23 @@ echo <Ctl-V><Ctl-J></programlisting></para>
|
|||
<userinput>variable1</userinput> is the name of a
|
||||
variable, then <userinput>$variable1</userinput>
|
||||
is a reference to its <emphasis>value</emphasis>,
|
||||
the data item it contains. The only time a
|
||||
variable appears <quote>naked</quote> -- without
|
||||
the <token>$</token> prefix -- is when declared
|
||||
or assigned, when <emphasis>unset</emphasis>,
|
||||
the data item it contains.</para>
|
||||
|
||||
<para>
|
||||
<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>,
|
||||
or in the special case of a variable representing
|
||||
a <link linkend="signald">signal</link> (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.</programlisting>
|
||||
|
||||
See also <xref linkend="selfsource">.</para>
|
||||
|
@ -6039,10 +6068,13 @@ echo "FUNCNAME = $FUNCNAME" # FUNCNAME =
|
|||
<para>
|
||||
<screen><prompt>bash$ </prompt><userinput>echo $IFS | cat -vte</userinput>
|
||||
<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>
|
||||
<computeroutput>w:x:y:z</computeroutput>
|
||||
<computeroutput>(Read commands from string and assign any arguments to pos params.)</computeroutput>
|
||||
</screen>
|
||||
</para>
|
||||
|
||||
|
@ -6058,8 +6090,10 @@ echo "FUNCNAME = $FUNCNAME" # FUNCNAME =
|
|||
|
||||
<para>(Thanks, S. C., for clarification and examples.)</para>
|
||||
|
||||
<para>See also <xref linkend="isspammer"> for an instructive
|
||||
example of using <varname>$IFS</varname>.</para>
|
||||
<para>See also <xref linkend="isspammer">, <xref
|
||||
linkend="bingrep">, and <xref linkend="mailboxgrep">
|
||||
for instructive examples of using
|
||||
<varname>$IFS</varname>.</para>
|
||||
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
@ -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</programlisting></para>
|
||||
|
||||
</listitem>
|
||||
|
@ -8653,9 +8687,10 @@ echo $? # 1
|
|||
|
||||
<para>Omitting the <userinput>in [list]</userinput> part of a
|
||||
<firstterm>for loop</firstterm> causes the loop to operate
|
||||
on <token>$@</token> -- the list of arguments given
|
||||
on the command line to the script. A particularly clever
|
||||
illustration of this is <xref linkend="primes">.</para>
|
||||
on <token>$@</token> -- the <link linkend="posparamref">
|
||||
positional parameters</link>. A particularly clever
|
||||
illustration of this is <xref linkend="primes">. See also <xref
|
||||
linkend="revposparams">.</para>
|
||||
|
||||
<example id="ex23">
|
||||
<title>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</title>
|
||||
<programlisting>&revposparams;</programlisting>
|
||||
</example>
|
||||
|
||||
<para>Invoking <command>set</command> without any options or
|
||||
arguments simply lists all the <link
|
||||
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>
|
||||
</indexterm>
|
||||
<listitem>
|
||||
|
||||
<para><anchor id="cpref"></para>
|
||||
|
||||
<para>This is the file copy command. <userinput>cp file1
|
||||
file2</userinput> copies <filename>file1</filename>
|
||||
to <filename>file2</filename>, overwriting
|
||||
<filename>file2</filename> if it already exists (see <xref
|
||||
linkend="ex42">).</para>
|
||||
|
||||
<tip><para>Particularly useful are the <option>-a</option>
|
||||
archive flag (for copying an entire directory tree)
|
||||
<tip>
|
||||
<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>
|
||||
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>
|
||||
</varlistentry>
|
||||
|
@ -12848,7 +12899,14 @@ Here is some text.</programlisting>
|
|||
as <command>grep -E</command>. This uses a somewhat
|
||||
different, extended set of <link linkend="regexref">Regular
|
||||
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>
|
||||
- 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)
|
||||
is rarely seen any more, having been supplanted by
|
||||
<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">
|
||||
<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
|
||||
the <command>md5sum</command> command.</para>
|
||||
|
||||
<note><para>
|
||||
<note>
|
||||
|
||||
<para>
|
||||
There have been reports that the 128-bit
|
||||
<command>md5sum</command> can be cracked, so the more secure
|
||||
160-bit <command>sha1sum</command> is a welcome new addition
|
||||
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>
|
||||
<computeroutput>e181e2c8720c60522c4c4c981108e367 testfile</computeroutput>
|
||||
|
@ -14649,7 +14716,7 @@ gzip -cd patchXX.gz | patch -p0
|
|||
|
||||
<prompt>bash$ </prompt><userinput>sha1sum testfile</userinput>
|
||||
<computeroutput>5d7425a9c08a66c3177f1e31286fa40986ffc996 testfile</computeroutput>
|
||||
</screen></para></note>
|
||||
</screen></note>
|
||||
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
@ -17723,9 +17790,11 @@ setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ</programlisting><
|
|||
<secondary>port scan</secondary>
|
||||
</indexterm>
|
||||
<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
|
||||
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>
|
||||
<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
|
||||
<replaceable>S</replaceable>tatistics: lists currently
|
||||
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>
|
||||
or <link linkend="sedref">sed</link> to search for a
|
||||
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>
|
||||
</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>
|
||||
<term><command>pstree</command></term>
|
||||
<indexterm>
|
||||
|
@ -19351,6 +19468,9 @@ mount -o loop /dev/loop0 /mnt # Mount it.
|
|||
in use by a particular process (<quote>busy</quote>), and
|
||||
this permits only restricted access (or no access) to other
|
||||
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
|
||||
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
|
||||
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.
|
||||
<programlisting>appname=xyzip
|
||||
# Application "xyzip" created lock file "/var/lock/xyzip.lock".
|
||||
|
@ -19376,6 +19501,35 @@ then
|
|||
</listitem>
|
||||
</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>
|
||||
<term><command>mknod</command></term>
|
||||
<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
|
||||
<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">
|
||||
<title>Prepending a line to a file</title>
|
||||
<programlisting>&prependex;</programlisting>
|
||||
</example>
|
||||
|
||||
<example id="mailboxgrep">
|
||||
<title>Parsing a mailbox</title>
|
||||
<programlisting>&mailboxgrep;</programlisting>
|
||||
</example>
|
||||
|
||||
<para>Exercise: Find other uses for <emphasis>here
|
||||
strings</emphasis>.</para>
|
||||
|
||||
|
@ -21114,6 +21292,12 @@ echo "This line had better not echo." # Follows an 'exit' command.</programlist
|
|||
<chapter id="regexp">
|
||||
<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>To fully utilize the power of shell scripting, you need to
|
||||
|
@ -22072,6 +22256,8 @@ fi
|
|||
<chapter id="restricted-sh">
|
||||
<title>Restricted Shells</title>
|
||||
|
||||
<para><anchor id="restrictedshref"></para>
|
||||
|
||||
<variablelist id="disabledcommref0">
|
||||
<title><anchor id="disabledcommref">Disabled commands in restricted
|
||||
shells</title>
|
||||
|
@ -26058,6 +26244,9 @@ fi
|
|||
<sect1 id="securityissues">
|
||||
<title>Security Issues</title>
|
||||
|
||||
<sect2 id="infectedscripts">
|
||||
<title>Infected Shell Scripts</title>
|
||||
|
||||
<para>A brief warning about script security is appropriate.
|
||||
A shell script may contain a <emphasis>worm</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
|
||||
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 -->
|
||||
|
||||
|
||||
|
@ -26331,8 +26541,9 @@ else
|
|||
# Or, ask for corrected input.
|
||||
fi</programlisting></para>
|
||||
|
||||
<para>For another example of using the <command>=~</command>
|
||||
operator, see <xref linkend="whx">.</para>
|
||||
<para>For additional examples of using the
|
||||
<command>=~</command> operator, see <xref linkend="whx">
|
||||
and <xref linkend="mailboxgrep">.</para>
|
||||
|
||||
|
||||
</listitem>
|
||||
|
@ -26628,29 +26839,29 @@ fi</programlisting></para>
|
|||
errors and other corrections. Special thanks!</para>
|
||||
|
||||
<para>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,
|
||||
<quote>jipe,</quote> <quote>bojster,</quote> <quote>nyal,</quote>
|
||||
<quote>Hobbit,</quote> <quote>Ender,</quote> <quote>Little
|
||||
Monster</quote> (Alexis), <quote>Mark,</quote> 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).</para>
|
||||
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, <quote>jipe,</quote> <quote>bojster,</quote>
|
||||
<quote>nyal,</quote> <quote>Hobbit,</quote> <quote>Ender,</quote>
|
||||
<quote>Little Monster</quote> (Alexis), <quote>Mark,</quote>
|
||||
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).</para>
|
||||
|
||||
<para>My gratitude to <ulink url="mailto:chet@po.cwru.edu">Chet
|
||||
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 -->
|
||||
|
||||
|
||||
<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>
|
||||
|
||||
<para>Over time, there has evolved a loose standard for the
|
||||
|
@ -29142,8 +29363,64 @@ exit 0</programlisting>
|
|||
is available at <ulink
|
||||
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>
|
||||
<!-- End Standard Command-Line Options appendix -->
|
||||
<!-- End Command-Line Options appendix -->
|
||||
|
||||
|
||||
<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>WHORTLEBERRY release: Bugfixes, some materials added.</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>3.8</option></entry>
|
||||
<entry>26 Feb 2006</entry>
|
||||
<entry>BLAEBERRY release: Bugfixes, some materials added.</entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</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,
|
||||
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
|
||||
the copyright holder.</programlisting></para>
|
||||
|
||||
|
||||
<para><emphasis>Provision A</emphasis>, above, explicitly prohibits
|
||||
<emphasis>relabeling</emphasis> this document. An example of
|
||||
|
|
|
@ -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
|
||||
#################################################
|
||||
|
|
|
@ -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*]'
|
||||
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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).
|
||||
|
|
|
@ -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
|
|
@ -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.
|
||||
|
|
|
@ -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?
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue