This commit is contained in:
gferg 2014-03-07 02:24:29 +00:00
parent b80a906c84
commit a8dc10beab
21 changed files with 758 additions and 245 deletions

View File

@ -7,8 +7,8 @@
==================================================================
Current version = 6.6
Dated 11/27/12
Current version = 10
Dated 03/10/14
http://bash.deta.in/abs-guide-latest.tar.bz2
http://bash.deta.in/abs-guide.pdf
@ -16,25 +16,28 @@
Announcements
Version 6.6 released.
Version 10 released.
The ABS Guide no longer supports Sun Solaris and Apple versions
of UNIX, due to predatory corporate litigation policies.
If you're a Solaris and/or Apple person, then customer service
may be your friend, but the author of the ABS Guide is not.
The ABS Guide is now in the Public Domain.
The author will no longer support or update this document.
******************************************************************
It has come to the attention of the author that unauthorized
electronic and print editions of this book are being sold commercially
on itunes and elsewhere. These are illegal and pirated editions
produced without the author's permission, and readers of this book
are cautioned not to purchase them.
are advised not to purchase them. If you possess a printed copy of
this book, then be aware that its copyright is invalid, and that
therefore you have the right to copy or reproduce it.
******************************************************************
Honor roll of supporters.
The following persons have made unsolicited monetary contributions
to the ABS Guide author.
The following persons have made much welcomed, though unsolicited
monetary contributions to the ABS Guide author.
Matthias Gutfeldt
. . .
. . .
. . .
A big Thank You!
******************************************************************
@ -42,6 +45,63 @@
====================================================================
Version 10, PublicDomain release.
1) In "Miscellany" chapter:
In "Optimizations" section,
Quite a number of significant additions and examples, per Erik
Brandsberg.
(Thank you!)
In "Shell Wrappers" section,
Added "ex56py.sh" example of embedded Python script.
Added "speech.sh" example of a talking script.
2) In "Another Look At Variables" chapter
Made reference to $USERNAME more ambiguous, per note from Dominique
Brazziel
At "PS4" entry,
Added Erik Brandsberg's suggestion for embedding diagnostic info.
(Thank you!)
3) In "Loops" section of "Loops and Branches" chapter:
Added note concerning omitting do/done with curly-bracket
delimited command block.
(Thank you, YongYe!)
Added example of parameterized [list] in a for-loop.
4) In "External Commands" chapter:
At "diff" entry, added note about return code 2 on failed binary
file comparison.
At "dd" entry, added example script for preparing a bootable SD card
for the Raspberry Pi.
At "paste" entry, added short example.
Added entry for "xz" in File and Archiving Commands subsection.
5) In "Here Documents" chapter:
Fixup to multi-line variable example, to eliminate error message.
(Thank you, Ken B!)
6) Fixed some example scripts to remove quoting of matched variable with
=~ operator.
7) In "Parameter Substitution and Expansion" Table,
Made minor changes in because Bash updates changed the behavior of certain
parameter operators, and made not of same. Grrr!
8) In "History Commands" appendix:
Note about enabling the history mechanism within a script,
per comment by David (dhw).
9) In "Contributed Scripts" appendix:
Added "sedappend.sh" script example of inserting text in a file
using sed.
10) In the Bibliography:
Added an entry for Daniel Goldman's excellent ebook introduction to sed.
10) Fixed and/or updated some more scripts.
Version 6.6, Ytterbiumberry* release

View File

@ -840,7 +840,10 @@
<para><link linkend="arrayref">Arrays</link></para>
<itemizedlist>
<listitem><para><link linkend="assocarr">Associative arrays</link>
</para></listitem>
</para>
<para><link linkend="assocarrtst">more efficient</link>
than conventional arrays</para>
</listitem>
<listitem><para><link linkend="arrayref">Bracket notation</link>
</para></listitem>
<listitem><para><link linkend="arrayappend0">Concatenating</link>,
@ -1008,13 +1011,15 @@
<para><link linkend="codeblockref">Blocks of code</link>
<itemizedlist>
<listitem>
<para><link linkend="nododone">Iterating / looping</link></para>
</listitem>
<listitem>
<para><link
linkend="blockio">Redirection</link></para>
<para><link
linkend="blockio2"><firstterm>Script
example</firstterm></link>:
redirecting output of a a code block</para></listitem>
<para><firstterm>Script example</firstterm>:
<link linkend="blockio2">Redirecting output of a a code
block</link></para></listitem>
</itemizedlist></para>
<para><link linkend="bfs">Bootable flash drives</link>, creating</para>
@ -1254,6 +1259,10 @@
<listitem><para><link
linkend="ddrandom">Random access</link> on a data
stream</para></listitem>
<listitem><para><emphasis>Raspberry Pi</emphasis>,
<link linkend="rpsdcard01">script for preparing a bootable SD
card</link>
</para></listitem>
<listitem><para><link
linkend="ddswap">Swapfiles</link>, initializing</para></listitem>
<listitem><para><link linkend="ddlink">Thread on
@ -1675,6 +1684,8 @@
<listitem><para><link
linkend="bashpodder">Podcasting</link></para></listitem>
<listitem><para><link linkend="poem">Poem</link></para></listitem>
<listitem><para><link linkend="speech00">Speech
generation</link></para></listitem>
<listitem><para><link
linkend="hanoi">Towers of Hanoi</link></para>
<para><link linkend="hanoi2">Graphic
@ -2659,6 +2670,16 @@
</itemizedlist></para>
<!-- *************************************************************** -->
<para>Raspberry Pi (single-board computer)
<itemizedlist>
<listitem><para><link
linkend="rpsdcard01">Script for preparing a bootable SD card</link>
</para></listitem>
</itemizedlist></para>
<!-- *************************************************************** -->
@ -3297,7 +3318,10 @@
<listitem><para>Array, <link linkend="retarray">as
<firstterm>return value</firstterm> from
a function</link></para></listitem>
a function</link></para>
<para><firstterm>Associative</firstterm> array
<link linkend="assocarrtst">more efficient</link>
than a numerically-indexed array</para></listitem>
<listitem><para><link linkend="captureretval">Capturing
the return value</link> of a function, using
@ -3375,6 +3399,9 @@
linkend="setvaremb">Setting script variable</link>
to a block of embedded <firstterm>sed</firstterm> or
<firstterm>awk</firstterm> code</para></listitem>
<listitem><para>Speeding up script execution by <link
linkend="lcall">disabling <firstterm>unicode</firstterm></link>
</para></listitem>
<listitem><para>Subshell variable,
<link linkend="subshtmp">accessing outside the subshell</link>
</para></listitem>
@ -3451,7 +3478,14 @@
<para><link linkend="unameref">uname</link>,
output system information</para>
<para><link linkend="unicoderef">Unicode</link>, encoding standard
for representing letters and symbols</para>
for representing letters and symbols
<itemizedlist>
<listitem><para><link
linkend="lcall">Disabling <firstterm>unicode</firstterm></link>
to optimize script</para></listitem>
</itemizedlist></para>
<para><link linkend="uninitvar">Uninitialized variables</link> </para>
<para><link linkend="uniqref">uniq</link>,
filter to remove duplicate lines from a sorted file</para>

View File

@ -127,5 +127,6 @@ here-commsub.sh
UseGetOpt.sh: line 4 (comment)
UseGetOpt-2.sh: line 11 (comment)
bash64.sh
line 4 (comment)
bash64.sh: line 4 (comment)
speech.sh: line 14

File diff suppressed because it is too large Load Diff

View File

@ -73,3 +73,6 @@ echo ${arr[@]} # 11 12 13 14 15 16 17 18 19 20
echo
exit 0
# Nathan Coulter points out that passing arrays with elements containing
#+ whitespace breaks this example.

View File

@ -76,9 +76,7 @@ seed_random () # Seed random number generator.
seed=`eval date +%s`
let "seed %= 32766"
RANDOM=$seed
# What are some other methods
#+ of seeding the random number generator?
}
} # Consider other methods of seeding the random number generator.
deal_cards ()
{
@ -95,7 +93,7 @@ do
parse_card $t
u=$cards_picked+1
# Change back to 1-based indexing (temporarily). Why?
# Change back to 1-based indexing, temporarily. Why?
let "u %= $CARDS_IN_SUIT"
if [ "$u" -eq 0 ] # Nested if/then condition test.
then

View File

@ -19,6 +19,6 @@ done`
echo "variable2 = $variable2" # variable2 = 0123456789
# Demonstrates that it's possible to embed a loop
#+ within a variable declaration.
#+ inside a variable declaration.
exit 0

View File

@ -37,3 +37,23 @@ exit 0
# As it stands, this script converts *all* the files in the current
#+ working directory.
# Modify it to work *only* on files with a ".mac" suffix.
# *** And here's another way to do it. *** #
#!/bin/bash
# Batch convert into different graphic formats.
# Assumes imagemagick installed (standard in most Linux distros).
INFMT=png # Can be tif, jpg, gif, etc.
OUTFMT=pdf # Can be tif, jpg, gif, pdf, etc.
for pic in *"$INFMT"
do
p2=$(ls "$pic" | sed -e s/\.$INFMT//)
# echo $p2
convert "$pic" $p2.$OUTFMT
done
exit $?

View File

@ -12,7 +12,7 @@
# Input error in dialog box.
E_INPUT=65
E_INPUT=85
# Dimensions of display, input widgets.
HEIGHT=50
WIDTH=60

View File

@ -12,7 +12,7 @@ echo $a
# Note that including an exclamation mark (!) within a
#+ command substitution construct will not work from the command-line,
#+ since this triggers the Bash "history mechanism."
# Inside a script, however, the history functions are disabled.
# Inside a script, however, the history functions are disabled by default.
a=`ls -l` # Assigns result of 'ls -l' command to 'a'
echo $a # Unquoted, however, it removes tabs and newlines.

View File

@ -5,11 +5,13 @@
ARGCOUNT=1 # Expect one arg.
E_WRONGARGS=65
OFF=0 # Flag states.
ON=1
E_WRONGARGS=85
file="$1" # Target filename.
lineno=1 # Line number. Start at 1.
Flag=0 # Blank line flag.
Flag=$OFF # Blank line flag.
if [ $# -ne "$ARGCOUNT" ]
then
@ -22,18 +24,18 @@ file_read () # Scan file for pattern, then print line.
while read line
do
if [[ "$line" =~ ^[a-z] && $Flag -eq 1 ]]
then # Line begins with lc character, following blank line.
if [[ "$line" =~ ^[a-z] && $Flag -eq $ON ]]
then # Line begins with lowercase character, following blank line.
echo -n "$lineno:: "
echo "$line"
fi
if [[ "$line" =~ "^$" ]]
then # If blank line,
Flag=1 #+ set flag.
if [[ "$line" =~ ^$ ]]
then # If blank line,
Flag=$ON #+ set flag.
else
Flag=0
Flag=$OFF
fi
((lineno++))

View File

@ -43,7 +43,7 @@ else
echo "Problem in creating file: \"$OUTFILE\""
fi
# This method can also be used for generating
# This method also works for generating
#+ C programs, Perl programs, Python programs, Makefiles,
#+ and the like.

View File

@ -25,7 +25,7 @@ mbox_grep() # Parse mailbox file.
# Otherwise "read" will strip leading & trailing space from its input.
do
if [[ $mail =~ "^From " ]] # Match "From" field in message.
if [[ $mail =~ ^From ]] # Match "From" field in message.
then
(( body = 0 )) # "Zero out" variables.
(( match = 0 ))

View File

@ -12,7 +12,7 @@
# of integers in which all the rows, columns,
# and *long* diagonals add up to the same number.
# Being "square," the array has the same number
# of rows and columns.
# of rows and columns. That number is the "order."
# An example of a magic square of order 3 is:
# 8 1 6
# 3 5 7
@ -29,8 +29,8 @@ declare -i square
usage_message ()
{
echo "Usage: $0 square-size"
echo " ... where \"square-size\" is an ODD integer"
echo "Usage: $0 order"
echo " ... where \"order\" (square size) is an ODD integer"
echo " in the range 3 - 31."
# Actually works for squares up to order 159,
#+ but large squares will not display pretty-printed in a term window.
@ -57,7 +57,7 @@ calculate () # Here's where the actual work gets done.
square[$index]=cell_val; ((cell_val++))
done
done
} # Plain math, no visualization required.
} # Plain math, visualization not required.
print_square () # Output square, one row at a time.
@ -72,7 +72,7 @@ print_square () # Output square, one row at a time.
do
let "idx = $row * $dimension + $col"
printf "%3d " "${square[idx]}"; echo -n " "
done # Displays up to 13-order neatly in 80-column term window.
done # Displays up to 13th order neatly in 80-column term window.
echo # Newline after each row.
done

View File

@ -12,6 +12,9 @@ echo && echo # Two line-feeds in an "and list."
stty -echo # Turns off screen echo.
# May also be done with
# read -sp passwd
# A big Thank You to Leigh James for pointing this out.
echo -n "Enter password again "
read passwd

View File

@ -1,34 +1,43 @@
#!/bin/bash
# seeding-random.sh: Seeding the RANDOM variable.
# v 1.1, reldate 09 Feb 2013
MAXCOUNT=25 # How many numbers to generate.
SEED=
random_numbers ()
{
count=0
local count=0
local number
while [ "$count" -lt "$MAXCOUNT" ]
do
number=$RANDOM
echo -n "$number "
let "count += 1"
let "count++"
done
}
echo; echo
RANDOM=1 # Setting RANDOM seeds the random number generator.
SEED=1
RANDOM=$SEED # Setting RANDOM seeds the random number generator.
echo "Random seed = $SEED"
random_numbers
echo; echo "Trying again with same random seed ..."
RANDOM=1 # Same seed for RANDOM . . .
RANDOM=$SEED # Same seed for RANDOM . . .
echo; echo "Again, with same random seed ..."
echo "Random seed = $SEED"
random_numbers # . . . reproduces the exact same number series.
#
# When is it useful to duplicate a "random" series?
echo; echo
RANDOM=2 # Trying again, but with a different seed . . .
SEED=2
RANDOM=$SEED # Trying again, but with a different seed . . .
echo "Random seed = $SEED"
random_numbers # . . . gives a different number series.
echo; echo
@ -37,12 +46,14 @@ echo; echo
# It is also possible to seed RANDOM from 'time' or 'date' commands.
# Getting fancy...
SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }')
SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }'| sed s/^0*//)
# Pseudo-random output fetched
#+ from /dev/urandom (system pseudo-random device-file),
#+ then converted to line of printable (octal) numbers by "od",
#+ finally "awk" retrieves just one number for SEED.
#+ then "awk" retrieves just one number for SEED,
#+ finally "sed" removes any leading zeros.
RANDOM=$SEED
echo "Random seed = $SEED"
random_numbers
echo; echo

View File

@ -79,7 +79,7 @@ process_text ()
continue # Skip the underscore test.
else # Otherwise . . .
if [[ "$line" =~ "\[*jpg\]" ]] # Is a graphic?
if [[ "$line" =~ \[*jpg\] ]] # Is a graphic?
then # Strip away brackets.
temp=$( echo "$line" | sed -e 's/\[//' -e 's/\]//' )
line=""$CENTER" &lt;img src="\"$IMGDIR"/$temp\"&gt; "$END_CENTER" "

View File

@ -10,7 +10,7 @@ echo " Just initialized \$variable to $variable in line number $line."
let "variable *= 3"; line=$LINENO
echo " Just multiplied \$variable by 3 in line number $line."
exit
exit 0
# The "trap 'command1 . . . command2 . . .' DEBUG" construct is
#+ more appropriate in the context of a complex script,

View File

@ -42,5 +42,9 @@ echo "\$var1 = "$var1"" # $var1 = Two bits
# Or, as Chris Hiestand points out ...
if [[ "$(du "$My_File1")" -gt "$(du "$My_File2")" ]]; then ...
if [[ "$(du "$My_File1")" -gt "$(du "$My_File2")" ]]
# ^ ^ ^ ^ ^ ^ ^ ^
then
...
fi
# ************************************************************ #

View File

@ -27,7 +27,7 @@ then
fi
if [[ "$1" =~ "[a-zA-Z][a-zA-Z]$" ]] # Ends in two alpha chars?
if [[ "$1" =~ [a-zA-Z][a-zA-Z]$ ]] # Ends in two alpha chars?
then # It's a domain name &&
#+ must do host lookup.
IPADDR=$(host -W $HOSTWAIT $1 | awk '{print $4}')
@ -57,21 +57,21 @@ then
exit $E_NOHOST # Bail out.
fi
if [[ "$IPADDR" =~ "^[;;]" ]]
if [[ "$IPADDR" =~ ^[;;] ]]
# ;; Connection timed out; no servers could be reached.
then
echo "Host lookup timed out!"
exit $E_TIMEOUT # Bail out.
fi
if [[ "$IPADDR" =~ "[(NXDOMAIN)]$" ]]
if [[ "$IPADDR" =~ [(NXDOMAIN)]$ ]]
# Host xxxxxxxxx.xxx not found: 3(NXDOMAIN)
then
echo "Host not found!"
exit $E_NOHOST # Bail out.
fi
if [[ "$IPADDR" =~ "[(SERVFAIL)]$" ]]
if [[ "$IPADDR" =~ [(SERVFAIL)]$ ]]
# Host xxxxxxxxx.xxx not found: 2(SERVFAIL)
then
echo "Host not found!"

View File

@ -1,20 +1,25 @@
making
nogood
syzygy
nogood
filesystem
alltold
orangutan
doing
nekulturno
madeupword
gargantuan
tasteless
coredump
nicrosoft
hazmat
ziggurat
what
haystack
abracadbra
advanced
bash
scripting
guide
lowercase
bremsstrahlung
abcdef
duplicity
kitsch
avoirdupois
radicand
end