This commit is contained in:
gferg 2002-09-30 15:56:52 +00:00
parent 44a61d53db
commit 36a0d2d3c1
8 changed files with 474 additions and 166 deletions

View File

@ -3,6 +3,80 @@ RELEASE HISTORY
Change log
Version 1.6 (minor update)
'POMEGRANATE' release
09/29/02
1) In "System and Administrative Commands" chapter:
Added "nmap" entry, with in-line usage example.
2) In "Communications Commands" section of "External Commands" chapter:
Revised "self-mailer.sh" example.
3) In "Miscellaneous Commands" section of "External Commands" Chapter:
Added "objdump" entry, with usage example.
4) In "File and Archiving Commands" section of "External Commands" Chapter:
Added "readlink" entry.
5) In "/dev and /proc" chapter:
Added example of extracting information from a /proc file.
6) Moved Chapter 3, "Exit and Exit Status" ahead 3 positions, to make it
Chapter 6. This makes the organization of the first part of book more
logical.
7) In "List Constructs" chapter:
Added discussion of using an "and list" to supply a default command-line
argument.
8) In "Quoting" chapter:
Rewrote "Of course, grep [Ff]irst *.txt would not work."
This does work under Bash, and fails under tcsh.
(Thanks, Simon Williams.)
Added example of hexadecimal character assignment to "escaped.sh" example.
(Thanks, Greg Keraunen.)
9) In "Special Variable Types" section of "Introduction to Variables and
Parameters" chapter:
Showed how to reference the last command-line parameter.
10) In "Internal Commands" chapter:
Added cautionary note about setting and exporting a variable in a single
operation (thanks, Greg Keraunen).
Added use of '-n' option to "read" to detect keypress of arrow keys,
with "arrow-detect.sh" example.
(thanks, Sandro Magi).
11) In "Assorted Tips" section of "Miscellany" chapter:
Added example of using "whatis" to test for an invoked command and do a
workaround if necessary.
12) In "Colorizing Scripts" section of "Miscellany" chapter:
Fixed typo in comment in "color-echo.sh" example.
13) In "Debugging" chapter:
Added note about syntax error messages possibly ignoring comment lines
when calculating the line number of the error. (Thanks, Keith Matthews)
14) Wrote a short introduction to "Part 4: Advanced Topics."
15) In "Writing Scripts" section of "Exercises" appendix:
In "Intermediate" section", added "Mailing List" exercise.
16) In the "Sed and Awk Micro-primer" appendix:
Added discussion and example of single-quoted 'sed' editing command not
working.
17) In "Bibliography" section:
Updated entry for "Sed F.A.Q." / "Do It With Sed."
18) Various minor fixups on example scripts.
Version 1.5 (major update)
'PAPAYA' release
07/13/02

View File

@ -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 &lt;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 &lt;.init&gt;:
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>

View File

@ -38,7 +38,7 @@ assert "$condition" $LINENO
# Some commands.
# ...
echo "You will never see this statement echo."
echo "This statement echoes only if the \"assert\" does not fail."
# ...
# Some more commands.

View File

@ -20,7 +20,7 @@ alias Reset="tput sgr0" # Reset text attributes to normal
cecho () # Color-echo.
# Argument $1 = message
# Argument $1 = color
# Argument $2 = color
{
local default_msg="No message passed."
# Doesn't really need to be a local variable.

View File

@ -3,18 +3,34 @@
echo; echo
echo "\v\v\v\v" # Prints \v\v\v\v
echo "\v\v\v\v" # Prints \v\v\v\v literally.
# Use the -e option with 'echo' to print escaped characters.
echo "============="
echo "VERTICAL TABS"
echo -e "\v\v\v\v" # Prints 4 vertical tabs.
echo "=============="
echo "QUOTATION MARK"
echo -e "\042" # Prints " (quote, octal ASCII character 42).
echo "=============="
# The $'\X' construct makes the -e option unnecessary.
echo; echo "NEWLINE AND BEEP"
echo $'\n' # Newline.
echo $'\a' # Alert (beep).
# Version 2 and later of Bash permits using the $'\xxx' construct.
echo "==============="
echo "QUOTATION MARKS"
# Version 2 and later of Bash permits using the $'\nnn' construct.
# Note that in this case, '\nnn' is an octal value.
echo $'\t \042 \t' # Quote (") framed by tabs.
# It also works with hexadecimal values, in an $'\xhhh' construct.
echo $'\t \x022 \t' # Quote (") framed by tabs.
# Thank you, Greg Keraunen, for pointing this out.
echo "==============="
echo
# Assigning ASCII characters to a variable.
# ----------------------------------------
@ -27,6 +43,8 @@ echo
triple_underline=$'\137\137\137' # 137 is octal ASCII code for '_'.
echo "$triple_underline UNDERLINE $triple_underline"
echo
ABC=$'\101\102\103\010' # 101, 102, 103 are octal A, B, C.
echo $ABC

View File

@ -2,6 +2,14 @@
# This is a simple script that removes blank lines from a file.
# No argument checking.
#
# You might wish to add something like:
# if [ -z "$1" ]
# then
# echo "Usage: `basename $0` target-file"
# exit 65
# fi
# Same as
# sed -e '/^$/d' filename

View File

@ -1,17 +1,19 @@
#!/bin/sh
# self-mailer.sh: Self-mailing script
ARGCOUNT=1 # Need name of addressee.
E_WRONGARGS=65
if [ $# -ne "$ARGCOUNT" ]
then
echo "Usage: `basename $0` addressee"
exit $E_WRONGARGS
fi
adr=${1:-`whoami`} # Default to current user, if not specified.
# Typing 'self-mailer.sh wiseguy@superdupergenius.com'
#+ sends this script to that addressee.
# Just 'self-mailer.sh' (no argument) sends the script
#+ to the person invoking it, for example, bozo@localhost.localdomain.
#
# For more on the ${parameter:-default} construct,
#+ see the "Parameter Substitution" section
#+ of the "Variables Revisited" chapter.
# ========================================================================
cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$1"
# ========================================================================
# ============================================================================
cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$adr"
# ============================================================================
# --------------------------------------------
# Greetings from the self-mailing script.
@ -21,4 +23,6 @@ cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$1"
#+ to do with their time.
# --------------------------------------------
echo "At `date`, script \"`basename $0`\" mailed to "$adr"."
exit 0

View File

@ -27,5 +27,6 @@ done | sort # Otherwise file list is unsorted.
# As Dominik 'Aeneas' Schnitzer points out,
#+ failing to quote $( find $directory -type l )
#+ will choke on filenames with embedded whitespace.
# Even this will only pick up the first field of each argument.
exit 0