|
|
|
@ -263,6 +263,7 @@ Uncomment line below to generate index.
|
|
|
|
|
<!ENTITY usagemessage SYSTEM "usage-message.sh">
|
|
|
|
|
<!ENTITY colorecho SYSTEM "color-echo.sh">
|
|
|
|
|
<!ENTITY selfsource SYSTEM "self-source.sh">
|
|
|
|
|
<!ENTITY arrowdetect SYSTEM "arrow-detect.sh">
|
|
|
|
|
<!ENTITY namesdata SYSTEM "names.data">
|
|
|
|
|
<!ENTITY gen0data SYSTEM "gen0">
|
|
|
|
|
<!ENTITY bashrc SYSTEM "bashrc">
|
|
|
|
@ -283,8 +284,8 @@ Uncomment line below to generate index.
|
|
|
|
|
</affiliation>
|
|
|
|
|
</author>
|
|
|
|
|
|
|
|
|
|
<releaseinfo>1.5</releaseinfo>
|
|
|
|
|
<pubdate>13 July 2002</pubdate>
|
|
|
|
|
<releaseinfo>1.6</releaseinfo>
|
|
|
|
|
<pubdate>29 September 2002</pubdate>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<revhistory>
|
|
|
|
@ -373,6 +374,14 @@ Uncomment line below to generate index.
|
|
|
|
|
and scripts added.</revremark>
|
|
|
|
|
</revision>
|
|
|
|
|
|
|
|
|
|
<revision>
|
|
|
|
|
<revnumber>1.6</revnumber>
|
|
|
|
|
<date>29 September 2002</date>
|
|
|
|
|
<authorinitials>mc</authorinitials>
|
|
|
|
|
<revremark>'POMEGRANATE' release: some bugfixes, more material, one
|
|
|
|
|
more script added.</revremark>
|
|
|
|
|
</revision>
|
|
|
|
|
|
|
|
|
|
</revhistory>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -393,7 +402,7 @@ Uncomment line below to generate index.
|
|
|
|
|
linkend="bzipref">bzip2-ed</link> <quote>tarball</quote>
|
|
|
|
|
including both the SGML source and
|
|
|
|
|
rendered HTML, may be downloaded from <ulink
|
|
|
|
|
url="http://personal.riverusers.com/~thegrendel/abs-guide-1.5.tar.bz2">
|
|
|
|
|
url="http://personal.riverusers.com/~thegrendel/abs-guide-1.6.tar.bz2">
|
|
|
|
|
the author's home site</ulink>. See the <ulink
|
|
|
|
|
url="http://personal.riverusers.com/~thegrendel/Change.log">change
|
|
|
|
|
log</ulink> for a revision history.</para>
|
|
|
|
@ -835,126 +844,6 @@ fi</programlisting>
|
|
|
|
|
<title>Basics</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<chapter id="exit-status">
|
|
|
|
|
<title>Exit and Exit Status</title>
|
|
|
|
|
|
|
|
|
|
<epigraph>
|
|
|
|
|
<attribution>Chet Ramey</attribution>
|
|
|
|
|
<para>...there are dark corners in the Bourne shell, and people use all
|
|
|
|
|
of them.</para>
|
|
|
|
|
</epigraph>
|
|
|
|
|
|
|
|
|
|
<para><anchor id="exitcommandref">The
|
|
|
|
|
<command>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>exit</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>command</primary>
|
|
|
|
|
<secondary>exit</secondary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
exit
|
|
|
|
|
</command>
|
|
|
|
|
command may be used to terminate a script, just as in a C program.
|
|
|
|
|
It can also return a value, which is available to the script's parent
|
|
|
|
|
process.</para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para><anchor id="exitstatusref">Every command returns an
|
|
|
|
|
<firstterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>exit status</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
exit status
|
|
|
|
|
</firstterm>
|
|
|
|
|
(sometimes referred to as a
|
|
|
|
|
<firstterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>return status</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
return status
|
|
|
|
|
</firstterm>). A successful command returns a
|
|
|
|
|
<returnvalue>0</returnvalue>, while an unsuccessful one returns
|
|
|
|
|
a <returnvalue>non-zero</returnvalue> value that usually may
|
|
|
|
|
be interpreted as an error code. Well-behaved UNIX commands,
|
|
|
|
|
programs, and utilities return a <returnvalue>0</returnvalue>
|
|
|
|
|
exit code upon successful completion, though there are some
|
|
|
|
|
exceptions.</para>
|
|
|
|
|
|
|
|
|
|
<para>Likewise, functions within a script and the script itself
|
|
|
|
|
return an exit status. The last command executed in the function
|
|
|
|
|
or script determines the exit status. Within a script, an
|
|
|
|
|
<userinput>exit <replaceable>nnn</replaceable></userinput>
|
|
|
|
|
command may be used to deliver an
|
|
|
|
|
<returnvalue><replaceable>nnn</replaceable></returnvalue> exit status
|
|
|
|
|
to the shell (<returnvalue><replaceable>nnn</replaceable></returnvalue>
|
|
|
|
|
must be a decimal number in the <returnvalue>0</returnvalue> -
|
|
|
|
|
<returnvalue>255</returnvalue> range).</para>
|
|
|
|
|
|
|
|
|
|
<note><para>When a script ends with an <command>exit</command> that has
|
|
|
|
|
no parameter, the exit status of the script is the exit status of
|
|
|
|
|
the last command executed in the script (<emphasis>not</emphasis>
|
|
|
|
|
counting the <command>exit</command>).</para></note>
|
|
|
|
|
|
|
|
|
|
<para><anchor id="exsref"></para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
<varname>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>$?</primary>
|
|
|
|
|
</indexterm> <indexterm>
|
|
|
|
|
<primary>variable</primary> <secondary>$?</secondary>
|
|
|
|
|
</indexterm> $?</varname> reads the exit status of the last
|
|
|
|
|
command executed. After a function returns,
|
|
|
|
|
<varname>$?</varname> gives the exit status of the last
|
|
|
|
|
command executed in the function. This is Bash's way of
|
|
|
|
|
giving functions a <quote>return value</quote>. After a
|
|
|
|
|
script terminates, a <varname>$?</varname> from the command
|
|
|
|
|
line gives the exit status of the script, that is, the last
|
|
|
|
|
command executed in the script, which is, by convention,
|
|
|
|
|
<userinput>0</userinput> on success or an integer in the
|
|
|
|
|
range 1 - 255 on error.</para>
|
|
|
|
|
|
|
|
|
|
<example id="ex5">
|
|
|
|
|
<title>exit / exit status</title>
|
|
|
|
|
<programlisting>&ex5;</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
<para><link linkend="xstatvarref">$?</link> is especially useful
|
|
|
|
|
for testing the result of a command in a script (see <xref
|
|
|
|
|
linkend="filecomp"> and <xref linkend="lookup">).</para>
|
|
|
|
|
|
|
|
|
|
<note>
|
|
|
|
|
<para>The <link linkend="notref">!</link>, the logical
|
|
|
|
|
<quote>not</quote> qualifier, reverses the outcome of a test or
|
|
|
|
|
command, and this affects its <link linkend="exitstatusref">exit
|
|
|
|
|
status</link>.
|
|
|
|
|
|
|
|
|
|
<example id="negcond">
|
|
|
|
|
<title>Negating a condition using <token>!</token></title>
|
|
|
|
|
<programlisting>true # the "true" builtin.
|
|
|
|
|
echo "exit status of \"true\" = $?" # 0
|
|
|
|
|
|
|
|
|
|
! true
|
|
|
|
|
echo "exit status of \"! true\" = $?" # 1
|
|
|
|
|
# Note that the "!" needs a space.
|
|
|
|
|
# !true leads to a "command not found" error
|
|
|
|
|
|
|
|
|
|
# Thanks, S.C.</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
</para>
|
|
|
|
|
</note>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<caution><para>Certain exit status codes have <link
|
|
|
|
|
linkend="exitcodesref">reserved meanings</link> and should not
|
|
|
|
|
be user-specified in a script. </para></caution>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</chapter> <!-- Exit and Exit status -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<chapter id="special-chars">
|
|
|
|
|
<title>Special Characters</title>
|
|
|
|
@ -3110,6 +2999,16 @@ arch=$(uname -m)</programlisting></para>
|
|
|
|
|
<programlisting>&ex17;</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
<para>The <emphasis>bracket notation</emphasis> for positional
|
|
|
|
|
parameters leads to a fairly simply way of referencing
|
|
|
|
|
the <emphasis>last</emphasis> argument passed to a
|
|
|
|
|
script on the command line. This also requires <link
|
|
|
|
|
linkend="varrefnew">indirect referencing</link>.</para>
|
|
|
|
|
|
|
|
|
|
<para><programlisting>args=$# # Number of args passed.
|
|
|
|
|
lastarg=${!args} # Note that lastarg=${!$#} doesn't work.</programlisting></para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para>Some scripts can perform different operations,
|
|
|
|
|
depending on which name they are invoked with. For this
|
|
|
|
|
to work, the script needs to check <varname>$0</varname>,
|
|
|
|
@ -3257,8 +3156,9 @@ fi
|
|
|
|
|
file2.txt:This is the First line of file2.txt.</computeroutput></screen>
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>Of course, <userinput>grep [Ff]irst *.txt</userinput>
|
|
|
|
|
would not work.</para>
|
|
|
|
|
<para>Note that the unquoted <userinput>grep [Ff]irst *.txt</userinput>
|
|
|
|
|
works under the Bash shell, but <emphasis>not</emphasis> under
|
|
|
|
|
<command>tcsh</command>.</para>
|
|
|
|
|
</note>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -3682,6 +3582,125 @@ bar' # Escape character \ taken literally because of strong quoting.
|
|
|
|
|
</chapter> <!-- Quoting -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<chapter id="exit-status">
|
|
|
|
|
<title>Exit and Exit Status</title>
|
|
|
|
|
|
|
|
|
|
<epigraph>
|
|
|
|
|
<attribution>Chet Ramey</attribution>
|
|
|
|
|
<para>...there are dark corners in the Bourne shell, and people use all
|
|
|
|
|
of them.</para>
|
|
|
|
|
</epigraph>
|
|
|
|
|
|
|
|
|
|
<para><anchor id="exitcommandref">The
|
|
|
|
|
<command>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>exit</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>command</primary>
|
|
|
|
|
<secondary>exit</secondary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
exit
|
|
|
|
|
</command>
|
|
|
|
|
command may be used to terminate a script, just as in a C program.
|
|
|
|
|
It can also return a value, which is available to the script's parent
|
|
|
|
|
process.</para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para><anchor id="exitstatusref">Every command returns an
|
|
|
|
|
<firstterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>exit status</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
exit status
|
|
|
|
|
</firstterm>
|
|
|
|
|
(sometimes referred to as a
|
|
|
|
|
<firstterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>return status</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
return status
|
|
|
|
|
</firstterm>). A successful command returns a
|
|
|
|
|
<returnvalue>0</returnvalue>, while an unsuccessful one returns
|
|
|
|
|
a <returnvalue>non-zero</returnvalue> value that usually may
|
|
|
|
|
be interpreted as an error code. Well-behaved UNIX commands,
|
|
|
|
|
programs, and utilities return a <returnvalue>0</returnvalue>
|
|
|
|
|
exit code upon successful completion, though there are some
|
|
|
|
|
exceptions.</para>
|
|
|
|
|
|
|
|
|
|
<para>Likewise, functions within a script and the script itself
|
|
|
|
|
return an exit status. The last command executed in the function
|
|
|
|
|
or script determines the exit status. Within a script, an
|
|
|
|
|
<userinput>exit <replaceable>nnn</replaceable></userinput>
|
|
|
|
|
command may be used to deliver an
|
|
|
|
|
<returnvalue><replaceable>nnn</replaceable></returnvalue> exit status
|
|
|
|
|
to the shell (<returnvalue><replaceable>nnn</replaceable></returnvalue>
|
|
|
|
|
must be a decimal number in the <returnvalue>0</returnvalue> -
|
|
|
|
|
<returnvalue>255</returnvalue> range).</para>
|
|
|
|
|
|
|
|
|
|
<note><para>When a script ends with an <command>exit</command> that has
|
|
|
|
|
no parameter, the exit status of the script is the exit status of
|
|
|
|
|
the last command executed in the script (<emphasis>not</emphasis>
|
|
|
|
|
counting the <command>exit</command>).</para></note>
|
|
|
|
|
|
|
|
|
|
<para><anchor id="exsref"></para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
<varname>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>$?</primary>
|
|
|
|
|
</indexterm> <indexterm>
|
|
|
|
|
<primary>variable</primary> <secondary>$?</secondary>
|
|
|
|
|
</indexterm> $?</varname> reads the exit status of the last
|
|
|
|
|
command executed. After a function returns,
|
|
|
|
|
<varname>$?</varname> gives the exit status of the last
|
|
|
|
|
command executed in the function. This is Bash's way of
|
|
|
|
|
giving functions a <quote>return value</quote>. After a
|
|
|
|
|
script terminates, a <varname>$?</varname> from the command
|
|
|
|
|
line gives the exit status of the script, that is, the last
|
|
|
|
|
command executed in the script, which is, by convention,
|
|
|
|
|
<userinput>0</userinput> on success or an integer in the
|
|
|
|
|
range 1 - 255 on error.</para>
|
|
|
|
|
|
|
|
|
|
<example id="ex5">
|
|
|
|
|
<title>exit / exit status</title>
|
|
|
|
|
<programlisting>&ex5;</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
<para><link linkend="xstatvarref">$?</link> is especially useful
|
|
|
|
|
for testing the result of a command in a script (see <xref
|
|
|
|
|
linkend="filecomp"> and <xref linkend="lookup">).</para>
|
|
|
|
|
|
|
|
|
|
<note>
|
|
|
|
|
<para>The <link linkend="notref">!</link>, the logical
|
|
|
|
|
<quote>not</quote> qualifier, reverses the outcome of a test or
|
|
|
|
|
command, and this affects its <link linkend="exitstatusref">exit
|
|
|
|
|
status</link>.
|
|
|
|
|
|
|
|
|
|
<example id="negcond">
|
|
|
|
|
<title>Negating a condition using <token>!</token></title>
|
|
|
|
|
<programlisting>true # the "true" builtin.
|
|
|
|
|
echo "exit status of \"true\" = $?" # 0
|
|
|
|
|
|
|
|
|
|
! true
|
|
|
|
|
echo "exit status of \"! true\" = $?" # 1
|
|
|
|
|
# Note that the "!" needs a space.
|
|
|
|
|
# !true leads to a "command not found" error
|
|
|
|
|
|
|
|
|
|
# Thanks, S.C.</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
</para>
|
|
|
|
|
</note>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<caution><para>Certain exit status codes have <link
|
|
|
|
|
linkend="exitcodesref">reserved meanings</link> and should not
|
|
|
|
|
be user-specified in a script. </para></caution>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</chapter> <!-- Exit and Exit status -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<chapter id="tests">
|
|
|
|
|
<title>Tests</title>
|
|
|
|
@ -7074,6 +7093,11 @@ filename=${1:-$DEFAULT_FILENAME}
|
|
|
|
|
<para>See also <xref linkend="ex58">, <xref
|
|
|
|
|
linkend="ex73">, and <xref linkend="collatz">.</para>
|
|
|
|
|
|
|
|
|
|
<para>Compare this method with <link
|
|
|
|
|
linkend="anddefault">using an <emphasis>and
|
|
|
|
|
list</emphasis> to supply a default command-line
|
|
|
|
|
argument</link>.</para>
|
|
|
|
|
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
@ -8591,6 +8615,16 @@ echo; echo "Keypress was "\"$keypress\""."
|
|
|
|
|
# Using these options is tricky, since they need to be in the correct order.</programlisting></para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para>The <option>-n</option> option to <command>read</command>
|
|
|
|
|
also allows detection of the <emphasis>arrow keys</emphasis>
|
|
|
|
|
and certain of the other unusual keys.</para>
|
|
|
|
|
|
|
|
|
|
<example id="arrowdetect">
|
|
|
|
|
<title>Detecting the arrow keys</title>
|
|
|
|
|
<programlisting>&arrowdetect;</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para>The <option>-t</option> option to <command>read</command>
|
|
|
|
|
permits timed input (see <xref linkend="tout">).</para>
|
|
|
|
|
|
|
|
|
@ -8962,9 +8996,30 @@ done</programlisting></para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<tip><para>It is possible to initialize and export
|
|
|
|
|
variables in the same operation, as in <command>export
|
|
|
|
|
var1=xxx</command>.</para></tip>
|
|
|
|
|
<tip>
|
|
|
|
|
|
|
|
|
|
<para>It is possible to initialize and export
|
|
|
|
|
variables in the same operation, as in <command>export
|
|
|
|
|
var1=xxx</command>.</para>
|
|
|
|
|
|
|
|
|
|
<para>However, as Greg Keraunen points out, in certain
|
|
|
|
|
situations this may have a different effect than
|
|
|
|
|
setting a variable, then exporting it.</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
<screen>
|
|
|
|
|
<prompt>bash$ </prompt><userinput>export var=(a b); echo ${var[0]}</userinput>
|
|
|
|
|
<computeroutput>(a b)</computeroutput>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<prompt>bash$ </prompt><userinput>var=(a b); export var; echo ${var[0]}</userinput>
|
|
|
|
|
<computeroutput>a</computeroutput>
|
|
|
|
|
</screen>
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</tip>
|
|
|
|
|
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
@ -12572,6 +12627,26 @@ tr -d 0-9 <filename
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><command>readlink</command></term>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>readlink</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>command</primary>
|
|
|
|
|
<secondary>link</secondary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>Disclose the file that a symbolic link points to.</para>
|
|
|
|
|
<para>
|
|
|
|
|
<screen><prompt>bash$ </prompt><userinput>readlink /usr/bin/awk</userinput>
|
|
|
|
|
<computeroutput>../../bin/gawk</computeroutput>
|
|
|
|
|
</screen>
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><command>strings</command></term>
|
|
|
|
|
<indexterm>
|
|
|
|
@ -14643,6 +14718,34 @@ echo -n "hello world" | dd cbs=1 conv=unblock 2> /dev/null
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><command>objdump</command></term>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>objdump</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>command</primary>
|
|
|
|
|
<secondary>object binary dump</secondary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>Displays an object file or binary executable in either
|
|
|
|
|
hexadecimal form or as a disassembled listing (with the
|
|
|
|
|
<option>-d</option> option).</para>
|
|
|
|
|
<para>
|
|
|
|
|
<screen><prompt>bash$ </prompt><userinput>objdump -d /bin/ls</userinput>
|
|
|
|
|
<computeroutput>/bin/ls: file format elf32-i386
|
|
|
|
|
|
|
|
|
|
Disassembly of section .init:
|
|
|
|
|
|
|
|
|
|
080490bc <.init>:
|
|
|
|
|
80490bc: 55 push %ebp
|
|
|
|
|
80490bd: 89 e5 mov %esp,%ebp
|
|
|
|
|
. . .</computeroutput>
|
|
|
|
|
</screen>
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><command>mcookie</command></term>
|
|
|
|
|
<indexterm>
|
|
|
|
@ -15644,6 +15747,35 @@ setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ</programlisting><
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><command>nmap</command></term>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>nmap</primary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<indexterm>
|
|
|
|
|
<primary>command</primary>
|
|
|
|
|
<secondary>port scan</secondary>
|
|
|
|
|
</indexterm>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>Network 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
|
|
|
|
|
a network against hacking attempts.</para>
|
|
|
|
|
<para><programlisting>#!/bin/bash
|
|
|
|
|
|
|
|
|
|
SERVER=$HOST # localhost.localdomain (127.0.0.1).
|
|
|
|
|
PORT_NUMBER=25 # SMTP port.
|
|
|
|
|
|
|
|
|
|
nmap $SERVER | grep -w "$PORT_NUMBER" # Is that particular port open?
|
|
|
|
|
# grep -w matches whole words only,
|
|
|
|
|
#+ so this wouldn't match port 1025, for example.
|
|
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
|
|
# 25/tcp open smtp</programlisting></para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><anchor id="freeref"><command>free</command></term>
|
|
|
|
|
<indexterm>
|
|
|
|
@ -18350,6 +18482,14 @@ exit 0</programlisting>
|
|
|
|
|
<part label="Part 4" id="part4">
|
|
|
|
|
<title>Advanced Topics</title>
|
|
|
|
|
|
|
|
|
|
<partintro>
|
|
|
|
|
<para>At this point, we are ready to delve into certain of the
|
|
|
|
|
difficult and unusual aspects of scripting. Along the way, we
|
|
|
|
|
will attempt to <quote>push the envelope</quote> in various
|
|
|
|
|
ways and examine <emphasis>boundary conditions</emphasis>
|
|
|
|
|
(what happens when we move into uncharted territory?).</para>
|
|
|
|
|
</partintro>
|
|
|
|
|
|
|
|
|
|
<chapter id="regexp">
|
|
|
|
|
<title>Regular Expressions</title>
|
|
|
|
|
|
|
|
|
@ -20054,6 +20194,16 @@ drwxr-xr-x 40 bozo bozo 2048 Feb 6 14:04 ..
|
|
|
|
|
<programlisting>&andlist2;</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
<para><anchor id="anddefault"></para>
|
|
|
|
|
<para>
|
|
|
|
|
Of course, an <emphasis>and list</emphasis> can also
|
|
|
|
|
<emphasis>set</emphasis> variables to a default value.
|
|
|
|
|
<programlisting>arg1=$@ # Set $arg1 to command line arguments, if any.
|
|
|
|
|
|
|
|
|
|
[ -z "$arg1" ] && arg1=DEFAULT
|
|
|
|
|
# Set to DEFAULT if not specified on command line.</programlisting>
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
@ -20534,6 +20684,13 @@ echo $element_count # 8</programlisting>
|
|
|
|
|
and <link linkend="uptimeref">uptime</link>
|
|
|
|
|
do this as well.</para></footnote></para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
<screen><prompt>bash$ </prompt><userinput>cat /proc/filesystems | grep iso9660</userinput>
|
|
|
|
|
<computeroutput> iso9660</computeroutput>
|
|
|
|
|
|
|
|
|
|
</screen>
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para><programlisting>kernel_version=$( awk '{ print $3 }' /proc/version )</programlisting></para>
|
|
|
|
|
|
|
|
|
@ -20728,6 +20885,9 @@ ln -s /dev/null ~/.netscape/cookies
|
|
|
|
|
Bash interpreter finally becomes aware of the error.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>Error messages may disregard comment lines in a script when
|
|
|
|
|
reporting the line number of a syntax error.</para>
|
|
|
|
|
|
|
|
|
|
<para>What if the script executes, but does not work as expected? This is the
|
|
|
|
|
all too familiar logic error.</para>
|
|
|
|
|
|
|
|
|
@ -21404,22 +21564,22 @@ exit 0</programlisting>
|
|
|
|
|
<programlisting>#!/bin/bash
|
|
|
|
|
|
|
|
|
|
#************************************************#
|
|
|
|
|
# xyz.sh
|
|
|
|
|
# written by Bozo Bozeman
|
|
|
|
|
# July 05, 2001
|
|
|
|
|
|
|
|
|
|
# Clean up project files.
|
|
|
|
|
# xyz.sh #
|
|
|
|
|
# written by Bozo Bozeman #
|
|
|
|
|
# July 05, 2001 #
|
|
|
|
|
# #
|
|
|
|
|
# Clean up project files. #
|
|
|
|
|
#************************************************#
|
|
|
|
|
|
|
|
|
|
BADDIR=65 # No such directory.
|
|
|
|
|
projectdir=/home/bozo/projects # Directory to clean up.
|
|
|
|
|
|
|
|
|
|
#-------------------------------------------------------#
|
|
|
|
|
# cleanup_pfiles ()
|
|
|
|
|
# Removes all files in designated directory.
|
|
|
|
|
# Parameter: $target_directory
|
|
|
|
|
# Returns: 0 on success, $BADDIR if something went wrong.
|
|
|
|
|
#-------------------------------------------------------#
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
# cleanup_pfiles () #
|
|
|
|
|
# Removes all files in designated directory. #
|
|
|
|
|
# Parameter: $target_directory #
|
|
|
|
|
# Returns: 0 on success, $BADDIR if something went wrong. #
|
|
|
|
|
# ------------------------------------------------------- #
|
|
|
|
|
cleanup_pfiles ()
|
|
|
|
|
{
|
|
|
|
|
if [ ! -d "$1" ] # Test if target directory exists.
|
|
|
|
@ -22375,6 +22535,30 @@ tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`</programlisting>
|
|
|
|
|
a <token>#</token>. See <xref linkend="commentblock">.</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>Running a script on a machine that relies on a command
|
|
|
|
|
that might not be installed is dangerous. Use <link
|
|
|
|
|
linkend="whatisref">whatis</link> to avoid potential problems
|
|
|
|
|
with this.</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
<programlisting>CMD=command1 # First choice.
|
|
|
|
|
PlanB=command2 # Fallback option.
|
|
|
|
|
|
|
|
|
|
command_test=$(whatis "$CMD" | grep 'nothing appropriate')
|
|
|
|
|
# If 'command1' not found on system , 'whatis' will return
|
|
|
|
|
#+ "command1: nothing appropriate."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if [[ -z "$command_test" ]] # Check whether command present.
|
|
|
|
|
then
|
|
|
|
|
$CMD option1 option2 # Run command1 with options.
|
|
|
|
|
else # Otherwise,
|
|
|
|
|
$PlanB #+ run command2.
|
|
|
|
|
fi</programlisting>
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>The <link linkend="runpartsref">run-parts</link>
|
|
|
|
|
command is handy for running a set of command
|
|
|
|
@ -22522,6 +22706,7 @@ tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`</programlisting>
|
|
|
|
|
<programlisting>&ex77;</programlisting>
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
|
|
<para><anchor id="varrefnew"></para>
|
|
|
|
|
<example id="ex78">
|
|
|
|
|
<title>Indirect variable references - the new way</title>
|
|
|
|
|
<programlisting>&ex78;</programlisting>
|
|
|
|
@ -22759,9 +22944,10 @@ tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`</programlisting>
|
|
|
|
|
|
|
|
|
|
<para>Others 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, and
|
|
|
|
|
David Lawyer (himself an author of 4 HOWTOs).</para>
|
|
|
|
|
Tony Richardson, Nick Drage (script ideas!), Rich Bartell, Jess
|
|
|
|
|
Thrysoee, Adam Lazur, Bram Moolenaar, Baris Cicek, Greg Keraunen,
|
|
|
|
|
Keith Matthews, Sandro Magi, and David Lawyer (himself an author
|
|
|
|
|
of 4 HOWTOs).</para>
|
|
|
|
|
|
|
|
|
|
<para>My gratitude to <ulink url="mailto:chet@po.cwru.edu">Chet
|
|
|
|
|
Ramey</ulink> and Brian Fox for writing <command>Bash</command>,
|
|
|
|
@ -23256,16 +23442,8 @@ tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`</programlisting>
|
|
|
|
|
<biblioentry>
|
|
|
|
|
<abstract>
|
|
|
|
|
<para>The <ulink
|
|
|
|
|
url="http://www.cornerstonemag.com/sed/sedfaq.html">sed
|
|
|
|
|
F.A.Q.</ulink></para>
|
|
|
|
|
</abstract>
|
|
|
|
|
</biblioentry>
|
|
|
|
|
|
|
|
|
|
<biblioentry>
|
|
|
|
|
<abstract>
|
|
|
|
|
<para>Carlos Duarte's instructive <ulink
|
|
|
|
|
url="http://www.dbnet.ece.ntua.gr/~george/sed/sedtut_1.html"><quote>Do
|
|
|
|
|
It With Sed</quote></ulink> tutorial.</para>
|
|
|
|
|
url="http://www.dbnet.ece.ntua.gr/~george/howto/sed/">sed F.A.Q. /
|
|
|
|
|
Do It With Sed</ulink>.</para>
|
|
|
|
|
</abstract>
|
|
|
|
|
</biblioentry>
|
|
|
|
|
|
|
|
|
@ -23652,6 +23830,19 @@ tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`</programlisting>
|
|
|
|
|
# Operates on the text contained in file $filename.
|
|
|
|
|
</programlisting></para>
|
|
|
|
|
|
|
|
|
|
<para>In certain cases, a <command>sed</command> editing command will
|
|
|
|
|
not work with single quotes.</para>
|
|
|
|
|
|
|
|
|
|
<para><programlisting>
|
|
|
|
|
filename=file1.txt
|
|
|
|
|
pattern=BEGIN
|
|
|
|
|
|
|
|
|
|
sed "/^$pattern/d" "$filename" # Works as specified.
|
|
|
|
|
# sed '/^$pattern/d' "$filename" has unexpected results.
|
|
|
|
|
# In this instance, with strong quoting (' ... '),
|
|
|
|
|
#+ "$pattern" will not expand to "BEGIN".</programlisting></para>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<note><para>Sed uses the <option>-e</option> option
|
|
|
|
|
to specify that the following string is an instruction or set
|
|
|
|
|
of instructions. If there is only a single instruction contained
|
|
|
|
@ -25091,6 +25282,18 @@ Smith,Tom,404 Polk Ave.,Los Angeles,CA,90003,(213) 879-5612
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><command>Mailing List</command></term>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>Using the <link linkend="commmail1">mail</link> command,
|
|
|
|
|
write a script that manages a simple mailing list. The
|
|
|
|
|
script automatically e-mails the monthly company newsletter,
|
|
|
|
|
read from a specified text file, and sends it to all the
|
|
|
|
|
addresses on the mailing list, which the script reads from
|
|
|
|
|
another specified file.</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
|
<term><command>Passwords</command></term>
|
|
|
|
|
<listitem>
|
|
|
|
|