Appendice A. Script aggiuntivi

Questi script, sebbene non siano stati inseriti nel testo del documento, illustrano alcune interessanti tecniche di programmazione di shell. Sono anche utili. Ci si diverta ad analizzarli e a eseguirli.

Esempio A-1. mailformat: impaginare un messaggio e-mail

#!/bin/bash
# mail-format.sh (ver. 1.1): formatta messaggi e-mail.

#  Si sbarazza di accenti circonflessi, tabulazioni 
#+ e suddivide anche le righe eccessivamente lunghe.

# =====================================================================
#       Verifica standard del(gli) argomento(i) dello script
ARG=1
E_ERR_ARG=65
E_NOFILE=66

if [ $# -ne $ARG ]   # Numero corretto di argomenti passati allo script?
then
  echo "Utilizzo: `basename $0` nomefile"
  exit $E_ERR_ARG
fi

if [ -f "$1" ]       # Verifica l'esistenza del file.
then
    nome_file=$1
else
    echo "Il file \"$1\" non esiste."
    exit $E_NOFILE
fi
# =========================================================================

LUNG_MAX=70          # Dimensione massima delle righe.

# -------------------------------------------
# Una variabile può contenere uno script sed.
scriptsed='s/^>//
s/^  *>//
s/^  *//
s/		*//'
# -------------------------------------------

#  Cancella gli accenti circonflessi e le tabulazioni presenti 
#+ all'inizio delle righe, quindi dimensiona le righe a $LUNG_MAX caratteri.
sed "$scriptsed" $1 | fold -s --width=$LUNG_MAX
                         #  -s opzione di "fold" per suddividere le righe 
                         #+ in corrispondenza di uno spazio, se possibile.	  


#  Questo script è stato ispirato da un articolo, apparso su una notissima
#+ rivista, che magnificava una utility MS Windows di 164K avente una 
#+ funzionalità simile.
#
#  Una buona serie di utility per l'elaborazione del testo e un linguaggio
#+ di scripting efficiente sono una valida alternativa ad eseguibili obesi. 

exit 0

Esempio A-2. rn: una semplice utility per rinominare un file

Questo script è una modifica di Esempio 15-19.

#! /bin/bash
#
# Semplicissima utility per "rinominare" i file (basata su "lowercase.sh").
#
#  L'utility "ren", di Vladimir Lanin (lanin@csd2.nyu.edu),
#+ svolge meglio lo stesso compito.


ARG=2
E_ERR_ARG=65
UNO=1                     #  Per il corretto uso di singolare/plurale 
                          #+ (vedi oltre).

if [ $# -ne "$ARG" ]
then
  echo "Utilizzo: `basename $0` vecchio-modello nuovo-modello"
  #  Come "rn gif jpg", che rinomina tutti i file gif della directory 
  #+ di lavoro in jpg.
  exit $E_ERR_ARG
fi

numero=0                  # Per contare quanti file sono stati rinominati.


for nomefile in *$1*      #  Scorre i file corrispondenti presenti 
                          #+ nella directory.
do
   if [ -f "$nomefile" ]  # Se trova corrispondenza...
   then
     fnome=`basename $nomefile`            #  Elimina il percorso.
     n=`echo $fnome | sed -e "s/$1/$2/"`   #  Sostituisce il vecchio nome 
                                           #+ col nuovo.
     mv $fnome $n                          #  Rinomina.
     let "numero += 1"
   fi
done   

if [ "$numero" -eq "$UNO" ]                # Per una corretta grammatica.
then
 echo "$numero file rinominato."
else 
 echo "$numero file rinominati."
fi 

exit 0


# Esercizi:
# --------
# Quali sono i file sui quali lo script non agisce?
# Come può essere risolto il problema?
#
#  Riscrivete lo script in modo che elabori tutti i file della directory
#+ nei cui nomi sono presenti degli spazi, e li rinomini sostituendo
#+ ogni spazio con un trattino di sottolineatura (underscore).

Esempio A-3. blank-rename: rinomina file i cui nomi contengono spazi

È una versione ancor più semplice dello script precedente

#! /bin/bash
# blank-rename.sh
#
#  Sostituisce gli spazi nei nomi dei file presenti nella directory 
#+ con degli underscore.

UNO=1                     #  Per gestire correttamente il singolare/plurale
                          #+ (vedi oltre).
numero=0                  #  Per contare i file effettivamente rinominati.
TROVATO=0                 #  Valore di ritorno in caso di successo.

for nomefile in *         # Scorre i file della directory.
do
     echo "$nomefile" | grep -q " "         #  Controlla se il nome del file
     if [ $? -eq $TROVATO ]                 #+ contiene uno/degli spazio(i).
     then
       fnome=$nomefile                      #  Sì, nomefile
                                            #+ ha bisogno di una sistemata.
       n=`echo $fnome | sed -e "s/ /_/g"`   #  Sostituisce lo spazio 
                                            #+ con un underscore.
       mv "$fnome" "$n"                     #  Rinomina.
       let "numero += 1"
     fi
done   

if [ "$numero" -eq "$UNO" ]                 # Per una corretta grammatica.
then
 echo "$numero file rinominato."
else 
 echo "$numero file rinominati."
fi 

exit 0

Esempio A-4. encryptedpw: upload a un sito ftp utilizzando una password criptata localmente

#!/bin/bash

# Esempio "ex72.sh" modificato per l'uso di una password criptata.

#  Va notato che resta ancora piuttosto insicuro,
#+ dal momento che la password decodificata viene inviata in chiaro.
#  Se necessario usate un programma come "ssh".

E_ERR_ARG=65

if [ -z "$1" ]
then
  echo "Utilizzo: `basename $0` nomefile"
  exit $E_ERR_ARG
fi  

Nomeutente=ecobel       # Va impostata al nome utente.
pword=/home/ecobel/secret/password_encrypted.file
# File contenente la password cifrata.

Nomefile=`basename $1`  # Elimina il percorso dal nome del file

Server="XXX"            #  Impostatele con i nomi effettivi del server
Directory="YYY"         #+ & della directory.


Password=`cruft <$pword`          # Decodifica la password.
#  Viene usato il programma di cifratura di file "cruft" dell'autore del libro,
#+ basato sull'algoritmo classico "onetime pad",
#+ e ottenibile da:
#+ Sito-principale:  ftp://ibiblio.org/pub/Linux/utils/file
#+                   cruft-0.2.tar.gz [16k]


ftp -n $Server <<Fine-Sessione
user $Nomeutente $Password
binary
bell
cd $Directory
put $Nomefile
bye
Fine-Sessione
#  L'opzione -n di "ftp" disabilita l'auto-logon.
#  Notate che il comando "bell" fa emettere un segnale acustico dopo ogni
#+ trasferimento di file.

exit 0

Esempio A-5. copy-cd: copiare un CD di dati

#!/bin/bash
# copy-cd.sh: copiare un CD dati

CDROM=/dev/cdrom                           # Dispositivo CD ROM
FO=/home/bozo/projects/cdimage.iso         # File di output
#       /xxxx/xxxxxxx/                        Da modificare secondo le
                                           #+ proprie impostazioni.
DIMBLOCCO=2048
VELOC=2                                    #  Se supportata, si può usare una
                                           #+ velocità superiore.
DISPOSITIVO=cdrom
# DISPOSITIVO="0,0"    per le vecchie versioni di cdrecord.

echo; echo "Inserite il CD sorgente, ma *senza* montare il dispositivo."
echo "Fatto questo, premete INVIO. "
read pronto                                 #  Attende l'input, 
                                            #+ $pronto non va bene.

echo; echo "Copia del CD sorgente in $FO."
echo "Occorre un po' di tempo. Pazientate, prego."

dd if=$CDROM of=$FO bs=$DIMBLOCCO           # Coppia grezza del dispositivo.


echo; echo "Rimuovete il CD dati."
echo "Inserite un CDR vergine."
echo "Quando siete pronti premete INVIO. "
read pronto                                  #  Attende l'input, 
                                             #+ $pronto non va bene.

echo "Copia di $FO sul CDR."

cdrecord -v -isosize speed=$VELOC dev=$DISPOSITIVO $FO
# Viene usato il programma "cdrecord" di Joerg Schilling (vedi documentazione).
# http://www.fokus.gmd.de/nthp/employees/schilling/cdrecord.html


echo; echo "Eseguita copia di $FO sul CDR del dispositivo $CDROM."

echo "Volete cancellare il file immagine (s/n)? "  #  Probabilmente un file di
                                                   #+ dimensioni notevoli.
read risposta

case "$risposta" in
[sS]) rm -f $FO
      echo "$FO cancellato."
      ;;
*)    echo "$FO non cancellato.";;
esac

echo

#  Esercizio:
#  Modificate il precedente enunciato "case" in modo che accetti come input
#+ anche "si" e "Si".

exit 0

Esempio A-6. Serie di Collatz

#!/bin/bash
# collatz.sh

#  Si tratta della notoria "hailstone" o serie di Collatz.
#  ------------------------------------------------------
#  1) Il "seme" è un intero ottenuto come argomento da riga di comando.
#  2) NUMERO <--- seme
#  3) Visualizza NUMERO.
#  4)  Se NUMERO è pari, lo divide per 2,
#  5)+ se dispari, lo moltiplica per 3 e aggiunge 1.
#  6) NUMERO <--- risultato 
#  7) Ripete il ciclo da punto 3 (per il numero di iterazioni specificato).
#
#  La teoria dice che ogni sequenza,
#+ non importa quanto grande sia il valore iniziale,
#+ alla fine si riduce a ripetizioni di cicli "4,2,1...",
#+ anche dopo notevoli fluttuazioni attraverso un'ampia serie di valori.
#
#  È un esempio di "iterazione",
#+ un'operazione che reimpiega i propri output come input.
#  Talvolta il risultato è una serie "caotica".


MAX_ITERAZIONI=200
# Per grandi numeri (>32000), aumentate MAX_ITERAZIONI.

h=${1:-$$}                      #  Seme
                                #  Usa come seme $PID, se non viene specificato
                                #+ come argomento da riga di comando.

echo
echo "C($h) --- $MAX_ITERAZIONI Iterazioni"
echo

for ((i=1; i<=MAX_ITERAZIONI; i++))
do

echo -n "$h     "
#          ^^^^^
#          tabulazione

  let "resto = h % 2"
  if [ "$resto" -eq 0 ]         # Pari?
  then
    let "h /= 2"                # Divide per 2.
  else
    let "h = h*3 + 1"           # Moltiplica per 3 e aggiunge 1.
  fi


COLONNE=10                      # Visualizza 10 numeri per riga.
let "interruzione_riga = i % $COLONNE"
if [ "$interruzione_riga" -eq 0 ]
then
  echo
fi  

done

echo

#  Per ulteriori informazioni su questa funzione matematica,
#+ vedi _Computers, Pattern, Chaos, and Beauty_, by Pickover, p. 185 ff.,
#+ presente in bibliografia.

exit 0

Esempio A-7. days-between: calcolo del numero di giorni intercorrenti tra due date

#!/bin/bash
# days-between.sh:    Numero di giorni intercorrenti tra due date.
# Utilizzo: ./days-between.sh [M]M/[G]G/AAAA [M]M/[G]G/AAAA
#
# Nota: Script modificato per tener conto dei cambiamenti avvenuti 
#+      in Bash v. 2.05b +, che hanno chiuso una falla che consentiva la
#+      restituzione di grandi interi negativi.

ARG=2                 # Sono attesi due argomenti da riga di comando.
E_ERR_PARAM=65        # Errore di parametro.

ANNORIF=1600          # Anno di riferimento.
SECOLO=100
GPA=365
AGG_GPA=367           # Aggiustamento per anno bisestile e frazione.
MPA=12
GPM=31
FREQ_BISESTILE=4

MAXVALRIT=255         #  Massimo intero positivo consentito
                      #+ come valore di ritorno di una funzione.

diff=                 # Variabile globale per la differenza delle date.
valore=               # Variabile globale per il valore assoluto.
giorno=               # Variabili globali per giorno, mese, anno.
mese=
anno=


Errore_Param ()       # Parametri da riga di comando errati.
{
  echo "Utilizzo: `basename $0` [M]M/[G]G/AAAA [M]M/[G]G/AAAA"
  echo "          (la data deve essere successiva al 1/3/1600)"
  exit $E_ERR_PARAM
}  


Analizza_Data ()              #  Analizza la data passata come parametro 
                              #+ da riga di comando.
{
  mese=${1%%/**}
  gm=${1%/**}                 # Giorno e mese.
  giorno=${gm#*/}
  let "anno = `basename $1`"  #  Non è un nome di un file, 
                              #+ ma funziona ugualmente.
}  


controlla_data ()             #  Controlla la validità della(e) data(e) 
                              #+ passate(e).
{
  [ "$giorno" -gt "$GPM" ] || [ "$mese" -gt "$MPA" ] ||
  [ "$anno" -lt "$ANNORIF" ] && Errore_Param
  # Esce dallo script se i valori sono errati.
  # Impiego di lista-or / lista-and.
  #
  # Esercizio: implementate una verifica più rigorosa.
}


toglie_zero_iniziale () #  Toglie i possibili zeri iniziali
{                       #+ dal giorno e dal mese
  return ${1#0}         #+ altrimenti Bash li interpreta
}                       #+ come valori ottali (POSIX.2, sez. 2.9.2.1).


tot_giorni ()           #  Formula di Gauss:
{                       #  Giorni intercorrenti dal 1 marzo 1600 
                        #                           ^^^^^^^^^^^^
                        #+ alla data passata come parametro.

  giorno=$1
  mese=$2
  anno=$3

  let "mese = $mese - 2"
  if [ "$mese" -le 0 ]
  then
    let "mese += 12"
    let "anno -= 1"
  fi  

  let "anno -= $ANNORIF"
  let "totanni = $anno / $SECOLO"


  let "Giorni = $GPA*$anno + $anno/$FREQ_BISESTILE - $totanni \
   + $totanni/$FREQ_BISESTILE + $AGG_GPA*$mese/$MPA + $giorno - $GPM"
  #  Per un'approfondita spiegazione di questo algoritmo, vedi
  #+ http://weblogs.asp.net/pgreborio/archive/2005/01/06/347968.aspx


  echo $Giorni

}  


calcola_differenza ()              # Differenza tra i totali dei giorni.
{
  let "diff = $1 - $2"             # Variabile globale.
}  


abs ()                             #  Valore assoluto
{                                  #  Usa la variabile globale "valore".
  if [ "$1" -lt 0 ]                #  Se nagativa
  then                             #+ allora
    let "valore = 0 - $1"          #+ cambia il segno,
  else                             #+ altrimenti
    let "valore = $1"              #+ la lascia invariata.
  fi
}



if [ $# -ne "$ARG" ]               # Richiede due parametri da riga di comando.
then
  Errore_Param
fi  

Analizza_Data $1
controlla_data $giorno $mese $anno #  Verifica se la data è valida.

toglie_zero_iniziale $giorno       #  Rimuove gli zeri iniziali
giorno=$?                          #+ dal giorno e/o dal mese.
toglie_zero_iniziale $mese
mese=$?

let "data1 = `tot_giorni $giorno $mese $anno`"


Analizza_Data $2
controlla_data $giorno $mese $anno

toglie_zero_iniziale $giorno
giorno=$?
toglie_zero_iniziale $mese
mese=$?

data2=$(tot_giorni $giorno $mese $anno) # Sostituzione di comando.


calcola_differenza $data1 $data2

abs $diff                               # Si accerta che sia positiva.
diff=$valore

echo $diff

exit 0

#  Confrontate questo script con
#+ l'implementazione della Formula di Gauss in un programma in C presso:
#+    http://buschencrew.hypermart.net/software/datedif

Esempio A-8. Creare un "dizionario"

#!/bin/bash
# makedict.sh  [crea un dizionario]

# Modifica dello script /usr/sbin/mkdict.
# Script originale copyright 1993, di Alec Muffett.
#
#  Lo script modificato incluso nel presente documento si conforma solo
#+ fino a un certo punto con la "LICENSE" document of the "Crack" del pacchetto
#+ a cui lo script originale appartiene.

#  Lo script elabora file di testo e produce un elenco ordinato
#+ delle parole trovate nel(i) file.
#  Può essere utile per la compilazione di dizionari
#+ e per ricerche lessicografiche.


E_ERR_ARG=65

if [ ! -r "$1" ]                     #  Occorre almeno un
then                                 #+ argomento valido.
  echo "Utilizzo: $0 file-da-elaborare"
  exit $E_ERR_ARG
fi  


# SORT="sort"                        #  Non più necessario per definire
                                     #+ le opzioni di sort. 
                                     #+ Modifica allo script originale.

cat $* |                             #  Visualizza il contenuto del(i) file
                                     #+ allo stdout.
        tr A-Z a-z |                 #  Convertito in lettere minuscole.
        tr ' ' '\012' |              #  Nuovo: cambia gli spazi in "a capo".
#       tr -cd '\012[a-z][0-9]' |    #  Elimina tutto ciò che non è 
                                     #+ alfanumerico (script originale).
        tr -c '\012a-z'  '\012' |    #  Invece che cancellarli, adesso i 
                                     #+ caratteri non alfanumerici vengono
                                     #+ trasformati in "a capo".
        sort |                       #  Le opzioni $SORT ora non sono più 
                                     #+ necessarie.
        uniq |                       #  Rimuove le dupplicazioni.
        grep -v '^#' |               #  Cancella le righe che iniziano con un
                                     #+ cancelletto (#).
        grep -v '^$'                 #  Cancella le righe vuote.

exit 0	

Esempio A-9. Codifica soundex

#!/bin/bash
# soundex.sh: Calcola il codice "soundex" dei nomi

# ===========================================================
#        Soundex script
#              di
#         Mendel Cooper
#     thegrendel@theriver.com
#       23 January, 2002
#
#     di Domainio Pubblico.
#
#  Una versione di questo script leggermente diversa è apparsa
#+ in Ed Schaefer's July, 2002 "Shell Corner" column
#+ in "Unix Review" on-line,
#+ http://www.unixreview.com/documents/uni1026336632258/
# ===========================================================


CONTA_ARG=1                     # Occorre un nome come argomento. [1]
E_ERR_ARG=70

if [ $# -ne "$CONTA_ARG" ]
then
  echo "Utilizzo: `basename $0` nome"
  exit $E_ERR_ARG
fi  


assegna_valore ()              #  Assegna un un numero
{                              #+ alle lettere del nome.

  val1=bfpv                    # 'b,f,p,v' = 1
  val2=cgjkqsxz                # 'c,g,j,k,q,s,x,z' = 2
  val3=dt                      #  ecc.
  val4=l
  val5=mn
  val6=r

# Ecco un uso straordinariamente intelligente di 'tr'.
# Cercate di scoprire cosa succede.

valore=$( echo "$1" \
| tr -d wh \
| tr $val1 1 | tr $val2 2 | tr $val3 3 \
| tr $val4 4 | tr $val5 5 | tr $val6 6 \
| tr -s 123456 \
| tr -d aeiouy )

#  Assegna dei valori alle lettere.
#  Rimuove i numeri dupplicati, tranne quando sono separati da vocali.
#  Ignora le vocali, tranne quando separano le consonanti, 
#+ che vengono cancellate per ultime.
#  Ignora 'w' e 'h', che vengono cancellate subito.
#
#  The above command substitution lays more pipe than a plumber <g>. [2]

}  


input_nome="$1"
echo
echo "Nome = $input_nome"


# Cambia tutti i caratteri del nome in lettere minuscole.
# ------------------------------------------------------
nome=$( echo $input_nome | tr A-Z a-z )
# ------------------------------------------------------
# Nel caso il nome fornito contenga sia maiuscole che minuscole.


# Prefisso del codice soundex: la prima lettera del nome.
# ------------------------------------------------------


pos_car=0                      #  Inizializza la posizione del carattere. 
prefisso0=${nome:$pos_car:1}
prefisso=`echo $prefisso0 | tr a-z A-Z`
                               #  Cambia in maiuscolo la prima lettera 
                               #+ del codice soundex.

let "pos_car += 1"             #  Incrementa la posizione del carattere che 
                               #+ adesso corrisponde alla 2a lettera del nome.
nome1=${nome:$pos_car}


# +++++++++++++++++++++ Gestione delle Eccezioni ++++++++++++++++++++++++++++
#  Adesso vengono passati alla funzione di assegnamento-valore sia il nome 
#+ completo che lo stesso nome a cui è stata tolta la lettera iniziale.
#  Se si ottiene lo stesso valore ciò significa che ai primi due caratteri
#+ del nome corrisponde lo stesso valore, quindi il secondo va cancellato.
#  Inoltre, è necessario controllare se la prima lettera del nome
#+ è una vocale, una 'w' o una 'h', perché questo può incasinare tutto. [3]

car1=`echo $prefisso | tr A-Z a-z`  # Prima lettera del nome in maiuscolo.

assegna_valore $nome
s1=$valore
assegna_valore $nome1
s2=$valore
assegna_valore $car1
s3=$valore
s3=9$s3                              #  Se la prima lettera del nome è 
                                     #+ una vocale o una 'w' o una 'h',
                                     #+ allora "valore" è nulla 
                                     #+ (non impostata). Viene quindi impostata
                                     #+ a 9, valore altrimenti inutilizzato, 
                                     #+ per poter effettuare una verifica.


if [[ "$s1" -ne "$s2" || "$s3" -eq 9 ]]
then
  suffisso=$s2
else  
  suffisso=${s2:$car_pos}
fi  
# ++++++++++++++++++ fine Gestione delle Eccezioni ++++++++++++++++++++++++++


completa=000                      #  Si usano al massimo 3 zeri 
                                  #+ per completare il codice.


soun=$prefisso$suffisso$completa  # Completa con gli zeri (se necessario).

LUNGMAX=4                         # Il codice viene ridotto a 4 caratteri.
soundex=${soun:0:$LUNGMAX}

echo "Soundex = $soundex"

echo

#  Il codice soundex è un metodo per l'ordinamento e la classiicazione
#+ dei nomi somiglianti.
#  Il codice soundex di un dato nome è formato dalla prima lettera del nome,
#+ seguita da tre cifre calcolate nel modo visto sopra.
#  A nomi simili dovrebbero corrispondere codici soundex quasi uguali.

#   Esempi:
#   Smith e Smythe hanno entrambi codice soundex "S-530".
#   Harrison = H-625
#   Hargison = H-622
#   Harriman = H-655

#  Nella pratica tutto questo funziona piuttosto bene, 
#+ sebbene ci siano alcune anomalie.
#
#
#  Lo U.S. Census e alcune altre agenzie governative, utilizzano il soundex
#  per effettuare ricerche genealogiche.
#
#  Per ulteriori informazioni,
#+ vedi la "National Archives and Records Administration home page",
#+ http://www.nara.gov/genealogy/soundex/soundex.html



# Esercizio:
# ---------
# Semplificate la sezione "Gestione delle Eccezioni" dello script.

exit 0

# [N.d.T.]
# 1 - M. Cooper usa il termine "name" che ho tradotto, ovviamente, con "nome".
#     In realtà la documentazione riguardante la codifica soundex usa
#     il termine "surname", vale a dire "cognome" (ed infatti sono dei cognomi
#     quelli riportati dallo stesso Cooper negli esempi presentati 
#     nello script).
# 2 - Gioco di parole intraducibile in italiano basato sul diverso significato
#     che la parola "pipe" assume nell'ambito dello scripting di shell (dove 
#     non viene tradotta) e nell'uso comune, cioè "condotto, tubo, 
#     canna, ecc.". La frase tradotta letteralmente risulterebbe:
#     "La precedente sostituzione di comando ha installato più tubi di
#     un idraulico". <g> è l'abbreviazione della parola inglese
#     "grin", vale a dire "sorriso".
# 3 - L'autore ha usato il verbo "to bollix up" che in gergo significa appunto
#     "incasinare". Non si tratta, quindi, di una mia licenza.

Esempio A-10. "Game of Life"

#!/bin/bash
# life.sh: "Life in the Slow Lane"
# Version 2: Patched by Daniel Albers
#+           to allow non-square grids as input.

# ##################################################################### #
# This is the Bash script version of John Conway's "Game of Life".      #
# "Life" is a simple implementation of cellular automata.               #
# --------------------------------------------------------------------- #
# On a rectangular grid, let each "cell" be either "living" or "dead".  #
# Designate a living cell with a dot, and a dead one with a blank space.#
#  Begin with an arbitrarily drawn dot-and-blank grid,                  #
#+ and let this be the starting generation, "generation 0".             #
# Determine each successive generation by the following rules:          #
# 1) Each cell has 8 neighbors, the adjoining cells                     #
#+   left, right, top, bottom, and the 4 diagonals.                     #
#                       123                                             #
#                       4*5                                             #
#                       678                                             #
#                                                                       #
# 2) A living cell with either 2 or 3 living neighbors remains alive.   #
# 3) A dead cell with 3 living neighbors becomes alive (a "birth").     #
SURVIVE=2                                                               #
BIRTH=3                                                                 #
# 4) All other cases result in a dead cell for the next generation.     #
# ##################################################################### #


startfile=gen0   # Read the starting generation from the file "gen0".
                 # Default, if no other file specified when invoking script.
                 #
if [ -n "$1" ]   # Specify another "generation 0" file.
then
  if [ -e "$1" ] # Check for existence.
  then
    startfile="$1"
  fi  
fi  


ALIVE1=.
DEAD1=_
                 # Represent living and "dead" cells in the start-up file.

#  ---------------------------------------------------------- #
#  This script uses a 10 x 10 grid (may be increased,
#+ but a large grid will will cause very slow execution).
ROWS=10
COLS=10
#  Change above two variables to match grid size, if necessary.
#  ---------------------------------------------------------- #

GENERATIONS=10          #  How many generations to cycle through.
                        #  Adjust this upwards,
                        #+ if you have time on your hands.

NONE_ALIVE=80           #  Exit status on premature bailout,
                        #+ if no cells left alive.
TRUE=0
FALSE=1
ALIVE=0
DEAD=1

avar=                   # Global; holds current generation.
generation=0            # Initialize generation count.

# =================================================================


let "cells = $ROWS * $COLS"
                        # How many cells.

declare -a initial      # Arrays containing "cells".
declare -a current

display ()
{

alive=0                 # How many cells "alive" at any given time.
                        # Initially zero.

declare -a arr
arr=( `echo "$1"` )     # Convert passed arg to array.

element_count=${#arr[*]}

local i
local rowcheck

for ((i=0; i<$element_count; i++))
do

  # Insert newline at end of each row.
  let "rowcheck = $i % COLS"
  if [ "$rowcheck" -eq 0 ]
  then
    echo                # Newline.
    echo -n "      "    # Indent.
  fi  

  cell=${arr[i]}

  if [ "$cell" = . ]
  then
    let "alive += 1"
  fi  

  echo -n "$cell" | sed -e 's/_/ /g'
  # Print out array and change underscores to spaces.
done  

return

}

IsValid ()                            # Test whether cell coordinate valid.
{

  if [ -z "$1"  -o -z "$2" ]          # Mandatory arguments missing?
  then
    return $FALSE
  fi

local row
local lower_limit=0                   # Disallow negative coordinate.
local upper_limit
local left
local right

let "upper_limit = $ROWS * $COLS - 1" # Total number of cells.


if [ "$1" -lt "$lower_limit" -o "$1" -gt "$upper_limit" ]
then
  return $FALSE                       # Out of array bounds.
fi  

row=$2
let "left = $row * $COLS"             # Left limit.
let "right = $left + $COLS - 1"       # Right limit.

if [ "$1" -lt "$left" -o "$1" -gt "$right" ]
then
  return $FALSE                       # Beyond row boundary.
fi  

return $TRUE                          # Valid coordinate.

}  


IsAlive ()              # Test whether cell is alive.
                        # Takes array, cell number, state of cell as arguments.
{
  GetCount "$1" $2      # Get alive cell count in neighborhood.
  local nhbd=$?


  if [ "$nhbd" -eq "$BIRTH" ]  # Alive in any case.
  then
    return $ALIVE
  fi

  if [ "$3" = "." -a "$nhbd" -eq "$SURVIVE" ]
  then                  # Alive only if previously alive.
    return $ALIVE
  fi  

  return $DEAD          # Default.

}  


GetCount ()             # Count live cells in passed cell's neighborhood.
                        # Two arguments needed:
			# $1) variable holding array
			# $2) cell number
{
  local cell_number=$2
  local array
  local top
  local center
  local bottom
  local r
  local row
  local i
  local t_top
  local t_cen
  local t_bot
  local count=0
  local ROW_NHBD=3

  array=( `echo "$1"` )

  let "top = $cell_number - $COLS - 1"    # Set up cell neighborhood.
  let "center = $cell_number - 1"
  let "bottom = $cell_number + $COLS - 1"
  let "r = $cell_number / $COLS"

  for ((i=0; i<$ROW_NHBD; i++))           # Traverse from left to right. 
  do
    let "t_top = $top + $i"
    let "t_cen = $center + $i"
    let "t_bot = $bottom + $i"


    let "row = $r"                        # Count center row of neighborhood.
    IsValid $t_cen $row                   # Valid cell position?
    if [ $? -eq "$TRUE" ]
    then
      if [ ${array[$t_cen]} = "$ALIVE1" ] # Is it alive?
      then                                # Yes?
        let "count += 1"                  # Increment count.
      fi	
    fi  

    let "row = $r - 1"                    # Count top row.          
    IsValid $t_top $row
    if [ $? -eq "$TRUE" ]
    then
      if [ ${array[$t_top]} = "$ALIVE1" ] 
      then
        let "count += 1"
      fi	
    fi  

    let "row = $r + 1"                    # Count bottom row.
    IsValid $t_bot $row
    if [ $? -eq "$TRUE" ]
    then
      if [ ${array[$t_bot]} = "$ALIVE1" ] 
      then
        let "count += 1"
      fi	
    fi  

  done  


  if [ ${array[$cell_number]} = "$ALIVE1" ]
  then
    let "count -= 1"        #  Make sure value of tested cell itself
  fi                        #+ is not counted.


  return $count
  
}

next_gen ()               # Update generation array.
{

local array
local i=0

array=( `echo "$1"` )     # Convert passed arg to array.

while [ "$i" -lt "$cells" ]
do
  IsAlive "$1" $i ${array[$i]}   # Is cell alive?
  if [ $? -eq "$ALIVE" ]
  then                           #  If alive, then
    array[$i]=.                  #+ represent the cell as a period.
  else  
    array[$i]="_"                #  Otherwise underscore
   fi                            #+ (which will later be converted to space).  
  let "i += 1" 
done   


# let "generation += 1"   # Increment generation count.
# Why was the above line commented out?


# Set variable to pass as parameter to "display" function.
avar=`echo ${array[@]}`   # Convert array back to string variable.
display "$avar"           # Display it.
echo; echo
echo "Generation $generation  -  $alive alive"

if [ "$alive" -eq 0 ]
then
  echo
  echo "Premature exit: no more cells alive!"
  exit $NONE_ALIVE        #  No point in continuing
fi                        #+ if no live cells.

}


# =========================================================

# main ()

# Load initial array with contents of startup file.
initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\
sed -e 's/\./\. /g' -e 's/_/_ /g'` )
# Delete lines containing '#' comment character.
# Remove linefeeds and insert space between elements.

clear          # Clear screen.

echo #         Title
echo "======================="
echo "    $GENERATIONS generations"
echo "           of"
echo "\"Life in the Slow Lane\""
echo "======================="


# -------- Display first generation. --------
Gen0=`echo ${initial[@]}`
display "$Gen0"           # Display only.
echo; echo
echo "Generation $generation  -  $alive alive"
# -------------------------------------------


let "generation += 1"     # Increment generation count.
echo

# ------- Display second generation. -------
Cur=`echo ${initial[@]}`
next_gen "$Cur"          # Update & display.
# ------------------------------------------

let "generation += 1"     # Increment generation count.

# ------ Main loop for displaying subsequent generations ------
while [ "$generation" -le "$GENERATIONS" ]
do
  Cur="$avar"
  next_gen "$Cur"
  let "generation += 1"
done
# ==============================================================

echo

exit 0

# --------------------------------------------------------------

# The grid in this script has a "boundary problem."
# The the top, bottom, and sides border on a void of dead cells.
# Exercise: Change the script to have the grid wrap around,
# +         so that the left and right sides will "touch,"      
# +         as will the top and bottom.
#
# Exercise: Create a new "gen0" file to seed this script.
#           Use a 12 x 16 grid, instead of the original 10 x 10 one.
#           Make the necessary changes to the script,
#+          so it will run with the altered file.
#
# Exercise: Modify this script so that it can determine the grid size
#+          from the "gen0" file, and set any variables necessary
#+          for the script to run.
#           This would make unnecessary any changes to variables
#+          in the script for an altered grid size.

Esempio A-11. File dati per "Game of Life"

# gen0
#
# This is an example "generation 0" start-up file for "life.sh".
# --------------------------------------------------------------
#  The "gen0" file is a 10 x 10 grid using a period (.) for live cells,
#+ and an underscore (_) for dead ones. We cannot simply use spaces
#+ for dead cells in this file because of a peculiarity in Bash arrays.
#  [Exercise for the reader: explain this.]
#
# Lines beginning with a '#' are comments, and the script ignores them.
__.__..___
___._.____
____.___..
_._______.
____._____
..__...___
____._____
___...____
__.._..___
_..___..__

+++

I due script seguenti sono di Mark Moraes della University of Toronto. Si veda il file Moraes-COPYRIGHT per quanto riguarda i permessi e le restrizioni. Questo file è inserito nei formati HTML/sorgente tarball della Guida ASB

Esempio A-12. behead: togliere le intestazioni dai messaggi di e-mail e di news

#! /bin/sh
# Toglie l'intestazione da una e-mail/messaggio News, vale a dire tutto
# fino alla prima riga vuota.
# Mark Moraes, University of Toronto

# ==> Questi commenti sono stati aggiunti dall'autore del libro.

if [ $# -eq 0 ]; then
# ==> Se non ci sono argomenti da riga di comando, 
#+    agisce sul file rediretto allo stdin.
	sed -e '1,/^$/d' -e '/^[ 	]*$/d'
	# --> Cancella tutte le righe, comprese quelle vuote,
	# --> fino a quella che inizia con uno spazio.
else
# ==> Se sono stati passati degli argomenti, agisce sul/i file passato(i).
	for i do
		sed -e '1,/^$/d' -e '/^[ 	]*$/d' $i
		# --> Idem, come sopra.
	done
fi

# ==> Esercizio: aggiungete una verifica d'errore ed altre opzioni.
# ==>
# ==> Notate che il breve script sed viene ripetuto due volte, 
# ==> con la solo differenza dell'argomento passato.
# ==> Avrebbe senso inserirlo in una funzione? Perché sì o perché no?

Esempio A-13. ftpget: scaricare file via ftp

#! /bin/sh 
# $Id: ftpget.sh,v 1.1.1.1 2003/06/25 22:41:32 giacomo Exp $ 
# Script per l'esecuzione batch di un ftp anonimo. Praticamente, trasforma un
# elenco di comandi passati come argomenti da riga di comando 
# in un input per ftp.
# ==> Questo script non è nient'altro che uno shell wrapper ad "ftp" . . .
# Semplice e rapido - scritto come compagno di ftplist 
# -h specifica l'host remoto (default: prep.ai.mit.edu) 
# -d specifica la directory remota a cui si vuole accedere - possono
# essere indicate più opzioni -d - in questo caso il programma vi 
# accederà in sequenza. Se i percorsi sono relativi, 
# si deve far attenzione a indicarli esattamente. Prudenza con i 
# percorsi relativi - ci sono fin troppi link simbolici oggigiorno.  
# (la directory di default è quella di login di ftp)
# -v abilita l'opzione verbose di ftp, per visualizzare tutte le risposte
# provenienti dal server ftp.  
# -f fileremoto[:filelocale] nome da assegnare in locale al file remoto.
# -m modello, effettua un mget con il modello specificato. Ricordate di
# applicare il quoting ai caratteri.  
# -c accede alla directory locale specificata.
# Per esempio, 
# 	ftpget -h expo.lcs.mit.edu -d contrib -f xplaces.shar:xplaces.sh \
#		-d ../pub/R3/fixes -c ~/fixes -m 'fix*' 
# Ottiene xplaces.shar da ~ftp/contrib su expo.lcs.mit.edu, registrandolo come
# xplaces.sh nella directory di lavoro corrente; recupera tutti i fixes da
# ~ftp/pub/R3/fixes e li inserisce nella directory ~/fixes. 
# Ovviamente, è importante la sequenza delle opzioni, perché i
# corrispondenti comandi vengono eseguiti da ftp in quell'ordine.
#
# Mark Moraes (moraes@csri.toronto.edu), Feb 1, 1989 
#


# ==> Questi commenti sono stati aggiunti dall'autore del libro.

# PATH=/local/bin:/usr/ucb:/usr/bin:/bin
# export PATH
# ==> Le 2 righe precedenti dello script originale sono 
#        probabilmente superflue.

E_ERR_ARG=65

FILETMP=/tmp/ftp.$$
# ==> Crea un file temporaneo, il cui nome viene formato utilizzando
# ==> l'id di processo dello script ($$).

SITO=`domainname`.toronto.edu
# ==> 'domainname' simile a 'hostname'
# ==> Andrebbe riscritto per un uso più generico.

utilizzo="Utilizzo: $0 [-h hostremoto] [-d directoryremota]... \
          [-f filerem:filelocale]... [-c directorylocale] [-m modello] [-v]"
opzftp="-i -n"
opzverb=
set -f 		# In modo da abilitare il globbing in -m
set x `getopt vh:d:c:m:f: $*`
if [ $? != 0 ]; then
	echo $utilizzo
	exit $E_ERR_ARG
fi
shift
trap 'rm -f ${FILETMP} ; exit' 0   1     2              3        15
# ==>                   Segnali:   HUP   INT (Ctl-C)    QUIT     TERM
# ==> Cancella il file temporaneo in caso di uscita anomala dallo script.
echo "utente anonimo ${USER-gnu}@${SITO} > ${FILETMP}"
# ==> Usate gli apici (raccomandati per visualizzazioni complesse).
echo binary >> ${FILETMP}
for i in $*   # ==> Elaborazione degli argomenti passati da riga di comando.
do
	case $i in
	-v) opzverb=-v; echo hash >> ${FILETMP}; shift;;
	-h) hostrem=$2; shift 2;;
	-d) echo cd $2 >> ${FILETMP};
	    if [ x${opzverb} != x ]; then
	        echo pwd >> ${FILETMP};
	    fi;
	    shift 2;;
	-c) echo lcd $2 >> ${FILETMP}; shift 2;;
	-m) echo mget "$2" >> ${FILETMP}; shift 2;;
	-f) f1=`expr "$2" : "\([^:]*\).*"`; f2=`expr "$2" : "[^:]*:\(.*\)"`;
	    echo get ${f1} ${f2} >> ${FILETMP}; shift 2;;
	--) shift; break;;
	esac
        # ==> 'lcd' e 'mget' sono comandi ftp. Vedi "man ftp" . . .
done
if [ $# -ne 0 ]; then
	echo $utilizzo
	exit $E_ERR_ARG   
	# ==> "exit 2" nell'originale, cambiato per uniformarsi allo standard.
fi
if [ x${opzverb} != x ]; then
	opzftp="${opzftp} -v"
fi
if [ x${hostrem} = x ]; then
	hostrem=prep.ai.mit.edu
	# ==> Modificatelo per il sito ftp appropriato.
fi
echo quit >> ${FILETMP}
# ==> Tutti i comandi salvati nel file temporaneo.

ftp ${opzftp} ${hostrem} < ${FILETMP}
# ==> Il file temporaneao viene elaborato in modalità batch da ftp.

rm -f ${FILETMP}
# ==> Infine, il file temporaneo viene cancellato 
#        (potreste desiderare registralo in un file di log).


# ==> Esercizi:
# ==> --------
# ==> 1) Aggiungete le verifiche d'errore.
# ==> 2) Aggiungete altri fronzoli.

+

Antek Sawicki ha fornito lo script seguente che fa un uso molto intelligente degli operatori di sostituzione di parametro discussi in la Sezione 9.3.

Esempio A-14. password: generare password casuali di 8 caratteri

#!/bin/bash
#  Su macchine un po' vecchie, 
#+ potrebbe essere necessario cambiare l'intestazione in #!/bin/bash2.
#
#  Generatore di password casuali per Bash 2.x +
#+ di Antek Sawicki <tenox@tenox.tc>,
#+ che ha generosamente permesso all'autore de Guida ASB il suo utilizzo.
#
# ==> Commenti aggiunti dall'autore del libro ==>


MATRICE="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# ==> La password viene formata con caratteri alfanumerici.
LUNGHEZZA="8"
# ==> Se desiderate passaword più lunghe modificate 'LUNGHEZZA'.


while [ "${n:=1}" -le "$LUNGHEZZA" ]
# ==> Ricordo che := è l'operatore "sostiruzione default".
# ==> Quindi, se 'n' non è stata inizializzata, viene impostata ad 1.
do
	PASS="$PASS${MATRICE:$(($RANDOM%${#MATRICE})):1}"
	# ==> Molto intelligente e scaltro.

	# ==> Iniziando dall'annidamento più interno...
	# ==> ${#MATRICE} restituisce la lunghezza dell'array MATRICE.

	# ==> $RANDOM%${#MATRICE} restituisce un numero casuale compreso tra 1
	# ==> e [lunghezza di MATRICE] - 1.

	# ==> ${MATRICE:$(($RANDOM%${#MATRICE})):1}
	# ==> restituisce l'espansione di lunghezza 1 di MATRICE 
	# ==> partendo da una posizione casuale.
	# ==> Vedi la sostituzione di parametro {var:pos:lun},
	# ==> con relativi esempi, al Capitolo 9.

	# ==> PASS=... aggiunge semplicemente il risultato al precedente 
	# ==> valore di PASS (concatenamento).

	# ==> Per visualizzare tutto questo più chiaramente, 
	# ==> decommentate la riga seguente
	#                 echo "$PASS"
	# ==> e vedrete come viene costruita PASS,
	# ==> un carattere alla volta ad ogni iterazione del ciclo.

	let n+=1
	# ==> Incrementa 'n' per il passaggio successivo.
done

echo "$PASS"      # ==> Oppure, se preferite, redirigetela in un file.

exit 0

+

James R. Van Zandt ha fornito questo script che fa uso delle named pipe ed è, parole sue, "vero esercizio di quoting ed escaping".

Esempio A-15. fifo: eseguire backup giornalieri utilizzando le named pipe

#!/bin/bash
# ==> Script di James R. Van Zandt, e qui usato con il suo permesso.

# ==> Commenti aggiunti dall'aurore del libro.

  
  QUI=`uname -n`    # ==> hostname
  LA=bilbo
  echo "inizio di un backup remoto in $LA alle `date +%r`"
  # ==> `date +%r` restituisce l'ora nel formato 12-ore, cioè "08:08:34 PM".
  
  # si accerta che /pipe sia veramente una pipe e non un file regolare
  rm -rf /pipe
  mkfifo /pipe       # ==> Crea una "named pipe", di nome "/pipe".
  
  # ==> 'su xyz' esegue i comandi come utente "xyz".
  # ==> 'ssh' invoca secure shell (client per il login remoto).
  su xyz -c "ssh $LA \"cat > /home/xyz/backup/${QUI}-daily.tar.gz\" < /pipe"&
  cd /
  tar -czf - bin boot dev etc home info lib man root sbin share usr var > /pipe
  # ==>  Impiega la named pipe, /pipe, per la comunicazione tra processi:
  # ==>+ 'tar/gzip' scrive in /pipe e 'ssh' legge da /pipe.

  # ==>  Il risultato finale è il backup delle principali directory, 
  # ==>+ da / in giù.

  # ==>  Quali sono i vantaggi di una "named pipe" in una situazione come 
  # ==>+ questa, contrapposti a quelli di una "pipe anonima", con |?
  # ==>  In questo caso funzionerebbe anche una pipe anonima?

  # ==>  È necessario cancellare la pipe prima di uscire dallo script?
  # ==>  In che modo potrebbe essere fatto?


  exit 0

+

Questo script, inviato da Stéphane Chazelas, dimostra che si possono generare numeri primi senza ricorrere agli array.

Esempio A-16. Generare numeri primi utilizzando l'operatore modulo

#!/bin/bash
# primes.sh: Genera numeri primi, senza l'impiego degli array.
# Script fornito da Stephane Chazelas.

#  Qui *non* viene usato il classico algoritmo del "Crivello di Eratostene",
#+ viene invece usato il metodo più intuitivo di verificare ogni numero
#+ tramite i fattori (divisori), per mezzo dell'operatore modulo "%".


LIMITE=1000                   # Primi 2 - 1000

Primi()
{
 (( n = $1 + 1 ))             # Salta all'intero successivo.
 shift                        # Parametro successivo nell'elenco.
#  echo "_n=$n i=$i_"
 
 if (( n == LIMITE ))
 then echo $*
 return
 fi

 for i; do                    # "i" viene impostato a "@", 
                              #+ i valori precedenti di $n.
#   echo "-n=$n i=$i-"
   (( i * i > n )) && break   # Ottimizzazione.
   (( n % i )) && continue    # Scarta i non-primi usando l'operatore modulo.
   Primi $n $@                # Ricorsività all'interno del ciclo.
   return
   done

   Primi $n $@ $n             # Ricorsività esterna al ciclo.
                              # Accumula in successione i parametri posizionali.
                              # "$@" è l'elenco dei numeri primi.
}

Primi 1

exit 0

#  Decommentate le righe 16 e 25 come aiuto per scoprire quello che succede.

#  Confrontate la velocità di questo algoritmo
#+ rispetto al Crivello di Eratostene (ex68.sh).

#  Esercizio: per un'esecuzione ancor più veloce, riscrivete lo script 
#+ senza usare la ricorsività.

+

Questa è la revisione di Rick Boivie dello script tree di Jordi Sanfeliu.

Esempio A-17. tree: visualizzare l'albero di una directory

#!/bin/bash
# tree.sh

#  Scritto da Rick Boivie.
#  Usato con il suo consenso.
#  È la versione rivista e semplificata di uno script
#+ di Jordi Sanfeliu (e sistemato da Ian Kjos).
#  Il presente script sostituisce la precedente versione apparsa
#+ nelle precedenti release della Guida Avanzata di Scripting Bash.

# ==> Commenti aggiunti dall'autore del libro.


ricerca () {
for dir in `echo *`
#  ==> `echo *` elenca tutti i file della directory di lavoro corrente,
#+ ==> senza interruzioni di riga.
#  ==> Effetto simile a for dir in *
#  ==> ma "dir in `echo *`" non elabora i file i cui nomi contengono spazi.
do
  if [ -d "$dir" ] ; then # ==> Se si tratta di una directory (-d)...
  zz=0                    # ==> Variabile temporanea per registrare il livello
                          # ==> della directory.
  while [ $zz != $1 ]     # Per la gestione del ciclo interno.
    do
      echo -n "| "        # ==> Visualizza il simbolo di collegamento 
                          # ==> verticale, con 2 spazi & nessun "a capo" 
                          # ==> per effetuare l'indentazione.
      zz=`expr $zz + 1`   # ==> Incrementa zz.
    done

    if [ -L "$dir" ] ; then # ==> Se la directory è un link simbolico ...
      echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'`
      # ==> Visualizza il simbolo di collegamento orizzontale seguito dal nome 
      # ==> della directory, ma...
      # ==> cancella la parte riguardante data/ora.
    else
      echo "+---$dir"       # ==> Visualizza il simbolo di collegamento 
      # ==> orizzontale...e il nome della directory.
      numdir=`expr $numdir + 1` # ==> Incrementa il contatore delle directory.
      if cd "$dir" ; then       # ==> Se si può accedere alla sottodirectory...
        ricerca `expr $1 + 1`   # ricorsività ;-)
        # ==> Funzione che richiama se stessa.
        cd ..
      fi
    fi
  fi
done
}

if [ $# != 0 ] ; then
  cd $1       # si sposta nella directory indicata.
  #else       # rimane nella directory corrente
fi

echo "Directory iniziale = `pwd`"
numdir=0

ricerca 0
echo "Totale directory = $numdir"

exit 0

Noah Friedman ha permesso l'uso del suo script string function, che riproduce, sostanzialmente, alcune delle funzioni per la manipolazione di stringhe della libreria C.

Esempio A-18. string functions: funzioni per stringhe simili a quelle del C

#!/bin/bash

# string.bash --- bash emulation of string(3) library routines
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# ==>     Used with his kind permission in this document.
# Created: 1992-07-01
# Last modified: 1993-09-29
# Public domain

# Conversion to bash v2 syntax done by Chet Ramey

# Commentary:
# Code:

#:docstring strcat:
# Usage: strcat s1 s2
#
# Strcat appends the value of variable s2 to variable s1. 
#
# Example:
#    a="foo"
#    b="bar"
#    strcat a b
#    echo $a
#    => foobar
#
#:end docstring:

###;;;autoload   ==> Autoloading of function commented out.
function strcat ()
{
    local s1_val s2_val

    s1_val=${!1}                        # indirect variable expansion
    s2_val=${!2}
    eval "$1"=\'"${s1_val}${s2_val}"\'
    # ==> eval $1='${s1_val}${s2_val}' avoids problems,
    # ==> if one of the variables contains a single quote.
}

#:docstring strncat:
# Usage: strncat s1 s2 $n
#
# Line strcat, but strncat appends a maximum of n characters from the value
# of variable s2.  It copies fewer if the value of variabl s2 is shorter
# than n characters.  Echoes result on stdout.
#
# Example:
#    a=foo
#    b=barbaz
#    strncat a b 3
#    echo $a
#    => foobar
#
#:end docstring:

###;;;autoload
function strncat ()
{
    local s1="$1"
    local s2="$2"
    local -i n="$3"
    local s1_val s2_val

    s1_val=${!s1}                       # ==> indirect variable expansion
    s2_val=${!s2}

    if [ ${#s2_val} -gt ${n} ]; then
       s2_val=${s2_val:0:$n}            # ==> substring extraction
    fi

    eval "$s1"=\'"${s1_val}${s2_val}"\'
    # ==> eval $1='${s1_val}${s2_val}' avoids problems,
    # ==> if one of the variables contains a single quote.
}

#:docstring strcmp:
# Usage: strcmp $s1 $s2
#
# Strcmp compares its arguments and returns an integer less than, equal to,
# or greater than zero, depending on whether string s1 is lexicographically
# less than, equal to, or greater than string s2.
#:end docstring:

###;;;autoload
function strcmp ()
{
    [ "$1" = "$2" ] && return 0

    [ "${1}" '<' "${2}" ] > /dev/null && return -1

    return 1
}

#:docstring strncmp:
# Usage: strncmp $s1 $s2 $n
#
# Like strcmp, but makes the comparison by examining a maximum of n
# characters (n less than or equal to zero yields equality).
#:end docstring:

###;;;autoload
function strncmp ()
{
    if [ -z "${3}" -o "${3}" -le "0" ]; then
       return 0
    fi

    if [ ${3} -ge ${#1} -a ${3} -ge ${#2} ]; then
       strcmp "$1" "$2"
       return $?
    else
       s1=${1:0:$3}
       s2=${2:0:$3}
       strcmp $s1 $s2
       return $?
    fi
}

#:docstring strlen:
# Usage: strlen s
#
# Strlen returns the number of characters in string literal s.
#:end docstring:

###;;;autoload
function strlen ()
{
    eval echo "\${#${1}}"
    # ==> Returns the length of the value of the variable
    # ==> whose name is passed as an argument.
}

#:docstring strspn:
# Usage: strspn $s1 $s2
#
# Strspn returns the length of the maximum initial segment of string s1,
# which consists entirely of characters from string s2.
#:end docstring:

###;;;autoload
function strspn ()
{
    # Unsetting IFS allows whitespace to be handled as normal chars.
    local IFS=
    local result="${1%%[!${2}]*}"

    echo ${#result}
}

#:docstring strcspn:
# Usage: strcspn $s1 $s2
#
# Strcspn returns the length of the maximum initial segment of string s1,
# which consists entirely of characters not from string s2.
#:end docstring:

###;;;autoload
function strcspn ()
{
    # Unsetting IFS allows whitspace to be handled as normal chars.
    local IFS=
    local result="${1%%[${2}]*}"

    echo ${#result}
}

#:docstring strstr:
# Usage: strstr s1 s2
#
# Strstr echoes a substring starting at the first occurrence of string s2 in
# string s1, or nothing if s2 does not occur in the string.  If s2 points to
# a string of zero length, strstr echoes s1.
#:end docstring:

###;;;autoload
function strstr ()
{
    # if s2 points to a string of zero length, strstr echoes s1
    [ ${#2} -eq 0 ] && { echo "$1" ; return 0; }

    # strstr echoes nothing if s2 does not occur in s1
    case "$1" in
    *$2*) ;;
    *) return 1;;
    esac

    # use the pattern matching code to strip off the match and everything
    # following it
    first=${1/$2*/}

    # then strip off the first unmatched portion of the string
    echo "${1##$first}"
}

#:docstring strtok:
# Usage: strtok s1 s2
#
# Strtok considers the string s1 to consist of a sequence of zero or more
# text tokens separated by spans of one or more characters from the
# separator string s2.  The first call (with a non-empty string s1
# specified) echoes a string consisting of the first token on stdout. The
# function keeps track of its position in the string s1 between separate
# calls, so that subsequent calls made with the first argument an empty
# string will work through the string immediately following that token.  In
# this way subsequent calls will work through the string s1 until no tokens
# remain.  The separator string s2 may be different from call to call.
# When no token remains in s1, an empty value is echoed on stdout.
#:end docstring:

###;;;autoload
function strtok ()
{
 :
}

#:docstring strtrunc:
# Usage: strtrunc $n $s1 {$s2} {$...}
#
# Used by many functions like strncmp to truncate arguments for comparison.
# Echoes the first n characters of each string s1 s2 ... on stdout.
#:end docstring:

###;;;autoload
function strtrunc ()
{
    n=$1 ; shift
    for z; do
        echo "${z:0:$n}"
    done
}

# provide string

# string.bash ends here


# ========================================================================== #
# ==> Everything below here added by the document author.

# ==> Suggested use of this script is to delete everything below here,
# ==> and "source" this file into your own scripts.

# strcat
string0=one
string1=two
echo
echo "Testing \"strcat\" function:"
echo "Original \"string0\" = $string0"
echo "\"string1\" = $string1"
strcat string0 string1
echo "New \"string0\" = $string0"
echo

# strlen
echo
echo "Testing \"strlen\" function:"
str=123456789
echo "\"str\" = $str"
echo -n "Length of \"str\" = "
strlen str
echo



# Exercise:
# --------
# Add code to test all the other string functions above.


exit 0

Esempio di array complesso, di Michael Zick, che utilizza il comando md5sum per codificare informazioni sulle directory.

Esempio A-19. Informazioni sulle directory

#! /bin/bash
# directory-info.sh
# Parses and lists directory information.

# NOTE: Change lines 273 and 353 per "README" file.

# Michael Zick is the author of this script.
# Used here with his permission.

# Controls
# If overridden by command arguments, they must be in the order:
#   Arg1: "Descriptor Directory"
#   Arg2: "Exclude Paths"
#   Arg3: "Exclude Directories"
#
# Environment Settings override Defaults.
# Command arguments override Environment Settings.

# Default location for content addressed file descriptors.
MD5UCFS=${1:-${MD5UCFS:-'/tmpfs/ucfs'}}

# Directory paths never to list or enter
declare -a \
  EXCLUDE_PATHS=${2:-${EXCLUDE_PATHS:-'(/proc /dev /devfs /tmpfs)'}}

# Directories never to list or enter
declare -a \
  EXCLUDE_DIRS=${3:-${EXCLUDE_DIRS:-'(ucfs lost+found tmp wtmp)'}}

# Files never to list or enter
declare -a \
  EXCLUDE_FILES=${3:-${EXCLUDE_FILES:-'(core "Name with Spaces")'}}


# Here document used as a comment block.
: <<LSfieldsDoc
# # # # # List Filesystem Directory Information # # # # #
#
#	ListDirectory "FileGlob" "Field-Array-Name"
# or
#	ListDirectory -of "FileGlob" "Field-Array-Filename"
#	'-of' meaning 'output to filename'
# # # # #

String format description based on: ls (GNU fileutils) version 4.0.36

Produces a line (or more) formatted:
inode permissions hard-links owner group ...
32736 -rw-------    1 mszick   mszick

size    day month date hh:mm:ss year path
2756608 Sun Apr 20 08:53:06 2003 /home/mszick/core

Unless it is formatted:
inode permissions hard-links owner group ...
266705 crw-rw----    1    root  uucp

major minor day month date hh:mm:ss year path
4,  68 Sun Apr 20 09:27:33 2003 /dev/ttyS4
NOTE: that pesky comma after the major number

NOTE: the 'path' may be multiple fields:
/home/mszick/core
/proc/982/fd/0 -> /dev/null
/proc/982/fd/1 -> /home/mszick/.xsession-errors
/proc/982/fd/13 -> /tmp/tmpfZVVOCs (deleted)
/proc/982/fd/7 -> /tmp/kde-mszick/ksycoca
/proc/982/fd/8 -> socket:[11586]
/proc/982/fd/9 -> pipe:[11588]

If that isn't enough to keep your parser guessing,
either or both of the path components may be relative:
../Built-Shared -> Built-Static
../linux-2.4.20.tar.bz2 -> ../../../SRCS/linux-2.4.20.tar.bz2

The first character of the 11 (10?) character permissions field:
's' Socket
'd' Directory
'b' Block device
'c' Character device
'l' Symbolic link
NOTE: Hard links not marked - test for identical inode numbers
on identical filesystems.
All information about hard linked files are shared, except
for the names and the name's location in the directory system.
NOTE: A "Hard link" is known as a "File Alias" on some systems.
'-' An undistingushed file

Followed by three groups of letters for: User, Group, Others
Character 1: '-' Not readable; 'r' Readable
Character 2: '-' Not writable; 'w' Writable
Character 3, User and Group: Combined execute and special
'-' Not Executable, Not Special
'x' Executable, Not Special
's' Executable, Special
'S' Not Executable, Special
Character 3, Others: Combined execute and sticky (tacky?)
'-' Not Executable, Not Tacky
'x' Executable, Not Tacky
't' Executable, Tacky
'T' Not Executable, Tacky

Followed by an access indicator
Haven't tested this one, it may be the eleventh character
or it may generate another field
' ' No alternate access
'+' Alternate access
LSfieldsDoc


ListDirectory()
{
	local -a T
	local -i of=0		# Default return in variable
#	OLD_IFS=$IFS		# Using BASH default ' \t\n'

	case "$#" in
	3)	case "$1" in
		-of)	of=1 ; shift ;;
		 * )	return 1 ;;
		esac ;;
	2)	: ;;		# Poor man's "continue"
	*)	return 1 ;;
	esac

	# NOTE: the (ls) command is NOT quoted (")
	T=( $(ls --inode --ignore-backups --almost-all --directory \
	--full-time --color=none --time=status --sort=none \
	--format=long $1) )

	case $of in
	# Assign T back to the array whose name was passed as $2
		0) eval $2=\( \"\$\{T\[@\]\}\" \) ;;
	# Write T into filename passed as $2
		1) echo "${T[@]}" > "$2" ;;
	esac
	return 0
   }

# # # # # Is that string a legal number? # # # # #
#
#	IsNumber "Var"
# # # # # There has to be a better way, sigh...

IsNumber()
{
	local -i int
	if [ $# -eq 0 ]
	then
		return 1
	else
		(let int=$1)  2>/dev/null
		return $?	# Exit status of the let thread
	fi
}

# # # # # Index Filesystem Directory Information # # # # #
#
#	IndexList "Field-Array-Name" "Index-Array-Name"
# or
#	IndexList -if Field-Array-Filename Index-Array-Name
#	IndexList -of Field-Array-Name Index-Array-Filename
#	IndexList -if -of Field-Array-Filename Index-Array-Filename
# # # # #

: <<IndexListDoc
Walk an array of directory fields produced by ListDirectory

Having suppressed the line breaks in an otherwise line oriented
report, build an index to the array element which starts each line.

Each line gets two index entries, the first element of each line
(inode) and the element that holds the pathname of the file.

The first index entry pair (Line-Number==0) are informational:
Index-Array-Name[0] : Number of "Lines" indexed
Index-Array-Name[1] : "Current Line" pointer into Index-Array-Name

The following index pairs (if any) hold element indexes into
the Field-Array-Name per:
Index-Array-Name[Line-Number * 2] : The "inode" field element.
NOTE: This distance may be either +11 or +12 elements.
Index-Array-Name[(Line-Number * 2) + 1] : The "pathname" element.
NOTE: This distance may be a variable number of elements.
Next line index pair for Line-Number+1.
IndexListDoc



IndexList()
{
	local -a LIST			# Local of listname passed
	local -a -i INDEX=( 0 0 )	# Local of index to return
	local -i Lidx Lcnt
	local -i if=0 of=0		# Default to variable names

	case "$#" in			# Simplistic option testing
		0) return 1 ;;
		1) return 1 ;;
		2) : ;;			# Poor man's continue
		3) case "$1" in
			-if) if=1 ;;
			-of) of=1 ;;
			 * ) return 1 ;;
		   esac ; shift ;;
		4) if=1 ; of=1 ; shift ; shift ;;
		*) return 1
	esac

	# Make local copy of list
	case "$if" in
		0) eval LIST=\( \"\$\{$1\[@\]\}\" \) ;;
		1) LIST=( $(cat $1) ) ;;
	esac

	# Grok (grope?) the array
	Lcnt=${#LIST[@]}
	Lidx=0
	until (( Lidx >= Lcnt ))
	do
	if IsNumber ${LIST[$Lidx]}
	then
		local -i inode name
		local ft
		inode=Lidx
		local m=${LIST[$Lidx+2]}	# Hard Links field
		ft=${LIST[$Lidx+1]:0:1} 	# Fast-Stat
		case $ft in
		b)	((Lidx+=12)) ;;		# Block device
		c)	((Lidx+=12)) ;;		# Character device
		*)	((Lidx+=11)) ;;		# Anything else
		esac
		name=Lidx
		case $ft in
		-)	((Lidx+=1)) ;;		# The easy one
		b)	((Lidx+=1)) ;;		# Block device
		c)	((Lidx+=1)) ;;		# Character device
		d)	((Lidx+=1)) ;;		# The other easy one
		l)	((Lidx+=3)) ;;		# At LEAST two more fields
#  A little more elegance here would handle pipes,
#+ sockets, deleted files - later.
		*)	until IsNumber ${LIST[$Lidx]} || ((Lidx >= Lcnt))
			do
				((Lidx+=1))
			done
			;;			# Not required
		esac
		INDEX[${#INDEX[*]}]=$inode
		INDEX[${#INDEX[*]}]=$name
		INDEX[0]=${INDEX[0]}+1		# One more "line" found
# echo "Line: ${INDEX[0]} Type: $ft Links: $m Inode: \
# ${LIST[$inode]} Name: ${LIST[$name]}"

	else
		((Lidx+=1))
	fi
	done
	case "$of" in
		0) eval $2=\( \"\$\{INDEX\[@\]\}\" \) ;;
		1) echo "${INDEX[@]}" > "$2" ;;
	esac
	return 0				# What could go wrong?
}

# # # # # Content Identify File # # # # #
#
#	DigestFile Input-Array-Name Digest-Array-Name
# or
#	DigestFile -if Input-FileName Digest-Array-Name
# # # # #

# Here document used as a comment block.
: <<DigestFilesDoc

The key (no pun intended) to a Unified Content File System (UCFS)
is to distinguish the files in the system based on their content.
Distinguishing files by their name is just, so, 20th Century.

The content is distinguished by computing a checksum of that content.
This version uses the md5sum program to generate a 128 bit checksum
representative of the file's contents.
There is a chance that two files having different content might
generate the same checksum using md5sum (or any checksum).  Should
that become a problem, then the use of md5sum can be replace by a
cyrptographic signature.  But until then...

The md5sum program is documented as outputting three fields (and it
does), but when read it appears as two fields (array elements).  This
is caused by the lack of whitespace between the second and third field.
So this function gropes the md5sum output and returns:
	[0]	32 character checksum in hexidecimal (UCFS filename)
	[1]	Single character: ' ' text file, '*' binary file
	[2]	Filesystem (20th Century Style) name
	Note: That name may be the character '-' indicating STDIN read.

DigestFilesDoc



DigestFile()
{
	local if=0		# Default, variable name
	local -a T1 T2

	case "$#" in
	3)	case "$1" in
		-if)	if=1 ; shift ;;
		 * )	return 1 ;;
		esac ;;
	2)	: ;;		# Poor man's "continue"
	*)	return 1 ;;
	esac

	case $if in
	0) eval T1=\( \"\$\{$1\[@\]\}\" \)
	   T2=( $(echo ${T1[@]} | md5sum -) )
	   ;;
	1) T2=( $(md5sum $1) )
	   ;;
	esac

	case ${#T2[@]} in
	0) return 1 ;;
	1) return 1 ;;
	2) case ${T2[1]:0:1} in		# SanScrit-2.0.5
	   \*) T2[${#T2[@]}]=${T2[1]:1}
	       T2[1]=\*
	       ;;
	    *) T2[${#T2[@]}]=${T2[1]}
	       T2[1]=" "
	       ;;
	   esac
	   ;;
	3) : ;; # Assume it worked
	*) return 1 ;;
	esac

	local -i len=${#T2[0]}
	if [ $len -ne 32 ] ; then return 1 ; fi
	eval $2=\( \"\$\{T2\[@\]\}\" \)
}

# # # # # Locate File # # # # #
#
#	LocateFile [-l] FileName Location-Array-Name
# or
#	LocateFile [-l] -of FileName Location-Array-FileName
# # # # #

# A file location is Filesystem-id and inode-number

# Here document used as a comment block.
: <<StatFieldsDoc
	Based on stat, version 2.2
	stat -t and stat -lt fields
	[0]	name
	[1]	Total size
		File - number of bytes
		Symbolic link - string length of pathname
	[2]	Number of (512 byte) blocks allocated
	[3]	File type and Access rights (hex)
	[4]	User ID of owner
	[5]	Group ID of owner
	[6]	Device number
	[7]	Inode number
	[8]	Number of hard links
	[9]	Device type (if inode device) Major
	[10]	Device type (if inode device) Minor
	[11]	Time of last access
		May be disabled in 'mount' with noatime
		atime of files changed by exec, read, pipe, utime, mknod (mmap?)
		atime of directories changed by addition/deletion of files
	[12]	Time of last modification
		mtime of files changed by write, truncate, utime, mknod
		mtime of directories changed by addtition/deletion of files
	[13]	Time of last change
		ctime reflects time of changed inode information (owner, group
		permissions, link count
-*-*- Per:
	Return code: 0
	Size of array: 14
	Contents of array
	Element 0: /home/mszick
	Element 1: 4096
	Element 2: 8
	Element 3: 41e8
	Element 4: 500
	Element 5: 500
	Element 6: 303
	Element 7: 32385
	Element 8: 22
	Element 9: 0
	Element 10: 0
	Element 11: 1051221030
	Element 12: 1051214068
	Element 13: 1051214068

	For a link in the form of linkname -> realname
	stat -t  linkname returns the linkname (link) information
	stat -lt linkname returns the realname information

	stat -tf and stat -ltf fields
	[0]	name
	[1]	ID-0?		# Maybe someday, but Linux stat structure
	[2]	ID-0?		# does not have either LABEL nor UUID
				# fields, currently information must come
				# from file-system specific utilities
	These will be munged into:
	[1]	UUID if possible
	[2]	Volume Label if possible
	Note: 'mount -l' does return the label and could return the UUID

	[3]	Maximum length of filenames
	[4]	Filesystem type
	[5]	Total blocks in the filesystem
	[6]	Free blocks
	[7]	Free blocks for non-root user(s)
	[8]	Block size of the filesystem
	[9]	Total inodes
	[10]	Free inodes

-*-*- Per:
	Return code: 0
	Size of array: 11
	Contents of array
	Element 0: /home/mszick
	Element 1: 0
	Element 2: 0
	Element 3: 255
	Element 4: ef53
	Element 5: 2581445
	Element 6: 2277180
	Element 7: 2146050
	Element 8: 4096
	Element 9: 1311552
	Element 10: 1276425

StatFieldsDoc


#	LocateFile [-l] FileName Location-Array-Name
#	LocateFile [-l] -of FileName Location-Array-FileName

LocateFile()
{
	local -a LOC LOC1 LOC2
	local lk="" of=0

	case "$#" in
	0) return 1 ;;
	1) return 1 ;;
	2) : ;;
	*) while (( "$#" > 2 ))
	   do
	      case "$1" in
	       -l) lk=-1 ;;
	      -of) of=1 ;;
	        *) return 1 ;;
	      esac
	   shift
           done ;;
	esac

# More Sanscrit-2.0.5
      # LOC1=( $(stat -t $lk $1) )
      # LOC2=( $(stat -tf $lk $1) )
      # Uncomment above two lines if system has "stat" command installed.
	LOC=( ${LOC1[@]:0:1} ${LOC1[@]:3:11}
	      ${LOC2[@]:1:2} ${LOC2[@]:4:1} )

	case "$of" in
		0) eval $2=\( \"\$\{LOC\[@\]\}\" \) ;;
		1) echo "${LOC[@]}" > "$2" ;;
	esac
	return 0
# Which yields (if you are lucky, and have "stat" installed)
# -*-*- Location Discriptor -*-*-
#	Return code: 0
#	Size of array: 15
#	Contents of array
#	Element 0: /home/mszick		20th Century name
#	Element 1: 41e8			Type and Permissions
#	Element 2: 500			User
#	Element 3: 500			Group
#	Element 4: 303			Device
#	Element 5: 32385		inode
#	Element 6: 22			Link count
#	Element 7: 0			Device Major
#	Element 8: 0			Device Minor
#	Element 9: 1051224608		Last Access
#	Element 10: 1051214068		Last Modify
#	Element 11: 1051214068		Last Status
#	Element 12: 0			UUID (to be)
#	Element 13: 0			Volume Label (to be)
#	Element 14: ef53		Filesystem type
}



# And then there was some test code

ListArray() # ListArray Name
{
	local -a Ta

	eval Ta=\( \"\$\{$1\[@\]\}\" \)
	echo
	echo "-*-*- List of Array -*-*-"
	echo "Size of array $1: ${#Ta[*]}"
	echo "Contents of array $1:"
	for (( i=0 ; i<${#Ta[*]} ; i++ ))
	do
	    echo -e "\tElement $i: ${Ta[$i]}"
	done
	return 0
}

declare -a CUR_DIR
# For small arrays
ListDirectory "${PWD}" CUR_DIR
ListArray CUR_DIR

declare -a DIR_DIG
DigestFile CUR_DIR DIR_DIG
echo "The new \"name\" (checksum) for ${CUR_DIR[9]} is ${DIR_DIG[0]}"

declare -a DIR_ENT
# BIG_DIR # For really big arrays - use a temporary file in ramdisk
# BIG-DIR # ListDirectory -of "${CUR_DIR[11]}/*" "/tmpfs/junk2"
ListDirectory "${CUR_DIR[11]}/*" DIR_ENT

declare -a DIR_IDX
# BIG-DIR # IndexList -if "/tmpfs/junk2" DIR_IDX
IndexList DIR_ENT DIR_IDX

declare -a IDX_DIG
# BIG-DIR # DIR_ENT=( $(cat /tmpfs/junk2) )
# BIG-DIR # DigestFile -if /tmpfs/junk2 IDX_DIG
DigestFile DIR_ENT IDX_DIG
# Small (should) be able to parallize IndexList & DigestFile
# Large (should) be able to parallize IndexList & DigestFile & the assignment
echo "The \"name\" (checksum) for the contents of ${PWD} is ${IDX_DIG[0]}"

declare -a FILE_LOC
LocateFile ${PWD} FILE_LOC
ListArray FILE_LOC

exit 0

Stéphane Chazelas dà un esempio di programmazione object-oriented con uno script Bash.

Esempio A-20. Database object-oriented

#!/bin/bash
# obj-oriented.sh: programmazione object-oriented in uno script di shell.
# Script di Stephane Chazelas.


#  Nota importante:
#  ---- ----------
#  Se eseguite lo script con la versione 3 o successive di Bash,
#+ sostituite tutti i punti presenti nei nomi delle funzioni con un carattere  
#+ "consentito", ad esempio il trattino di sottolineatura (underscore).


persona.new()        # Assomiglia quasi ad una dichiarazione di classe in C++.
{
  local obj_nome=$1 cognome=$2 nome=$3 datanascita=$4

  eval "$obj_nome.set_cognome() {
          eval \"$obj_nome.get_cognome() {
                   echo \$1
                 }\"
        }"

  eval "$obj_nome.set_nome() {
          eval \"$obj_nome.get_nome() {
                   echo \$1
                 }\"
        }"

  eval "$obj_nome.set_datanascita() {
          eval \"$obj_nome.get_datanascita() {
            echo \$1
          }\"
          eval \"$obj_nome.show_datanascita() {
            echo \$(date -d \"1/1/1970 0:0:\$1 GMT\")
          }\"
          eval \"$obj_nome.get_eta() {
            echo \$(( (\$(date +%s) - \$1) / 3600 / 24 / 365 ))
          }\"
        }"

  $obj_nome.set_cognome $cognome
  $obj_nome.set_nome $nome
  $obj_nome.set_datanascita $datanascita
}

echo

persona.new self Bozeman Bozo 101272413
#  Crea un'instance di "persona.new" 
#+ (in realtà passa gli argomenti alla funzione).

self.get_nome              #   Bozo
self.get_cognome           #   Bozeman
self.get_eta               #   28
self.get_datanascita       #   101272413
self.show_datanascita      #   Sat Mar 17 20:13:33 MST 1973

echo

#  typeset -f
#+ per vedere le funzioni create (attenzione, visualizzazione su più pagine).

exit 0

Mariusz Gniazdowski ha fornito la seguente libreria hash da usare negli script.

Esempio A-21. Libreria di funzioni hash

# Hash:
# Hash function library
# Author: Mariusz Gniazdowski <mgniazd-at-gmail.com>
# Date: 2005-04-07

# Functions making emulating hashes in Bash a little less painful.


#    Limitations:
#  * Only global variables are supported.
#  * Each hash instance generates one global variable per value.
#  * Variable names collisions are possible
#+   if you define variable like __hash__hashname_key
#  * Keys must use chars that can be part of a Bash variable name
#+   (no dashes, periods, etc.).
#  * The hash is created as a variable:
#    ... hashname_keyname
#    So if somone will create hashes like:
#      myhash_ + mykey = myhash__mykey
#      myhash + _mykey = myhash__mykey
#    Then there will be a collision.
#    (This should not pose a major problem.)


Hash_config_varname_prefix=__hash__


# Emulates:  hash[key]=value
#
# Params:
# 1 - hash
# 2 - key
# 3 - value
function hash_set {
	eval "${Hash_config_varname_prefix}${1}_${2}=\"${3}\""
}


# Emulates:  value=hash[key]
#
# Params:
# 1 - hash
# 2 - key
# 3 - value (name of global variable to set)
function hash_get_into {
	eval "$3=\"\$${Hash_config_varname_prefix}${1}_${2}\""
}


# Emulates:  echo hash[key]
#
# Params:
# 1 - hash
# 2 - key
# 3 - echo params (like -n, for example)
function hash_echo {
	eval "echo $3 \"\$${Hash_config_varname_prefix}${1}_${2}\""
}


# Emulates:  hash1[key1]=hash2[key2]
#
# Params:
# 1 - hash1
# 2 - key1
# 3 - hash2
# 4 - key2
function hash_copy {
eval "${Hash_config_varname_prefix}${1}_${2}\
=\"\$${Hash_config_varname_prefix}${3}_${4}\""
}


# Emulates:  hash[keyN-1]=hash[key2]=...hash[key1]
#
# Copies first key to rest of keys.
#
# Params:
# 1 - hash1
# 2 - key1
# 3 - key2
# . . .
# N - keyN
function hash_dup {
  local hashName="$1" keyName="$2"
  shift 2
  until [ ${#} -le 0 ]; do
    eval "${Hash_config_varname_prefix}${hashName}_${1}\
=\"\$${Hash_config_varname_prefix}${hashName}_${keyName}\""
  shift;
  done;
}


# Emulates:  unset hash[key]
#
# Params:
# 1 - hash
# 2 - key
function hash_unset {
	eval "unset ${Hash_config_varname_prefix}${1}_${2}"
}


# Emulates something similar to:  ref=&hash[key]
#
# The reference is name of the variable in which value is held.
#
# Params:
# 1 - hash
# 2 - key
# 3 - ref - Name of global variable to set.
function hash_get_ref_into {
	eval "$3=\"${Hash_config_varname_prefix}${1}_${2}\""
}


# Emulates something similar to:  echo &hash[key]
#
# That reference is name of variable in which value is held.
#
# Params:
# 1 - hash
# 2 - key
# 3 - echo params (like -n for example)
function hash_echo_ref {
	eval "echo $3 \"${Hash_config_varname_prefix}${1}_${2}\""
}



# Emulates something similar to:  $$hash[key](param1, param2, ...)
#
# Params:
# 1 - hash
# 2 - key
# 3,4, ... - Function parameters
function hash_call {
  local hash key
  hash=$1
  key=$2
  shift 2
  eval "eval \"\$${Hash_config_varname_prefix}${hash}_${key} \\\"\\\$@\\\"\""
}


# Emulates something similar to:  isset(hash[key]) or hash[key]==NULL
#
# Params:
# 1 - hash
# 2 - key
# Returns:
# 0 - there is such key
# 1 - there is no such key
function hash_is_set {
  eval "if [[ \"\${${Hash_config_varname_prefix}${1}_${2}-a}\" = \"a\" && 
  \"\${${Hash_config_varname_prefix}${1}_${2}-b}\" = \"b\" ]]
    then return 1; else return 0; fi"
}


# Emulates something similar to:
#   foreach($hash as $key => $value) { fun($key,$value); }
#
# It is possible to write different variations of this function.
# Here we use a function call to make it as "generic" as possible.
#
# Params:
# 1 - hash
# 2 - function name
function hash_foreach {
  local keyname oldIFS="$IFS"
  IFS=' '
  for i in $(eval "echo \${!${Hash_config_varname_prefix}${1}_*}"); do
    keyname=$(eval "echo \${i##${Hash_config_varname_prefix}${1}_}")
    eval "$2 $keyname \"\$$i\""
  done
IFS="$oldIFS"
}

#  NOTE: In lines 103 and 116, ampersand changed.
#  But, it doesn't matter, because these are comment lines anyhow.

Ecco un script che utilizza la precedente libreria.

Esempio A-22. Colorare del testo con le funzioni di hash

#!/bin/bash
# hash-example.sh: Colorizing text.
# Author: Mariusz Gniazdowski <mgniazd-at-gmail.com>

. Hash.lib      # Load the library of functions.

hash_set colors red          "\033[0;31m"
hash_set colors blue         "\033[0;34m"
hash_set colors light_blue   "\033[1;34m"
hash_set colors light_red    "\033[1;31m"
hash_set colors cyan         "\033[0;36m"
hash_set colors light_green  "\033[1;32m"
hash_set colors light_gray   "\033[0;37m"
hash_set colors green        "\033[0;32m"
hash_set colors yellow       "\033[1;33m"
hash_set colors light_purple "\033[1;35m"
hash_set colors purple       "\033[0;35m"
hash_set colors reset_color  "\033[0;00m"


# $1 - keyname
# $2 - value
try_colors() {
	echo -en "$2"
	echo "This line is $1."
}
hash_foreach colors try_colors
hash_echo colors reset_color -en

echo -e '\nLet us overwrite some colors with yellow.\n'
# It's hard to read yellow text on some terminals.
hash_dup colors yellow   red light_green blue green light_gray cyan
hash_foreach colors try_colors
hash_echo colors reset_color -en

echo -e '\nLet us delete them and try colors once more . . .\n'

for i in red light_green blue green light_gray cyan; do
	hash_unset colors $i
done
hash_foreach colors try_colors
hash_echo colors reset_color -en

hash_set other txt "Other examples . . ."
hash_echo other txt
hash_get_into other txt text
echo $text

hash_set other my_fun try_colors
hash_call other my_fun   purple "`hash_echo colors purple`"
hash_echo colors reset_color -en

echo; echo "Back to normal?"; echo

exit $?

#  On some terminals, the "light" colors print in bold,
#  and end up looking darker than the normal ones.
#  Why is this?

Esempio che illustra la tecnica del hashing, ma da un punto di vista differente.

Esempio A-23. Altro sulle funzioni di hash

#!/bin/bash
# $Id: ha.sh,v 1.2 2005/04/21 23:24:26 oliver Exp $
# Copyright 2005 Oliver Beckstein
# Rilasciato sotto la GNU Public License
# L'autore dello script ha dato il permesso per l'inserimento in Guida ASB.
# (Grazie!)

#----------------------------------------------------------------
# pseudo hash basato sull'espansione indiretta di parametro
# API: access through functions (accesso per mezzo di funzioni):
# 
# crea lo  hash:
#  
#      nuovohash Amanti
#
# aggiunge una voce (notate gli apici singoli per gli spazi)
#    
#      agghash Amanti Tristano Isotta
#      agghash Amanti 'Romeo Montecchi' 'Giulietta Capuleti'
#
# accesso per chiave
#
#      usahash Amanti Tristano   ---->  Isotta
#
# mostra tutte le chiavi
#
#      chiavihash Amanti         ----> 'Tristano'  'Romeo Montecchi'
#
#
# convenzione: al posto della sintassi di perl' foo{bar} = boing',
# usiamo
#       '_foo_bar=boing' (due trattini di sottolineatura, nessuno spazio)
#
# 1) registriamo la chiave in _NOME_chiavi[]
# 2) registriamo il valore in _NOME_valori[] usando lo stesso indice
# L'indice dell'ultimo inserimento è _NOME_ptr
#
# NOTA: Nessun controllo di errori o funzionalità, solo essenza.


function _inihash () {
    # funzione privata
    # richiamata all'inizio di ogni procedura
    # definisce: _chiavi _valori _ptr
    #
    # uso: _inihash NOME
    local nome=$1
    _chiavi=_${nome}_chiavi
    _valori=_${nome}_valori
    _ptr=_${nome}_ptr
}

function nuovohash () {
    # uso: nuovohash NOME
    #      NOME non deve contenere spazi o '.';
    #      insomma: deve essere un nome valido per una variabile bash
    # Ci si basa sul fatto che bash riconosca automaticamente gli array.
    local nome=$1 
    local _chiavi _valori _ptr
    _inihash ${nome}
    eval ${_ptr}=0
}


function agghash () {
    # uso: agghash NOME CHIAVE 'VALORE con spazi'
    #      occore usare gli apici singoli per gli argomenti contenenti spazi ''
    local nome=$1 c="$2" v="$3" 
    local _chiavi _valori _ptr
    _inihash ${nome}

    #echo "DEBUG(agghash): ${_ptr}=${!_ptr}"

    eval let ${_ptr}=${_ptr}+1
    eval "$_chiavi[${!_ptr}]=\"${c}\""
    eval "$_valori[${!_ptr}]=\"${v}\""
}

function usahash () {
    # uso: usahash NOME CHIAVE
    #      restituisce l'exit status
    #      ERR=0 se viene trovata la voce, altrimenti 1 
    #  Non si tratta di un vero hash---è semplicemente di una ricerca
    #+ lineare per mezzo delle chiavi
    local nome=$1 chiave="$2" 
    local _chiavi _valori _ptr 
    local c v i trovato h
    _inihash ${nome}
    
    # _ptr contiene l'indice più altro del hash
    trovato=0

    for i in $(seq 1 ${!_ptr}); do
	h="\${${_chiavi}[${i}]}"  # più sicuro eseguirlo in due fasi
	eval c=${h}            # (specialmente se c'è il quoting per gli spazi)
	if [ "${c}" = "${chiave}" ]; then trovato=1; break; fi
    done;

    [ ${trovato} = 0 ] && return 1;
    # altrimenti: i è l'indice che verifica la chiave
    h="\${${_valori}[${i}]}"
    eval echo "${h}"
    return 0;	
}

function chiavihash () {
    # uso: chiavihash NOME
    # restituisce l'elenco delle chiavi definite per hash
    local nome=$1 chiave="$2" 
    local _chiavi _valori _ptr 
    local c i h
    _inihash ${nome}
    
    # _ptr contiene l'indice più alto del hash
    for i in $(seq 1 ${!_ptr}); do
	h="\${${_chiavi}[${i}]}"   # più sicuro eseguirlo in due fasi
	eval c=${h}            # (specialmente se c'è il quoting per gli spazi)
	echo -n "'${c}' "
    done;
}


# --------------------------------------------------------------------

# Ora proviamolo.
# (In base ai commenti all'inizio dello script.)
nuovohash Amanti
agghash Amanti Tristano Isotta
agghash Amanti 'Romeo Montecchi' 'Giulietta Capuleti'

# Risultati.
echo
usahash Amanti Tristano      # Isotta
echo
chiavihash Amanti            # 'Tristano' 'Romeo Montecchi'
echo; echo


exit 0

# Esercizio: aggiungete il controllo degli errori alle funzioni.

Ora uno script che installa e monta quelle graziose "chiavi" USB.

Esempio A-24. Montare le chiavi di memoria USB

#!/bin/bash
# ==> usb.sh
# ==> Script for mounting and installing pen/keychain USB storage devices.
# ==> Runs as root at system startup (see below).
# ==>
# ==> Newer Linux distros (2004 or later) autodetect
# ==> and install USB pen drives, and therefore don't need this script.
# ==> But, it's still instructive.
 
#  This code is free software covered by GNU GPL license version 2 or above.
#  Please refer to http://www.gnu.org/ for the full license text.
#
#  Some code lifted from usb-mount by Michael Hamilton's usb-mount (LGPL)
#+ see http://users.actrix.co.nz/michael/usbmount.html
#
#  INSTALL
#  -------
#  Put this in /etc/hotplug/usb/diskonkey.
#  Then look in /etc/hotplug/usb.distmap, and copy all usb-storage entries
#+ into /etc/hotplug/usb.usermap, substituting "usb-storage" for "diskonkey".
#  Otherwise this code is only run during the kernel module invocation/removal
#+ (at least in my tests), which defeats the purpose.
#
#  TODO
#  ----
#  Handle more than one diskonkey device at one time (e.g. /dev/diskonkey1
#+ and /mnt/diskonkey1), etc. The biggest problem here is the handling in
#+ devlabel, which I haven't yet tried.
#
#  AUTHOR and SUPPORT
#  ------------------
#  Konstantin Riabitsev, <icon linux duke edu>.
#  Send any problem reports to my email address at the moment.
#
# ==> Comments added by ABS Guide author.



SYMLINKDEV=/dev/diskonkey
MOUNTPOINT=/mnt/diskonkey
DEVLABEL=/sbin/devlabel
DEVLABELCONFIG=/etc/sysconfig/devlabel
IAM=$0

##
# Functions lifted near-verbatim from usb-mount code.
#
function allAttachedScsiUsb {
  find /proc/scsi/ -path '/proc/scsi/usb-storage*' -type f |
  xargs grep -l 'Attached: Yes'
}
function scsiDevFromScsiUsb {
  echo $1 | awk -F"[-/]" '{ n=$(NF-1);
  print "/dev/sd" substr("abcdefghijklmnopqrstuvwxyz", n+1, 1) }'
}

if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]; then
    ##
    # lifted from usbcam code.
    #
    if [ -f /var/run/console.lock ]; then
        CONSOLEOWNER=`cat /var/run/console.lock`
    elif [ -f /var/lock/console.lock ]; then
        CONSOLEOWNER=`cat /var/lock/console.lock`
    else
        CONSOLEOWNER=
    fi
    for procEntry in $(allAttachedScsiUsb); do
        scsiDev=$(scsiDevFromScsiUsb $procEntry)
        #  Some bug with usb-storage?
        #  Partitions are not in /proc/partitions until they are accessed
        #+ somehow.
        /sbin/fdisk -l $scsiDev >/dev/null
        ##
        #  Most devices have partitioning info, so the data would be on
        #+ /dev/sd?1. However, some stupider ones don't have any partitioning
        #+ and use the entire device for data storage. This tries to
        #+ guess semi-intelligently if we have a /dev/sd?1 and if not, then
        #+ it uses the entire device and hopes for the better.
        #
        if grep -q `basename $scsiDev`1 /proc/partitions; then
            part="$scsiDev""1"
        else
            part=$scsiDev
        fi
        ##
        #  Change ownership of the partition to the console user so they can
        #+ mount it.
        #
        if [ ! -z "$CONSOLEOWNER" ]; then
            chown $CONSOLEOWNER:disk $part
        fi
        ##
        # This checks if we already have this UUID defined with devlabel.
        # If not, it then adds the device to the list.
        #
        prodid=`$DEVLABEL printid -d $part`
        if ! grep -q $prodid $DEVLABELCONFIG; then
            # cross our fingers and hope it works
            $DEVLABEL add -d $part -s $SYMLINKDEV 2>/dev/null
        fi
        ##
        # Check if the mount point exists and create if it doesn't.
        #
        if [ ! -e $MOUNTPOINT ]; then
            mkdir -p $MOUNTPOINT
        fi
        ##
        # Take care of /etc/fstab so mounting is easy.
        #
        if ! grep -q "^$SYMLINKDEV" /etc/fstab; then
            # Add an fstab entry
            echo -e \
                "$SYMLINKDEV\t\t$MOUNTPOINT\t\tauto\tnoauto,owner,kudzu 0 0" \
                >> /etc/fstab
        fi
    done
    if [ ! -z "$REMOVER" ]; then
        ##
        # Make sure this script is triggered on device removal.
        #
        mkdir -p `dirname $REMOVER`
        ln -s $IAM $REMOVER
    fi
elif [ "${ACTION}" = "remove" ]; then
    ##
    # If the device is mounted, unmount it cleanly.
    #
    if grep -q "$MOUNTPOINT" /etc/mtab; then
        # unmount cleanly
        umount -l $MOUNTPOINT
    fi
    ##
    # Remove it from /etc/fstab if it's there.
    #
    if grep -q "^$SYMLINKDEV" /etc/fstab; then
        grep -v "^$SYMLINKDEV" /etc/fstab > /etc/.fstab.new
        mv -f /etc/.fstab.new /etc/fstab
    fi
fi

exit 0

Ecco qualcosa che riscalderà i cuori di webmaster e insegnanti di ogni dove: uno script che salva i weblog.

Esempio A-25. Preservare i weblog

#!/bin/bash
# archiveweblogs.sh v1.0

# Troy Engel <tengel@fluid.com>
# Con leggere modifiche effettuate dall'autore del libro.
# Usato con il consenso dell'autore dello script.
#
#  Lo scrip salva i weblog di un'istallazione di default RedHat/Apache
#+ che normalmente vengono ruotati ed eliminati.
#  I file verranno salvati in una directory data con data/ora inserita 
#+ nel nome del file, e compressi con bzip2.
#
#  Lo scrip va eseguito da crontab a notte fonda,
#+ perch´ bzip2 è particolarmente avido di risorse di CPU:
#  0 2 * * * /opt/sbin/archiveweblogs.sh


PROBLEMA=66

# Impostatela alla vostra directory di backup.
BKP_DIR=/opt/backups/weblogs

# Impostazioni di default Apache/RedHat
LOG_DAYS="4 3 2 1"
LOG_DIR=/var/log/httpd
LOG_FILES="access_log error_log"

# Collocazione di default dei programmi in RedHat
LS=/bin/ls
MV=/bin/mv
ID=/usr/bin/id
CUT=/bin/cut
COL=/usr/bin/column
BZ2=/usr/bin/bzip2

# Siete root?
UTENTE=`$ID -u`
if [ "X$UTENTE" != "X0" ]; then
  echo "PANIC: solo root può eseguire lo script!"
  exit $PROBLEMA
fi

# La directory di backup esiste/ha i permessi di scrittura?
if [ ! -x $BKP_DIR ]; then
  echo "PANIC: $BKP_DIR non esiste o non ha i permessi di scrittura!"
  exit $PROBLEMA
fi

# Sposta, rinomina e comprime con bzip2 i log
for logday in $LOG_DAYS; do
  for logfile in $LOG_FILES; do
    MIOFILE="$LOG_DIR/$logfile.$logday"
    if [ -w $MIOFILE ]; then
      DTS=`$LS -lgo --time-style=+%Y%m%d $MIOFILE | $COL -t | $CUT -d ' ' -f7`
      $MV $MIOFILE $BKP_DIR/$logfile.$DTS
      $BZ2 $BKP_DIR/$logfile.$DTS
    else
      #  L'errore viene visualizzato solo se il file esiste 
      #+ (ergo: non ha i permessi di scrittura).
      if [ -f $MIOFILE ]; then
        echo "ERRORE: $MIOFILE non ha i permessi di scrittura. Abbandono."
      fi
    fi
  done
done

exit 0

Come impedire alla shell di espandere e reinterpretare le stringhe?

Esempio A-26. Proteggere le stringhe letterali

#! /bin/bash
# protect_literal.sh

# set -vx

:<<-'_Protect_Literal_String_Doc'

    Copyright (c) Michael S. Zick, 2003; All Rights Reserved
    License: Unrestricted reuse in any form, for any purpose.
    Warranty: None
    Revision: $ID$

    Documentation redirected to the Bash no-operation.
    Bash will '/dev/null' this block when the script is first read.
    (Uncomment the above set command to see this action.)

    Remove the first (Sha-Bang) line when sourcing this as a library
    procedure.  Also comment out the example use code in the two
    places where shown.


    Usage:
        _protect_literal_str 'Whatever string meets your ${fancy}'
        Just echos the argument to standard out, hard quotes
        restored.

        $(_protect_literal_str 'Whatever string meets your ${fancy}')
        as the right-hand-side of an assignment statement.

    Does:
        As the right-hand-side of an assignment, preserves the
        hard quotes protecting the contents of the literal during
        assignment.

    Notes:
        The strange names (_*) are used to avoid trampling on
        the user's chosen names when this is sourced as a
        library.

_Protect_Literal_String_Doc

# The 'for illustration' function form

_protect_literal_str() {

# Pick an un-used, non-printing character as local IFS.
# Not required, but shows that we are ignoring it.
    local IFS=$'\x1B'               # \ESC character

# Enclose the All-Elements-Of in hard quotes during assignment.
    local tmp=$'\x27'$@$'\x27'
#    local tmp=$'\''$@$'\''         # Even uglier.

    local len=${#tmp}               # Info only.
    echo $tmp is $len long.         # Output AND information.
}

# This is the short-named version.
_pls() {
    local IFS=$'x1B'                # \ESC character (not required)
    echo $'\x27'$@$'\x27'           # Hard quoted parameter glob
}

# :<<-'_Protect_Literal_String_Test'
# # # Remove the above "# " to disable this code. # # #

# See how that looks when printed.
echo
echo "- - Test One - -"
_protect_literal_str 'Hello $user'
_protect_literal_str 'Hello "${username}"'
echo

# Which yields:
# - - Test One - -
# 'Hello $user' is 13 long.
# 'Hello "${username}"' is 21 long.

#  Looks as expected, but why all of the trouble?
#  The difference is hidden inside the Bash internal order
#+ of operations.
#  Which shows when you use it on the RHS of an assignment.

# Declare an array for test values.
declare -a arrayZ

# Assign elements with various types of quotes and escapes.
arrayZ=( zero "$(_pls 'Hello ${Me}')" 'Hello ${You}' "\'Pass: ${pw}\'" )

# Now list that array and see what is there.
echo "- - Test Two - -"
for (( i=0 ; i<${#arrayZ[*]} ; i++ ))
do
    echo  Element $i: ${arrayZ[$i]} is: ${#arrayZ[$i]} long.
done
echo

# Which yields:
# - - Test Two - -
# Element 0: zero is: 4 long.           # Our marker element
# Element 1: 'Hello ${Me}' is: 13 long. # Our "$(_pls '...' )"
# Element 2: Hello ${You} is: 12 long.  # Quotes are missing
# Element 3: \'Pass: \' is: 10 long.    # ${pw} expanded to nothing

# Now make an assignment with that result.
declare -a array2=( ${arrayZ[@]} )

# And print what happened.
echo "- - Test Three - -"
for (( i=0 ; i<${#array2[*]} ; i++ ))
do
    echo  Element $i: ${array2[$i]} is: ${#array2[$i]} long.
done
echo

# Which yields:
# - - Test Three - -
# Element 0: zero is: 4 long.           # Our marker element.
# Element 1: Hello ${Me} is: 11 long.   # Intended result.
# Element 2: Hello is: 5 long.          # ${You} expanded to nothing.
# Element 3: 'Pass: is: 6 long.         # Split on the whitespace.
# Element 4: ' is: 1 long.              # The end quote is here now.

#  Our Element 1 has had its leading and trailing hard quotes stripped.
#  Although not shown, leading and trailing whitespace is also stripped.
#  Now that the string contents are set, Bash will always, internally,
#+ hard quote the contents as required during its operations.

#  Why?
#  Considering our "$(_pls 'Hello ${Me}')" construction:
#  " ... " -> Expansion required, strip the quotes.
#  $( ... ) -> Replace with the result of..., strip this.
#  _pls ' ... ' -> called with literal arguments, strip the quotes.
#  The result returned includes hard quotes; BUT the above processing
#+ has already been done, so they become part of the value assigned.
#
#  Similarly, during further usage of the string variable, the ${Me}
#+ is part of the contents (result) and survives any operations
#  (Until explicitly told to evaluate the string).

#  Hint: See what happens when the hard quotes ($'\x27') are replaced
#+ with soft quotes ($'\x22') in the above procedures.
#  Interesting also is to remove the addition of any quoting.

# _Protect_Literal_String_Test
# # # Remove the above "# " to disable this code. # # #

exit 0

E se si volesse che la shell espanda e reinterpreti le stringhe?

Esempio A-27. Stringhe letterali non protette

#! /bin/bash
# unprotect_literal.sh

# set -vx

:<<-'_UnProtect_Literal_String_Doc'

    Copyright (c) Michael S. Zick, 2003; All Rights Reserved
    License: Unrestricted reuse in any form, for any purpose.
    Warranty: None
    Revision: $ID$

    Documentation redirected to the Bash no-operation. Bash will
    '/dev/null' this block when the script is first read.
    (Uncomment the above set command to see this action.)

    Remove the first (Sha-Bang) line when sourcing this as a library
    procedure.  Also comment out the example use code in the two
    places where shown.


    Usage:
        Complement of the "$(_pls 'Literal String')" function.
        (See the protect_literal.sh example.)

        StringVar=$(_upls ProtectedSringVariable)

    Does:
        When used on the right-hand-side of an assignment statement;
        makes the substitions embedded in the protected string.

    Notes:
        The strange names (_*) are used to avoid trampling on
        the user's chosen names when this is sourced as a
        library.


_UnProtect_Literal_String_Doc

_upls() {
    local IFS=$'x1B'                # \ESC character (not required)
    eval echo $@                    # Substitution on the glob.
}

# :<<-'_UnProtect_Literal_String_Test'
# # # Remove the above "# " to disable this code. # # #


_pls() {
    local IFS=$'x1B'                # \ESC character (not required)
    echo $'\x27'$@$'\x27'           # Hard quoted parameter glob
}

# Declare an array for test values.
declare -a arrayZ

# Assign elements with various types of quotes and escapes.
arrayZ=( zero "$(_pls 'Hello ${Me}')" 'Hello ${You}' "\'Pass: ${pw}\'" )

# Now make an assignment with that result.
declare -a array2=( ${arrayZ[@]} )

# Which yielded:
# - - Test Three - -
# Element 0: zero is: 4 long            # Our marker element.
# Element 1: Hello ${Me} is: 11 long    # Intended result.
# Element 2: Hello is: 5 long           # ${You} expanded to nothing.
# Element 3: 'Pass: is: 6 long          # Split on the whitespace.
# Element 4: ' is: 1 long               # The end quote is here now.

# set -vx

#  Initialize 'Me' to something for the embedded ${Me} substitution.
#  This needs to be done ONLY just prior to evaluating the
#+ protected string.
#  (This is why it was protected to begin with.)

Me="to the array guy."

# Set a string variable destination to the result.
newVar=$(_upls ${array2[1]})

# Show what the contents are.
echo $newVar

# Do we really need a function to do this?
newerVar=$(eval echo ${array2[1]})
echo $newerVar

#  I guess not, but the _upls function gives us a place to hang
#+ the documentation on.
#  This helps when we forget what a # construction like:
#+ $(eval echo ... ) means.

# What if Me isn't set when the protected string is evaluated?
unset Me
newestVar=$(_upls ${array2[1]})
echo $newestVar

# Just gone, no hints, no runs, no errors.

#  Why in the world?
#  Setting the contents of a string variable containing character
#+ sequences that have a meaning in Bash is a general problem in
#+ script programming.
#
#  This problem is now solved in eight lines of code
#+ (and four pages of description).

#  Where is all this going?
#  Dynamic content Web pages as an array of Bash strings.
#  Content set per request by a Bash 'eval' command
#+ on the stored page template.
#  Not intended to replace PHP, just an interesting thing to do.
###
#  Don't have a webserver application?
#  No problem, check the example directory of the Bash source;
#+ there is a Bash script for that also.

# _UnProtect_Literal_String_Test
# # # Remove the above "# " to disable this code. # # #

exit 0

Questo è uno script molto potente che aiuta a scovare gli spammer.

Esempio A-28. Identificare uno spammer

#!/bin/bash

# $Id: is_spammer.bash,v 1.12.2.11 2004/10/01 21:42:33 mszick Exp $
# Above line is RCS info.

# The latest version of this script is available from http://www.morethan.org.
#
# Spammer-identification
# by Michael S. Zick
# Used in the ABS Guide with permission.



#######################################################
# Documentation
# See also "Quickstart" at end of script.
#######################################################

:<<-'__is_spammer_Doc_'

    Copyright (c) Michael S. Zick, 2004
    License: Unrestricted reuse in any form, for any purpose.
    Warranty: None -{Its a script; the user is on their own.}-

Impatient?
    Application code: goto "# # # Hunt the Spammer' program code # # #"
    Example output: ":<<-'_is_spammer_outputs_'"
    How to use: Enter script name without arguments.
                Or goto "Quickstart" at end of script.

Provides
    Given a domain name or IP(v4) address as input:

    Does an exhaustive set of queries to find the associated
    network resources (short of recursing into TLDs).

    Checks the IP(v4) addresses found against Blacklist
    nameservers.

    If found to be a blacklisted IP(v4) address,
    reports the blacklist text records.
    (Usually hyper-links to the specific report.)

Requires
    A working Internet connection.
    (Exercise: Add check and/or abort if not on-line when running script.)
    Bash with arrays (2.05b+).

    The external program 'dig' --
    a utility program provided with the 'bind' set of programs.
    Specifically, the version which is part of Bind series 9.x
    See: http://www.isc.org

    All usages of 'dig' are limited to wrapper functions,
    which may be rewritten as required.
    See: dig_wrappers.bash for details.
         ("Additional documentation" -- below)

Usage
    Script requires a single argument, which may be:
    1) A domain name;
    2) An IP(v4) address;
    3) A filename, with one name or address per line.

    Script accepts an optional second argument, which may be:
    1) A Blacklist server name;
    2) A filename, with one Blacklist server name per line.

    If the second argument is not provided, the script uses
    a built-in set of (free) Blacklist servers.

    See also, the Quickstart at the end of this script (after 'exit').

Return Codes
    0 - All OK
    1 - Script failure
    2 - Something is Blacklisted

Optional environment variables
    SPAMMER_TRACE
        If set to a writable file,
        script will log an execution flow trace.

    SPAMMER_DATA
        If set to a writable file, script will dump its
        discovered data in the form of GraphViz file.
        See: http://www.research.att.com/sw/tools/graphviz

    SPAMMER_LIMIT
        Limits the depth of resource tracing.

        Default is 2 levels.

        A setting of 0 (zero) means 'unlimited' . . .
          Caution: script might recurse the whole Internet!

        A limit of 1 or 2 is most useful when processing
        a file of domain names and addresses.
        A higher limit can be useful when hunting spam gangs.


Additional documentation
    Download the archived set of scripts
    explaining and illustrating the function contained within this script.
    http://personal.riverusers.com/mszick_clf.tar.bz2


Study notes
    This script uses a large number of functions.
    Nearly all general functions have their own example script.
    Each of the example scripts have tutorial level comments.

Scripting project
    Add support for IP(v6) addresses.
    IP(v6) addresses are recognized but not processed.

Advanced project
    Add the reverse lookup detail to the discovered information.

    Report the delegation chain and abuse contacts.

    Modify the GraphViz file output to include the
    newly discovered information.

__is_spammer_Doc_

#######################################################




#### Special IFS settings used for string parsing. ####

# Whitespace == :Space:Tab:Line Feed:Carriage Return:
WSP_IFS=$'\x20'$'\x09'$'\x0A'$'\x0D'

# No Whitespace == Line Feed:Carriage Return
NO_WSP=$'\x0A'$'\x0D'

# Field separator for dotted decimal IP addresses
ADR_IFS=${NO_WSP}'.'

# Array to dotted string conversions
DOT_IFS='.'${WSP_IFS}

# # # Pending operations stack machine # # #
# This set of functions described in func_stack.bash.
# (See "Additional documentation" above.)
# # #

# Global stack of pending operations.
declare -f -a _pending_
# Global sentinel for stack runners
declare -i _p_ctrl_
# Global holder for currently executing function
declare -f _pend_current_

# # # Debug version only - remove for regular use # # #
#
# The function stored in _pend_hook_ is called
# immediately before each pending function is
# evaluated.  Stack clean, _pend_current_ set.
#
# This thingy demonstrated in pend_hook.bash.
declare -f _pend_hook_
# # #

# The do nothing function
pend_dummy() { : ; }

# Clear and initialize the function stack.
pend_init() {
    unset _pending_[@]
    pend_func pend_stop_mark
    _pend_hook_='pend_dummy'  # Debug only.
}

# Discard the top function on the stack.
pend_pop() {
    if [ ${#_pending_[@]} -gt 0 ]
    then
        local -i _top_
        _top_=${#_pending_[@]}-1
        unset _pending_[$_top_]
    fi
}

# pend_func function_name [$(printf '%q\n' arguments)]
pend_func() {
    local IFS=${NO_WSP}
    set -f
    _pending_[${#_pending_[@]}]=$@
    set +f
}

# The function which stops the release:
pend_stop_mark() {
    _p_ctrl_=0
}

pend_mark() {
    pend_func pend_stop_mark
}

# Execute functions until 'pend_stop_mark' . . .
pend_release() {
    local -i _top_             # Declare _top_ as integer.
    _p_ctrl_=${#_pending_[@]}
    while [ ${_p_ctrl_} -gt 0 ]
    do
       _top_=${#_pending_[@]}-1
       _pend_current_=${_pending_[$_top_]}
       unset _pending_[$_top_]
       $_pend_hook_            # Debug only.
       eval $_pend_current_
    done
}

# Drop functions until 'pend_stop_mark' . . .
pend_drop() {
    local -i _top_
    local _pd_ctrl_=${#_pending_[@]}
    while [ ${_pd_ctrl_} -gt 0 ]
    do
       _top_=$_pd_ctrl_-1
       if [ "${_pending_[$_top_]}" == 'pend_stop_mark' ]
       then
           unset _pending_[$_top_]
           break
       else
           unset _pending_[$_top_]
           _pd_ctrl_=$_top_
       fi
    done
    if [ ${#_pending_[@]} -eq 0 ]
    then
        pend_func pend_stop_mark
    fi
}

#### Array editors ####

# This function described in edit_exact.bash.
# (See "Additional documentation," above.)
# edit_exact <excludes_array_name> <target_array_name>
edit_exact() {
    [ $# -eq 2 ] ||
    [ $# -eq 3 ] || return 1
    local -a _ee_Excludes
    local -a _ee_Target
    local _ee_x
    local _ee_t
    local IFS=${NO_WSP}
    set -f
    eval _ee_Excludes=\( \$\{$1\[@\]\} \)
    eval _ee_Target=\( \$\{$2\[@\]\} \)
    local _ee_len=${#_ee_Target[@]}     # Original length.
    local _ee_cnt=${#_ee_Excludes[@]}   # Exclude list length.
    [ ${_ee_len} -ne 0 ] || return 0    # Can't edit zero length.
    [ ${_ee_cnt} -ne 0 ] || return 0    # Can't edit zero length.
    for (( x = 0; x < ${_ee_cnt} ; x++ ))
    do
        _ee_x=${_ee_Excludes[$x]}
        for (( n = 0 ; n < ${_ee_len} ; n++ ))
        do
            _ee_t=${_ee_Target[$n]}
            if [ x"${_ee_t}" == x"${_ee_x}" ]
            then
                unset _ee_Target[$n]     # Discard match.
                [ $# -eq 2 ] && break    # If 2 arguments, then done.
            fi
        done
    done
    eval $2=\( \$\{_ee_Target\[@\]\} \)
    set +f
    return 0
}

# This function described in edit_by_glob.bash.
# edit_by_glob <excludes_array_name> <target_array_name>
edit_by_glob() {
    [ $# -eq 2 ] ||
    [ $# -eq 3 ] || return 1
    local -a _ebg_Excludes
    local -a _ebg_Target
    local _ebg_x
    local _ebg_t
    local IFS=${NO_WSP}
    set -f
    eval _ebg_Excludes=\( \$\{$1\[@\]\} \)
    eval _ebg_Target=\( \$\{$2\[@\]\} \)
    local _ebg_len=${#_ebg_Target[@]}
    local _ebg_cnt=${#_ebg_Excludes[@]}
    [ ${_ebg_len} -ne 0 ] || return 0
    [ ${_ebg_cnt} -ne 0 ] || return 0
    for (( x = 0; x < ${_ebg_cnt} ; x++ ))
    do
        _ebg_x=${_ebg_Excludes[$x]}
        for (( n = 0 ; n < ${_ebg_len} ; n++ ))
        do
            [ $# -eq 3 ] && _ebg_x=${_ebg_x}'*'  #  Do prefix edit
            if [ ${_ebg_Target[$n]:=} ]          #+ if defined & set.
            then
                _ebg_t=${_ebg_Target[$n]/#${_ebg_x}/}
                [ ${#_ebg_t} -eq 0 ] && unset _ebg_Target[$n]
            fi
        done
    done
    eval $2=\( \$\{_ebg_Target\[@\]\} \)
    set +f
    return 0
}

# This function described in unique_lines.bash.
# unique_lines <in_name> <out_name>
unique_lines() {
    [ $# -eq 2 ] || return 1
    local -a _ul_in
    local -a _ul_out
    local -i _ul_cnt
    local -i _ul_pos
    local _ul_tmp
    local IFS=${NO_WSP}
    set -f
    eval _ul_in=\( \$\{$1\[@\]\} \)
    _ul_cnt=${#_ul_in[@]}
    for (( _ul_pos = 0 ; _ul_pos < ${_ul_cnt} ; _ul_pos++ ))
    do
        if [ ${_ul_in[${_ul_pos}]:=} ]      # If defined & not empty
        then
            _ul_tmp=${_ul_in[${_ul_pos}]}
            _ul_out[${#_ul_out[@]}]=${_ul_tmp}
            for (( zap = _ul_pos ; zap < ${_ul_cnt} ; zap++ ))
            do
                [ ${_ul_in[${zap}]:=} ] &&
                [ 'x'${_ul_in[${zap}]} == 'x'${_ul_tmp} ] &&
                    unset _ul_in[${zap}]
            done
        fi
    done
    eval $2=\( \$\{_ul_out\[@\]\} \)
    set +f
    return 0
}

# This function described in char_convert.bash.
# to_lower <string>
to_lower() {
    [ $# -eq 1 ] || return 1
    local _tl_out
    _tl_out=${1//A/a}
    _tl_out=${_tl_out//B/b}
    _tl_out=${_tl_out//C/c}
    _tl_out=${_tl_out//D/d}
    _tl_out=${_tl_out//E/e}
    _tl_out=${_tl_out//F/f}
    _tl_out=${_tl_out//G/g}
    _tl_out=${_tl_out//H/h}
    _tl_out=${_tl_out//I/i}
    _tl_out=${_tl_out//J/j}
    _tl_out=${_tl_out//K/k}
    _tl_out=${_tl_out//L/l}
    _tl_out=${_tl_out//M/m}
    _tl_out=${_tl_out//N/n}
    _tl_out=${_tl_out//O/o}
    _tl_out=${_tl_out//P/p}
    _tl_out=${_tl_out//Q/q}
    _tl_out=${_tl_out//R/r}
    _tl_out=${_tl_out//S/s}
    _tl_out=${_tl_out//T/t}
    _tl_out=${_tl_out//U/u}
    _tl_out=${_tl_out//V/v}
    _tl_out=${_tl_out//W/w}
    _tl_out=${_tl_out//X/x}
    _tl_out=${_tl_out//Y/y}
    _tl_out=${_tl_out//Z/z}
    echo ${_tl_out}
    return 0
}

#### Application helper functions ####

# Not everybody uses dots as separators (APNIC, for example).
# This function described in to_dot.bash
# to_dot <string>
to_dot() {
    [ $# -eq 1 ] || return 1
    echo ${1//[#|@|%]/.}
    return 0
}

# This function described in is_number.bash.
# is_number <input>
is_number() {
    [ "$#" -eq 1 ]    || return 1  # is blank?
    [ x"$1" == 'x0' ] && return 0  # is zero?
    local -i tst
    let tst=$1 2>/dev/null         # else is numeric!
    return $?
}

# This function described in is_address.bash.
# is_address <input>
is_address() {
    [ $# -eq 1 ] || return 1    # Blank ==> false
    local -a _ia_input
    local IFS=${ADR_IFS}
    _ia_input=( $1 )
    if  [ ${#_ia_input[@]} -eq 4 ]  &&
        is_number ${_ia_input[0]}   &&
        is_number ${_ia_input[1]}   &&
        is_number ${_ia_input[2]}   &&
        is_number ${_ia_input[3]}   &&
        [ ${_ia_input[0]} -lt 256 ] &&
        [ ${_ia_input[1]} -lt 256 ] &&
        [ ${_ia_input[2]} -lt 256 ] &&
        [ ${_ia_input[3]} -lt 256 ]
    then
        return 0
    else
        return 1
    fi
}

#  This function described in split_ip.bash.
#  split_ip <IP_address>
#+ <array_name_norm> [<array_name_rev>]
split_ip() {
    [ $# -eq 3 ] ||              #  Either three
    [ $# -eq 2 ] || return 1     #+ or two arguments
    local -a _si_input
    local IFS=${ADR_IFS}
    _si_input=( $1 )
    IFS=${WSP_IFS}
    eval $2=\(\ \$\{_si_input\[@\]\}\ \)
    if [ $# -eq 3 ]
    then
        # Build query order array.
        local -a _dns_ip
        _dns_ip[0]=${_si_input[3]}
        _dns_ip[1]=${_si_input[2]}
        _dns_ip[2]=${_si_input[1]}
        _dns_ip[3]=${_si_input[0]}
        eval $3=\(\ \$\{_dns_ip\[@\]\}\ \)
    fi
    return 0
}

# This function described in dot_array.bash.
# dot_array <array_name>
dot_array() {
    [ $# -eq 1 ] || return 1     # Single argument required.
    local -a _da_input
    eval _da_input=\(\ \$\{$1\[@\]\}\ \)
    local IFS=${DOT_IFS}
    local _da_output=${_da_input[@]}
    IFS=${WSP_IFS}
    echo ${_da_output}
    return 0
}

# This function described in file_to_array.bash
# file_to_array <file_name> <line_array_name>
file_to_array() {
    [ $# -eq 2 ] || return 1  # Two arguments required.
    local IFS=${NO_WSP}
    local -a _fta_tmp_
    _fta_tmp_=( $(cat $1) )
    eval $2=\( \$\{_fta_tmp_\[@\]\} \)
    return 0
}

#  Columnized print of an array of multi-field strings.
#  col_print <array_name> <min_space> <
#+ tab_stop [tab_stops]>
col_print() {
    [ $# -gt 2 ] || return 0
    local -a _cp_inp
    local -a _cp_spc
    local -a _cp_line
    local _cp_min
    local _cp_mcnt
    local _cp_pos
    local _cp_cnt
    local _cp_tab
    local -i _cp
    local -i _cpf
    local _cp_fld
    # WARNING: FOLLOWING LINE NOT BLANK -- IT IS QUOTED SPACES.
    local _cp_max='                                                            '
    set -f
    local IFS=${NO_WSP}
    eval _cp_inp=\(\ \$\{$1\[@\]\}\ \)
    [ ${#_cp_inp[@]} -gt 0 ] || return 0 # Empty is easy.
    _cp_mcnt=$2
    _cp_min=${_cp_max:1:${_cp_mcnt}}
    shift
    shift
    _cp_cnt=$#
    for (( _cp = 0 ; _cp < _cp_cnt ; _cp++ ))
    do
        _cp_spc[${#_cp_spc[@]}]="${_cp_max:2:$1}" #"
        shift
    done
    _cp_cnt=${#_cp_inp[@]}
    for (( _cp = 0 ; _cp < _cp_cnt ; _cp++ ))
    do
        _cp_pos=1
        IFS=${NO_WSP}$'\x20'
        _cp_line=( ${_cp_inp[${_cp}]} )
        IFS=${NO_WSP}
        for (( _cpf = 0 ; _cpf < ${#_cp_line[@]} ; _cpf++ ))
        do
            _cp_tab=${_cp_spc[${_cpf}]:${_cp_pos}}
            if [ ${#_cp_tab} -lt ${_cp_mcnt} ]
            then
                _cp_tab="${_cp_min}"
            fi
            echo -n "${_cp_tab}"
            (( _cp_pos = ${_cp_pos} + ${#_cp_tab} ))
            _cp_fld="${_cp_line[${_cpf}]}"
            echo -n ${_cp_fld}
            (( _cp_pos = ${_cp_pos} + ${#_cp_fld} ))
        done
        echo
    done
    set +f
    return 0
}

# # # # 'Hunt the Spammer' data flow # # # #

# Application return code
declare -i _hs_RC

# Original input, from which IP addresses are removed
# After which, domain names to check
declare -a uc_name

# Original input IP addresses are moved here
# After which, IP addresses to check
declare -a uc_address

# Names against which address expansion run
# Ready for name detail lookup
declare -a chk_name

# Addresses against which name expansion run
# Ready for address detail lookup
declare -a chk_address

#  Recursion is depth-first-by-name.
#  The expand_input_address maintains this list
#+ to prohibit looking up addresses twice during
#+ domain name recursion.
declare -a been_there_addr
been_there_addr=( '127.0.0.1' ) # Whitelist localhost

# Names which we have checked (or given up on)
declare -a known_name

# Addresses which we have checked (or given up on)
declare -a known_address

#  List of zero or more Blacklist servers to check.
#  Each 'known_address' will be checked against each server,
#+ with negative replies and failures suppressed.
declare -a list_server

# Indirection limit - set to zero == no limit
indirect=${SPAMMER_LIMIT:=2}

# # # # 'Hunt the Spammer' information output data # # # #

# Any domain name may have multiple IP addresses.
# Any IP address may have multiple domain names.
# Therefore, track unique address-name pairs.
declare -a known_pair
declare -a reverse_pair

#  In addition to the data flow variables; known_address
#+ known_name and list_server, the following are output to the
#+ external graphics interface file.

# Authority chain, parent -> SOA fields.
declare -a auth_chain

# Reference chain, parent name -> child name
declare -a ref_chain

# DNS chain - domain name -> address
declare -a name_address

# Name and service pairs - domain name -> service
declare -a name_srvc

# Name and resource pairs - domain name -> Resource Record
declare -a name_resource

# Parent and Child pairs - parent name -> child name
# This MAY NOT be the same as the ref_chain followed!
declare -a parent_child

# Address and Blacklist hit pairs - address->server
declare -a address_hits

# Dump interface file data
declare -f _dot_dump
_dot_dump=pend_dummy   # Initially a no-op

#  Data dump is enabled by setting the environment variable SPAMMER_DATA
#+ to the name of a writable file.
declare _dot_file

# Helper function for the dump-to-dot-file function
# dump_to_dot <array_name> <prefix>
dump_to_dot() {
    local -a _dda_tmp
    local -i _dda_cnt
    local _dda_form='    '${2}'%04u %s\n'
    local IFS=${NO_WSP}
    eval _dda_tmp=\(\ \$\{$1\[@\]\}\ \)
    _dda_cnt=${#_dda_tmp[@]}
    if [ ${_dda_cnt} -gt 0 ]
    then
        for (( _dda = 0 ; _dda < _dda_cnt ; _dda++ ))
        do
            printf "${_dda_form}" \
                   "${_dda}" "${_dda_tmp[${_dda}]}" >>${_dot_file}
        done
    fi
}

# Which will also set _dot_dump to this function . . .
dump_dot() {
    local -i _dd_cnt
    echo '# Data vintage: '$(date -R) >${_dot_file}
    echo '# ABS Guide: is_spammer.bash; v2, 2004-msz' >>${_dot_file}
    echo >>${_dot_file}
    echo 'digraph G {' >>${_dot_file}

    if [ ${#known_name[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Known domain name nodes' >>${_dot_file}
        _dd_cnt=${#known_name[@]}
        for (( _dd = 0 ; _dd < _dd_cnt ; _dd++ ))
        do
            printf '    N%04u [label="%s"] ;\n' \
                   "${_dd}" "${known_name[${_dd}]}" >>${_dot_file}
        done
    fi

    if [ ${#known_address[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Known address nodes' >>${_dot_file}
        _dd_cnt=${#known_address[@]}
        for (( _dd = 0 ; _dd < _dd_cnt ; _dd++ ))
        do
            printf '    A%04u [label="%s"] ;\n' \
                   "${_dd}" "${known_address[${_dd}]}" >>${_dot_file}
        done
    fi

    echo                                   >>${_dot_file}
    echo '/*'                              >>${_dot_file}
    echo ' * Known relationships :: User conversion to'  >>${_dot_file}
    echo ' * graphic form by hand or program required.'  >>${_dot_file}
    echo ' *'                              >>${_dot_file}

    if [ ${#auth_chain[@]} -gt 0 ]
    then
      echo >>${_dot_file}
      echo '# Authority ref. edges followed & field source.' >>${_dot_file}
        dump_to_dot auth_chain AC
    fi

    if [ ${#ref_chain[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Name ref. edges followed and field source.' >>${_dot_file}
        dump_to_dot ref_chain RC
    fi

    if [ ${#name_address[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Known name->address edges' >>${_dot_file}
        dump_to_dot name_address NA
    fi

    if [ ${#name_srvc[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Known name->service edges' >>${_dot_file}
        dump_to_dot name_srvc NS
    fi

    if [ ${#name_resource[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Known name->resource edges' >>${_dot_file}
        dump_to_dot name_resource NR
    fi

    if [ ${#parent_child[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Known parent->child edges' >>${_dot_file}
        dump_to_dot parent_child PC
    fi

    if [ ${#list_server[@]} -gt 0 ]
    then
        echo >>${_dot_file}
        echo '# Known Blacklist nodes' >>${_dot_file}
        _dd_cnt=${#list_server[@]}
        for (( _dd = 0 ; _dd < _dd_cnt ; _dd++ ))
        do
            printf '    LS%04u [label="%s"] ;\n' \
                   "${_dd}" "${list_server[${_dd}]}" >>${_dot_file}
        done
    fi

    unique_lines address_hits address_hits
    if [ ${#address_hits[@]} -gt 0 ]
    then
      echo >>${_dot_file}
      echo '# Known address->Blacklist_hit edges' >>${_dot_file}
      echo '# CAUTION: dig warnings can trigger false hits.' >>${_dot_file}
       dump_to_dot address_hits AH
    fi
    echo          >>${_dot_file}
    echo ' *'     >>${_dot_file}
    echo ' * That is a lot of relationships. Happy graphing.' >>${_dot_file}
    echo ' */'    >>${_dot_file}
    echo '}'      >>${_dot_file}
    return 0
}

# # # # 'Hunt the Spammer' execution flow # # # #

#  Execution trace is enabled by setting the
#+ environment variable SPAMMER_TRACE to the name of a writable file.
declare -a _trace_log
declare _log_file

# Function to fill the trace log
trace_logger() {
    _trace_log[${#_trace_log[@]}]=${_pend_current_}
}

# Dump trace log to file function variable.
declare -f _log_dump
_log_dump=pend_dummy   # Initially a no-op.

# Dump the trace log to a file.
dump_log() {
    local -i _dl_cnt
    _dl_cnt=${#_trace_log[@]}
    for (( _dl = 0 ; _dl < _dl_cnt ; _dl++ ))
    do
        echo ${_trace_log[${_dl}]} >> ${_log_file}
    done
    _dl_cnt=${#_pending_[@]}
    if [ ${_dl_cnt} -gt 0 ]
    then
        _dl_cnt=${_dl_cnt}-1
        echo '# # # Operations stack not empty # # #' >> ${_log_file}
        for (( _dl = ${_dl_cnt} ; _dl >= 0 ; _dl-- ))
        do
            echo ${_pending_[${_dl}]} >> ${_log_file}
        done
    fi
}

# # # Utility program 'dig' wrappers # # #
#
#  These wrappers are derived from the
#+ examples shown in dig_wrappers.bash.
#
#  The major difference is these return
#+ their results as a list in an array.
#
#  See dig_wrappers.bash for details and
#+ use that script to develop any changes.
#
# # #

# Short form answer: 'dig' parses answer.

# Forward lookup :: Name -> Address
# short_fwd <domain_name> <array_name>
short_fwd() {
    local -a _sf_reply
    local -i _sf_rc
    local -i _sf_cnt
    IFS=${NO_WSP}
echo -n '.'
# echo 'sfwd: '${1}
  _sf_reply=( $(dig +short ${1} -c in -t a 2>/dev/null) )
  _sf_rc=$?
  if [ ${_sf_rc} -ne 0 ]
  then
    _trace_log[${#_trace_log[@]}]='## Lookup error '${_sf_rc}' on '${1}' ##'
# [ ${_sf_rc} -ne 9 ] && pend_drop
        return ${_sf_rc}
    else
        # Some versions of 'dig' return warnings on stdout.
        _sf_cnt=${#_sf_reply[@]}
        for (( _sf = 0 ; _sf < ${_sf_cnt} ; _sf++ ))
        do
            [ 'x'${_sf_reply[${_sf}]:0:2} == 'x;;' ] &&
                unset _sf_reply[${_sf}]
        done
        eval $2=\( \$\{_sf_reply\[@\]\} \)
    fi
    return 0
}

# Reverse lookup :: Address -> Name
# short_rev <ip_address> <array_name>
short_rev() {
    local -a _sr_reply
    local -i _sr_rc
    local -i _sr_cnt
    IFS=${NO_WSP}
echo -n '.'
# echo 'srev: '${1}
  _sr_reply=( $(dig +short -x ${1} 2>/dev/null) )
  _sr_rc=$?
  if [ ${_sr_rc} -ne 0 ]
  then
    _trace_log[${#_trace_log[@]}]='## Lookup error '${_sr_rc}' on '${1}' ##'
# [ ${_sr_rc} -ne 9 ] && pend_drop
        return ${_sr_rc}
    else
        # Some versions of 'dig' return warnings on stdout.
        _sr_cnt=${#_sr_reply[@]}
        for (( _sr = 0 ; _sr < ${_sr_cnt} ; _sr++ ))
        do
            [ 'x'${_sr_reply[${_sr}]:0:2} == 'x;;' ] &&
                unset _sr_reply[${_sr}]
        done
        eval $2=\( \$\{_sr_reply\[@\]\} \)
    fi
    return 0
}

# Special format lookup used to query blacklist servers.
# short_text <ip_address> <array_name>
short_text() {
    local -a _st_reply
    local -i _st_rc
    local -i _st_cnt
    IFS=${NO_WSP}
# echo 'stxt: '${1}
  _st_reply=( $(dig +short ${1} -c in -t txt 2>/dev/null) )
  _st_rc=$?
  if [ ${_st_rc} -ne 0 ]
  then
    _trace_log[${#_trace_log[@]}]='##Text lookup error '${_st_rc}' on '${1}'##'
# [ ${_st_rc} -ne 9 ] && pend_drop
        return ${_st_rc}
    else
        # Some versions of 'dig' return warnings on stdout.
        _st_cnt=${#_st_reply[@]}
        for (( _st = 0 ; _st < ${#_st_cnt} ; _st++ ))
        do
            [ 'x'${_st_reply[${_st}]:0:2} == 'x;;' ] &&
                unset _st_reply[${_st}]
        done
        eval $2=\( \$\{_st_reply\[@\]\} \)
    fi
    return 0
}

# The long forms, a.k.a., the parse it yourself versions

# RFC 2782   Service lookups
# dig +noall +nofail +answer _ldap._tcp.openldap.org -t srv
# _<service>._<protocol>.<domain_name>
# _ldap._tcp.openldap.org. 3600   IN     SRV    0 0 389 ldap.openldap.org.
# domain TTL Class SRV Priority Weight Port Target

# Forward lookup :: Name -> poor man's zone transfer
# long_fwd <domain_name> <array_name>
long_fwd() {
    local -a _lf_reply
    local -i _lf_rc
    local -i _lf_cnt
    IFS=${NO_WSP}
echo -n ':'
# echo 'lfwd: '${1}
  _lf_reply=( $(
     dig +noall +nofail +answer +authority +additional \
         ${1} -t soa ${1} -t mx ${1} -t any 2>/dev/null) )
  _lf_rc=$?
  if [ ${_lf_rc} -ne 0 ]
  then
    _trace_log[${#_trace_log[@]}]='# Zone lookup err '${_lf_rc}' on '${1}' #'
# [ ${_lf_rc} -ne 9 ] && pend_drop
        return ${_lf_rc}
    else
        # Some versions of 'dig' return warnings on stdout.
        _lf_cnt=${#_lf_reply[@]}
        for (( _lf = 0 ; _lf < ${_lf_cnt} ; _lf++ ))
        do
            [ 'x'${_lf_reply[${_lf}]:0:2} == 'x;;' ] &&
                unset _lf_reply[${_lf}]
        done
        eval $2=\( \$\{_lf_reply\[@\]\} \)
    fi
    return 0
}
#  The reverse lookup domain name corresponding to the IPv6 address:
#      4321:0:1:2:3:4:567:89ab
#  would be (nibble, I.E: Hexdigit) reversed:
#  b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.0.0.0.0.1.2.3.4.IP6.ARPA.

# Reverse lookup :: Address -> poor man's delegation chain
# long_rev <rev_ip_address> <array_name>
long_rev() {
    local -a _lr_reply
    local -i _lr_rc
    local -i _lr_cnt
    local _lr_dns
    _lr_dns=${1}'.in-addr.arpa.'
    IFS=${NO_WSP}
echo -n ':'
# echo 'lrev: '${1}
  _lr_reply=( $(
       dig +noall +nofail +answer +authority +additional \
           ${_lr_dns} -t soa ${_lr_dns} -t any 2>/dev/null) )
  _lr_rc=$?
  if [ ${_lr_rc} -ne 0 ]
  then
    _trace_log[${#_trace_log[@]}]='# Deleg lkp error '${_lr_rc}' on '${1}' #'
# [ ${_lr_rc} -ne 9 ] && pend_drop
        return ${_lr_rc}
    else
        # Some versions of 'dig' return warnings on stdout.
        _lr_cnt=${#_lr_reply[@]}
        for (( _lr = 0 ; _lr < ${_lr_cnt} ; _lr++ ))
        do
            [ 'x'${_lr_reply[${_lr}]:0:2} == 'x;;' ] &&
                unset _lr_reply[${_lr}]
        done
        eval $2=\( \$\{_lr_reply\[@\]\} \)
    fi
    return 0
}

# # # Application specific functions # # #

# Mung a possible name; suppresses root and TLDs.
# name_fixup <string>
name_fixup(){
    local -a _nf_tmp
    local -i _nf_end
    local _nf_str
    local IFS
    _nf_str=$(to_lower ${1})
    _nf_str=$(to_dot ${_nf_str})
    _nf_end=${#_nf_str}-1
    [ ${_nf_str:${_nf_end}} != '.' ] &&
        _nf_str=${_nf_str}'.'
    IFS=${ADR_IFS}
    _nf_tmp=( ${_nf_str} )
    IFS=${WSP_IFS}
    _nf_end=${#_nf_tmp[@]}
    case ${_nf_end} in
    0) # No dots, only dots.
        echo
        return 1
    ;;
    1) # Only a TLD.
        echo
        return 1
    ;;
    2) # Maybe okay.
       echo ${_nf_str}
       return 0
       # Needs a lookup table?
       if [ ${#_nf_tmp[1]} -eq 2 ]
       then # Country coded TLD.
           echo
           return 1
       else
           echo ${_nf_str}
           return 0
       fi
    ;;
    esac
    echo ${_nf_str}
    return 0
}

# Grope and mung original input(s).
split_input() {
    [ ${#uc_name[@]} -gt 0 ] || return 0
    local -i _si_cnt
    local -i _si_len
    local _si_str
    unique_lines uc_name uc_name
    _si_cnt=${#uc_name[@]}
    for (( _si = 0 ; _si < _si_cnt ; _si++ ))
    do
        _si_str=${uc_name[$_si]}
        if is_address ${_si_str}
        then
            uc_address[${#uc_address[@]}]=${_si_str}
            unset uc_name[$_si]
        else
            if ! uc_name[$_si]=$(name_fixup ${_si_str})
            then
                unset ucname[$_si]
            fi
        fi
    done
  uc_name=( ${uc_name[@]} )
  _si_cnt=${#uc_name[@]}
  _trace_log[${#_trace_log[@]}]='#Input '${_si_cnt}' unchkd name input(s).#'
  _si_cnt=${#uc_address[@]}
  _trace_log[${#_trace_log[@]}]='#Input '${_si_cnt}' unchkd addr input(s).#'
    return 0
}

# # # Discovery functions -- recursively interlocked by external data # # #
# # # The leading 'if list is empty; return 0' in each is required. # # #

# Recursion limiter
# limit_chk() <next_level>
limit_chk() {
    local -i _lc_lmt
    # Check indirection limit.
    if [ ${indirect} -eq 0 ] || [ $# -eq 0 ]
    then
        # The 'do-forever' choice
        echo 1                 # Any value will do.
        return 0               # OK to continue.
    else
        # Limiting is in effect.
        if [ ${indirect} -lt ${1} ]
        then
            echo ${1}          # Whatever.
            return 1           # Stop here.
        else
            _lc_lmt=${1}+1     # Bump the given limit.
            echo ${_lc_lmt}    # Echo it.
            return 0           # OK to continue.
        fi
    fi
}

# For each name in uc_name:
#     Move name to chk_name.
#     Add addresses to uc_address.
#     Pend expand_input_address.
#     Repeat until nothing new found.
# expand_input_name <indirection_limit>
expand_input_name() {
    [ ${#uc_name[@]} -gt 0 ] || return 0
    local -a _ein_addr
    local -a _ein_new
    local -i _ucn_cnt
    local -i _ein_cnt
    local _ein_tst
    _ucn_cnt=${#uc_name[@]}

    if  ! _ein_cnt=$(limit_chk ${1})
    then
        return 0
    fi

    for (( _ein = 0 ; _ein < _ucn_cnt ; _ein++ ))
    do
        if short_fwd ${uc_name[${_ein}]} _ein_new
        then
          for (( _ein_cnt = 0 ; _ein_cnt < ${#_ein_new[@]}; _ein_cnt++ ))
          do
              _ein_tst=${_ein_new[${_ein_cnt}]}
              if is_address ${_ein_tst}
              then
                  _ein_addr[${#_ein_addr[@]}]=${_ein_tst}
              fi
    done
        fi
    done
    unique_lines _ein_addr _ein_addr     # Scrub duplicates.
    edit_exact chk_address _ein_addr     # Scrub pending detail.
    edit_exact known_address _ein_addr   # Scrub already detailed.
 if [ ${#_ein_addr[@]} -gt 0 ]        # Anything new?
 then
   uc_address=( ${uc_address[@]} ${_ein_addr[@]} )
   pend_func expand_input_address ${1}
   _trace_log[${#_trace_log[@]}]='#Add '${#_ein_addr[@]}' unchkd addr inp.#'
    fi
    edit_exact chk_name uc_name          # Scrub pending detail.
    edit_exact known_name uc_name        # Scrub already detailed.
    if [ ${#uc_name[@]} -gt 0 ]
    then
        chk_name=( ${chk_name[@]} ${uc_name[@]}  )
        pend_func detail_each_name ${1}
    fi
    unset uc_name[@]
    return 0
}

# For each address in uc_address:
#     Move address to chk_address.
#     Add names to uc_name.
#     Pend expand_input_name.
#     Repeat until nothing new found.
# expand_input_address <indirection_limit>
expand_input_address() {
    [ ${#uc_address[@]} -gt 0 ] || return 0
    local -a _eia_addr
    local -a _eia_name
    local -a _eia_new
    local -i _uca_cnt
    local -i _eia_cnt
    local _eia_tst
    unique_lines uc_address _eia_addr
    unset uc_address[@]
    edit_exact been_there_addr _eia_addr
    _uca_cnt=${#_eia_addr[@]}
    [ ${_uca_cnt} -gt 0 ] &&
        been_there_addr=( ${been_there_addr[@]} ${_eia_addr[@]} )

    for (( _eia = 0 ; _eia < _uca_cnt ; _eia++ ))
     do
       if short_rev ${_eia_addr[${_eia}]} _eia_new
       then
         for (( _eia_cnt = 0 ; _eia_cnt < ${#_eia_new[@]} ; _eia_cnt++ ))
         do
           _eia_tst=${_eia_new[${_eia_cnt}]}
           if _eia_tst=$(name_fixup ${_eia_tst})
           then
             _eia_name[${#_eia_name[@]}]=${_eia_tst}
       fi
     done
           fi
    done
    unique_lines _eia_name _eia_name     # Scrub duplicates.
    edit_exact chk_name _eia_name        # Scrub pending detail.
    edit_exact known_name _eia_name      # Scrub already detailed.
 if [ ${#_eia_name[@]} -gt 0 ]        # Anything new?
 then
   uc_name=( ${uc_name[@]} ${_eia_name[@]} )
   pend_func expand_input_name ${1}
   _trace_log[${#_trace_log[@]}]='#Add '${#_eia_name[@]}' unchkd name inp.#'
    fi
    edit_exact chk_address _eia_addr     # Scrub pending detail.
    edit_exact known_address _eia_addr   # Scrub already detailed.
    if [ ${#_eia_addr[@]} -gt 0 ]        # Anything new?
    then
        chk_address=( ${chk_address[@]} ${_eia_addr[@]} )
        pend_func detail_each_address ${1}
    fi
    return 0
}

# The parse-it-yourself zone reply.
# The input is the chk_name list.
# detail_each_name <indirection_limit>
detail_each_name() {
    [ ${#chk_name[@]} -gt 0 ] || return 0
    local -a _den_chk       # Names to check
    local -a _den_name      # Names found here
    local -a _den_address   # Addresses found here
    local -a _den_pair      # Pairs found here
    local -a _den_rev       # Reverse pairs found here
    local -a _den_tmp       # Line being parsed
    local -a _den_auth      # SOA contact being parsed
    local -a _den_new       # The zone reply
    local -a _den_pc        # Parent-Child gets big fast
    local -a _den_ref       # So does reference chain
    local -a _den_nr        # Name-Resource can be big
    local -a _den_na        # Name-Address
    local -a _den_ns        # Name-Service
    local -a _den_achn      # Chain of Authority
    local -i _den_cnt       # Count of names to detail
    local -i _den_lmt       # Indirection limit
    local _den_who          # Named being processed
    local _den_rec          # Record type being processed
    local _den_cont         # Contact domain
    local _den_str          # Fixed up name string
    local _den_str2         # Fixed up reverse
    local IFS=${WSP_IFS}

    # Local, unique copy of names to check
    unique_lines chk_name _den_chk
    unset chk_name[@]       # Done with globals.

    # Less any names already known
    edit_exact known_name _den_chk
    _den_cnt=${#_den_chk[@]}

    # If anything left, add to known_name.
    [ ${_den_cnt} -gt 0 ] &&
        known_name=( ${known_name[@]} ${_den_chk[@]} )

    # for the list of (previously) unknown names . . .
    for (( _den = 0 ; _den < _den_cnt ; _den++ ))
    do
        _den_who=${_den_chk[${_den}]}
        if long_fwd ${_den_who} _den_new
        then
            unique_lines _den_new _den_new
            if [ ${#_den_new[@]} -eq 0 ]
            then
                _den_pair[${#_den_pair[@]}]='0.0.0.0 '${_den_who}
            fi

            # Parse each line in the reply.
            for (( _line = 0 ; _line < ${#_den_new[@]} ; _line++ ))
            do
                IFS=${NO_WSP}$'\x09'$'\x20'
                _den_tmp=( ${_den_new[${_line}]} )
                IFS=${WSP_IFS}
              # If usable record and not a warning message . . .
              if [ ${#_den_tmp[@]} -gt 4 ] && [ 'x'${_den_tmp[0]} != 'x;;' ]
              then
                    _den_rec=${_den_tmp[3]}
                    _den_nr[${#_den_nr[@]}]=${_den_who}' '${_den_rec}
                    # Begin at RFC1033 (+++)
                    case ${_den_rec} in

#<name> [<ttl>]  [<class>] SOA <origin> <person>
                    SOA) # Start Of Authority
    if _den_str=$(name_fixup ${_den_tmp[0]})
    then
      _den_name[${#_den_name[@]}]=${_den_str}
      _den_achn[${#_den_achn[@]}]=${_den_who}' '${_den_str}' SOA'
      # SOA origin -- domain name of master zone record
      if _den_str2=$(name_fixup ${_den_tmp[4]})
      then
        _den_name[${#_den_name[@]}]=${_den_str2}
        _den_achn[${#_den_achn[@]}]=${_den_who}' '${_den_str2}' SOA.O'
      fi
      # Responsible party e-mail address (possibly bogus).
      # Possibility of first.last@domain.name ignored.
      set -f
      if _den_str2=$(name_fixup ${_den_tmp[5]})
      then
        IFS=${ADR_IFS}
        _den_auth=( ${_den_str2} )
        IFS=${WSP_IFS}
        if [ ${#_den_auth[@]} -gt 2 ]
        then
          _den_cont=${_den_auth[1]}
          for (( _auth = 2 ; _auth < ${#_den_auth[@]} ; _auth++ ))
          do
            _den_cont=${_den_cont}'.'${_den_auth[${_auth}]}
          done
          _den_name[${#_den_name[@]}]=${_den_cont}'.'
          _den_achn[${#_den_achn[@]}]=${_den_who}' '${_den_cont}'. SOA.C'
                                fi
        fi
        set +f
                        fi
                    ;;


      A) # IP(v4) Address Record
      if _den_str=$(name_fixup ${_den_tmp[0]})
      then
        _den_name[${#_den_name[@]}]=${_den_str}
        _den_pair[${#_den_pair[@]}]=${_den_tmp[4]}' '${_den_str}
        _den_na[${#_den_na[@]}]=${_den_str}' '${_den_tmp[4]}
        _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' A'
      else
        _den_pair[${#_den_pair[@]}]=${_den_tmp[4]}' unknown.domain'
        _den_na[${#_den_na[@]}]='unknown.domain '${_den_tmp[4]}
        _den_ref[${#_den_ref[@]}]=${_den_who}' unknown.domain A'
      fi
      _den_address[${#_den_address[@]}]=${_den_tmp[4]}
      _den_pc[${#_den_pc[@]}]=${_den_who}' '${_den_tmp[4]}
             ;;

             NS) # Name Server Record
             # Domain name being serviced (may be other than current)
               if _den_str=$(name_fixup ${_den_tmp[0]})
                 then
                   _den_name[${#_den_name[@]}]=${_den_str}
                   _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' NS'

             # Domain name of service provider
             if _den_str2=$(name_fixup ${_den_tmp[4]})
             then
               _den_name[${#_den_name[@]}]=${_den_str2}
               _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str2}' NSH'
               _den_ns[${#_den_ns[@]}]=${_den_str2}' NS'
               _den_pc[${#_den_pc[@]}]=${_den_str}' '${_den_str2}
              fi
               fi
                    ;;

             MX) # Mail Server Record
                 # Domain name being serviced (wildcards not handled here)
             if _den_str=$(name_fixup ${_den_tmp[0]})
             then
               _den_name[${#_den_name[@]}]=${_den_str}
               _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' MX'
             fi
             # Domain name of service provider
             if _den_str=$(name_fixup ${_den_tmp[5]})
             then
               _den_name[${#_den_name[@]}]=${_den_str}
               _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' MXH'
               _den_ns[${#_den_ns[@]}]=${_den_str}' MX'
               _den_pc[${#_den_pc[@]}]=${_den_who}' '${_den_str}
             fi
                    ;;

             PTR) # Reverse address record
                  # Special name
             if _den_str=$(name_fixup ${_den_tmp[0]})
             then
               _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' PTR'
               # Host name (not a CNAME)
               if _den_str2=$(name_fixup ${_den_tmp[4]})
               then
                 _den_rev[${#_den_rev[@]}]=${_den_str}' '${_den_str2}
                 _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str2}' PTRH'
                 _den_pc[${#_den_pc[@]}]=${_den_who}' '${_den_str}
               fi
             fi
                    ;;

             AAAA) # IP(v6) Address Record
             if _den_str=$(name_fixup ${_den_tmp[0]})
             then
               _den_name[${#_den_name[@]}]=${_den_str}
               _den_pair[${#_den_pair[@]}]=${_den_tmp[4]}' '${_den_str}
               _den_na[${#_den_na[@]}]=${_den_str}' '${_den_tmp[4]}
               _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' AAAA'
               else
                 _den_pair[${#_den_pair[@]}]=${_den_tmp[4]}' unknown.domain'
                 _den_na[${#_den_na[@]}]='unknown.domain '${_den_tmp[4]}
                 _den_ref[${#_den_ref[@]}]=${_den_who}' unknown.domain'
               fi
               # No processing for IPv6 addresses
               _den_pc[${#_den_pc[@]}]=${_den_who}' '${_den_tmp[4]}
                    ;;

             CNAME) # Alias name record
                    # Nickname
             if _den_str=$(name_fixup ${_den_tmp[0]})
             then
               _den_name[${#_den_name[@]}]=${_den_str}
               _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' CNAME'
               _den_pc[${#_den_pc[@]}]=${_den_who}' '${_den_str}
             fi
                    # Hostname
             if _den_str=$(name_fixup ${_den_tmp[4]})
             then
               _den_name[${#_den_name[@]}]=${_den_str}
               _den_ref[${#_den_ref[@]}]=${_den_who}' '${_den_str}' CHOST'
               _den_pc[${#_den_pc[@]}]=${_den_who}' '${_den_str}
             fi
                    ;;
#            TXT)
#            ;;
                    esac
                fi
            done
        else # Lookup error == 'A' record 'unknown address'
            _den_pair[${#_den_pair[@]}]='0.0.0.0 '${_den_who}
        fi
    done

    # Control dot array growth.
    unique_lines _den_achn _den_achn      # Works best, all the same.
    edit_exact auth_chain _den_achn       # Works best, unique items.
    if [ ${#_den_achn[@]} -gt 0 ]
    then
        IFS=${NO_WSP}
        auth_chain=( ${auth_chain[@]} ${_den_achn[@]} )
        IFS=${WSP_IFS}
    fi

    unique_lines _den_ref _den_ref      # Works best, all the same.
    edit_exact ref_chain _den_ref       # Works best, unique items.
    if [ ${#_den_ref[@]} -gt 0 ]
    then
        IFS=${NO_WSP}
        ref_chain=( ${ref_chain[@]} ${_den_ref[@]} )
        IFS=${WSP_IFS}
    fi

    unique_lines _den_na _den_na
    edit_exact name_address _den_na
    if [ ${#_den_na[@]} -gt 0 ]
    then
        IFS=${NO_WSP}
        name_address=( ${name_address[@]} ${_den_na[@]} )
        IFS=${WSP_IFS}
    fi

    unique_lines _den_ns _den_ns
    edit_exact name_srvc _den_ns
    if [ ${#_den_ns[@]} -gt 0 ]
    then
        IFS=${NO_WSP}
        name_srvc=( ${name_srvc[@]} ${_den_ns[@]} )
        IFS=${WSP_IFS}
    fi

    unique_lines _den_nr _den_nr
    edit_exact name_resource _den_nr
    if [ ${#_den_nr[@]} -gt 0 ]
    then
        IFS=${NO_WSP}
        name_resource=( ${name_resource[@]} ${_den_nr[@]} )
        IFS=${WSP_IFS}
    fi

    unique_lines _den_pc _den_pc
    edit_exact parent_child _den_pc
    if [ ${#_den_pc[@]} -gt 0 ]
    then
        IFS=${NO_WSP}
        parent_child=( ${parent_child[@]} ${_den_pc[@]} )
        IFS=${WSP_IFS}
    fi

    # Update list known_pair (Address and Name).
    unique_lines _den_pair _den_pair
    edit_exact known_pair _den_pair
    if [ ${#_den_pair[@]} -gt 0 ]  # Anything new?
    then
        IFS=${NO_WSP}
        known_pair=( ${known_pair[@]} ${_den_pair[@]} )
        IFS=${WSP_IFS}
    fi

    # Update list of reverse pairs.
    unique_lines _den_rev _den_rev
    edit_exact reverse_pair _den_rev
    if [ ${#_den_rev[@]} -gt 0 ]   # Anything new?
    then
        IFS=${NO_WSP}
        reverse_pair=( ${reverse_pair[@]} ${_den_rev[@]} )
        IFS=${WSP_IFS}
    fi

    # Check indirection limit -- give up if reached.
    if ! _den_lmt=$(limit_chk ${1})
    then
        return 0
    fi

# Execution engine is LIFO. Order of pend operations is important.
# Did we define any new addresses?
unique_lines _den_address _den_address    # Scrub duplicates.
edit_exact known_address _den_address     # Scrub already processed.
edit_exact un_address _den_address        # Scrub already waiting.
if [ ${#_den_address[@]} -gt 0 ]          # Anything new?
then
  uc_address=( ${uc_address[@]} ${_den_address[@]} )
  pend_func expand_input_address ${_den_lmt}
  _trace_log[${#_trace_log[@]}]='# Add '${#_den_address[@]}' unchkd addr. #'
    fi

# Did we find any new names?
unique_lines _den_name _den_name          # Scrub duplicates.
edit_exact known_name _den_name           # Scrub already processed.
edit_exact uc_name _den_name              # Scrub already waiting.
if [ ${#_den_name[@]} -gt 0 ]             # Anything new?
then
  uc_name=( ${uc_name[@]} ${_den_name[@]} )
  pend_func expand_input_name ${_den_lmt}
  _trace_log[${#_trace_log[@]}]='#Added '${#_den_name[@]}' unchkd name#'
    fi
    return 0
}

# The parse-it-yourself delegation reply
# Input is the chk_address list.
# detail_each_address <indirection_limit>
detail_each_address() {
    [ ${#chk_address[@]} -gt 0 ] || return 0
    unique_lines chk_address chk_address
    edit_exact known_address chk_address
    if [ ${#chk_address[@]} -gt 0 ]
    then
        known_address=( ${known_address[@]} ${chk_address[@]} )
        unset chk_address[@]
    fi
    return 0
}

# # # Application specific output functions # # #

# Pretty print the known pairs.
report_pairs() {
    echo
    echo 'Known network pairs.'
    col_print known_pair 2 5 30

    if [ ${#auth_chain[@]} -gt 0 ]
    then
        echo
        echo 'Known chain of authority.'
        col_print auth_chain 2 5 30 55
    fi

    if [ ${#reverse_pair[@]} -gt 0 ]
    then
        echo
        echo 'Known reverse pairs.'
        col_print reverse_pair 2 5 55
    fi
    return 0
}

# Check an address against the list of blacklist servers.
# A good place to capture for GraphViz: address->status(server(reports))
# check_lists <ip_address>
check_lists() {
    [ $# -eq 1 ] || return 1
    local -a _cl_fwd_addr
    local -a _cl_rev_addr
    local -a _cl_reply
    local -i _cl_rc
    local -i _ls_cnt
    local _cl_dns_addr
    local _cl_lkup

    split_ip ${1} _cl_fwd_addr _cl_rev_addr
    _cl_dns_addr=$(dot_array _cl_rev_addr)'.'
    _ls_cnt=${#list_server[@]}
    echo '    Checking address '${1}
    for (( _cl = 0 ; _cl < _ls_cnt ; _cl++ ))
    do
      _cl_lkup=${_cl_dns_addr}${list_server[${_cl}]}
      if short_text ${_cl_lkup} _cl_reply
      then
        if [ ${#_cl_reply[@]} -gt 0 ]
        then
          echo '        Records from '${list_server[${_cl}]}
          address_hits[${#address_hits[@]}]=${1}' '${list_server[${_cl}]}
          _hs_RC=2
          for (( _clr = 0 ; _clr < ${#_cl_reply[@]} ; _clr++ ))
          do
            echo '            '${_cl_reply[${_clr}]}
          done
        fi
      fi
    done
    return 0
}

# # # The usual application glue # # #

# Who did it?
credits() {
   echo
   echo 'Advanced Bash Scripting Guide: is_spammer.bash, v2, 2004-msz'
}

# How to use it?
# (See also, "Quickstart" at end of script.)
usage() {
    cat <<-'_usage_statement_'
    The script is_spammer.bash requires either one or two arguments.

    arg 1) May be one of:
        a) A domain name
        b) An IPv4 address
        c) The name of a file with any mix of names
           and addresses, one per line.

    arg 2) May be one of:
        a) A Blacklist server domain name
        b) The name of a file with Blacklist server
           domain names, one per line.
        c) If not present, a default list of (free)
           Blacklist servers is used.
        d) If a filename of an empty, readable, file
           is given,
           Blacklist server lookup is disabled.

    All script output is written to stdout.

    Return codes: 0 -> All OK, 1 -> Script failure,
                  2 -> Something is Blacklisted.

    Requires the external program 'dig' from the 'bind-9'
    set of DNS programs.  See: http://www.isc.org

    The domain name lookup depth limit defaults to 2 levels.
    Set the environment variable SPAMMER_LIMIT to change.
    SPAMMER_LIMIT=0 means 'unlimited'

    Limit may also be set on the command line.
    If arg#1 is an integer, the limit is set to that value
    and then the above argument rules are applied.

    Setting the environment variable 'SPAMMER_DATA' to a filename
    will cause the script to write a GraphViz graphic file.

    For the development version;
    Setting the environment variable 'SPAMMER_TRACE' to a filename
    will cause the execution engine to log a function call trace.

_usage_statement_
}

# The default list of Blacklist servers:
# Many choices, see: http://www.spews.org/lists.html

declare -a default_servers
# See: http://www.spamhaus.org (Conservative, well maintained)
default_servers[0]='sbl-xbl.spamhaus.org'
# See: http://ordb.org (Open mail relays)
default_servers[1]='relays.ordb.org'
# See: http://www.spamcop.net/ (You can report spammers here)
default_servers[2]='bl.spamcop.net'
# See: http://www.spews.org (An 'early detect' system)
default_servers[3]='l2.spews.dnsbl.sorbs.net'
# See: http://www.dnsbl.us.sorbs.net/using.shtml
default_servers[4]='dnsbl.sorbs.net'
# See: http://dsbl.org/usage (Various mail relay lists)
default_servers[5]='list.dsbl.org'
default_servers[6]='multihop.dsbl.org'
default_servers[7]='unconfirmed.dsbl.org'

# User input argument #1
setup_input() {
    if [ -e ${1} ] && [ -r ${1} ]  # Name of readable file
    then
        file_to_array ${1} uc_name
        echo 'Using filename >'${1}'< as input.'
    else
        if is_address ${1}          # IP address?
        then
            uc_address=( ${1} )
            echo 'Starting with address >'${1}'<'
        else                       # Must be a name.
            uc_name=( ${1} )
            echo 'Starting with domain name >'${1}'<'
        fi
    fi
    return 0
}

# User input argument #2
setup_servers() {
    if [ -e ${1} ] && [ -r ${1} ]  # Name of a readable file
    then
        file_to_array ${1} list_server
        echo 'Using filename >'${1}'< as blacklist server list.'
    else
        list_server=( ${1} )
        echo 'Using blacklist server >'${1}'<'
    fi
    return 0
}

# User environment variable SPAMMER_TRACE
live_log_die() {
    if [ ${SPAMMER_TRACE:=} ]    # Wants trace log?
    then
        if [ ! -e ${SPAMMER_TRACE} ]
        then
            if ! touch ${SPAMMER_TRACE} 2>/dev/null
            then
                pend_func echo $(printf '%q\n' \
                'Unable to create log file >'${SPAMMER_TRACE}'<')
                pend_release
                exit 1
            fi
            _log_file=${SPAMMER_TRACE}
            _pend_hook_=trace_logger
            _log_dump=dump_log
        else
            if [ ! -w ${SPAMMER_TRACE} ]
            then
                pend_func echo $(printf '%q\n' \
                'Unable to write log file >'${SPAMMER_TRACE}'<')
                pend_release
                exit 1
            fi
            _log_file=${SPAMMER_TRACE}
            echo '' > ${_log_file}
            _pend_hook_=trace_logger
            _log_dump=dump_log
        fi
    fi
    return 0
}

# User environment variable SPAMMER_DATA
data_capture() {
    if [ ${SPAMMER_DATA:=} ]    # Wants a data dump?
    then
        if [ ! -e ${SPAMMER_DATA} ]
        then
            if ! touch ${SPAMMER_DATA} 2>/dev/null
            then
                pend_func echo $(printf '%q]n' \
                'Unable to create data output file >'${SPAMMER_DATA}'<')
                pend_release
                exit 1
            fi
            _dot_file=${SPAMMER_DATA}
            _dot_dump=dump_dot
        else
            if [ ! -w ${SPAMMER_DATA} ]
            then
                pend_func echo $(printf '%q\n' \
                'Unable to write data output file >'${SPAMMER_DATA}'<')
                pend_release
                exit 1
            fi
            _dot_file=${SPAMMER_DATA}
            _dot_dump=dump_dot
        fi
    fi
    return 0
}

# Grope user specified arguments.
do_user_args() {
    if [ $# -gt 0 ] && is_number $1
    then
        indirect=$1
        shift
    fi

    case $# in                     # Did user treat us well?
        1)
            if ! setup_input $1    # Needs error checking.
            then
                pend_release
                $_log_dump
                exit 1
            fi
            list_server=( ${default_servers[@]} )
            _list_cnt=${#list_server[@]}
            echo 'Using default blacklist server list.'
            echo 'Search depth limit: '${indirect}
            ;;
        2)
            if ! setup_input $1    # Needs error checking.
            then
                pend_release
                $_log_dump
                exit 1
            fi
            if ! setup_servers $2  # Needs error checking.
            then
                pend_release
                $_log_dump
                exit 1
            fi
            echo 'Search depth limit: '${indirect}
            ;;
        *)
            pend_func usage
            pend_release
            $_log_dump
            exit 1
            ;;
    esac
    return 0
}

# A general purpose debug tool.
# list_array <array_name>
list_array() {
    [ $# -eq 1 ] || return 1  # One argument required.

    local -a _la_lines
    set -f
    local IFS=${NO_WSP}
    eval _la_lines=\(\ \$\{$1\[@\]\}\ \)
    echo
    echo "Element count "${#_la_lines[@]}" array "${1}
    local _ln_cnt=${#_la_lines[@]}

    for (( _i = 0; _i < ${_ln_cnt}; _i++ ))
    do
        echo 'Element '$_i' >'${_la_lines[$_i]}'<'
    done
    set +f
    return 0
}

# # # 'Hunt the Spammer' program code # # #
pend_init                               # Ready stack engine.
pend_func credits                       # Last thing to print.

# # # Deal with user # # #
live_log_die                            # Setup debug trace log.
data_capture                            # Setup data capture file.
echo
do_user_args $@

# # # Haven't exited yet - There is some hope # # #
# Discovery group - Execution engine is LIFO - pend
# in reverse order of execution.
_hs_RC=0                                # Hunt the Spammer return code
pend_mark
    pend_func report_pairs              # Report name-address pairs.

    # The two detail_* are mutually recursive functions.
    # They also pend expand_* functions as required.
    # These two (the last of ???) exit the recursion.
    pend_func detail_each_address       # Get all resources of addresses.
    pend_func detail_each_name          # Get all resources of names.

    #  The two expand_* are mutually recursive functions,
    #+ which pend additional detail_* functions as required.
    pend_func expand_input_address 1    # Expand input names by address.
    pend_func expand_input_name 1       # #xpand input addresses by name.

    # Start with a unique set of names and addresses.
    pend_func unique_lines uc_address uc_address
    pend_func unique_lines uc_name uc_name

    # Separate mixed input of names and addresses.
    pend_func split_input
pend_release

# # # Pairs reported -- Unique list of IP addresses found
echo
_ip_cnt=${#known_address[@]}
if [ ${#list_server[@]} -eq 0 ]
then
    echo 'Blacklist server list empty, none checked.'
else
    if [ ${_ip_cnt} -eq 0 ]
    then
        echo 'Known address list empty, none checked.'
    else
        _ip_cnt=${_ip_cnt}-1   # Start at top.
        echo 'Checking Blacklist servers.'
        for (( _ip = _ip_cnt ; _ip >= 0 ; _ip-- ))
        do
          pend_func check_lists $( printf '%q\n' ${known_address[$_ip]} )
        done
    fi
fi
pend_release
$_dot_dump                   # Graphics file dump
$_log_dump                   # Execution trace
echo


##############################
# Example output from script #
##############################
:<<-'_is_spammer_outputs_'

./is_spammer.bash 0 web4.alojamentos7.com

Starting with domain name >web4.alojamentos7.com<
Using default blacklist server list.
Search depth limit: 0
.:....::::...:::...:::.......::..::...:::.......::
Known network pairs.
    66.98.208.97             web4.alojamentos7.com.
    66.98.208.97             ns1.alojamentos7.com.
    69.56.202.147            ns2.alojamentos.ws.
    66.98.208.97             alojamentos7.com.
    66.98.208.97             web.alojamentos7.com.
    69.56.202.146            ns1.alojamentos.ws.
    69.56.202.146            alojamentos.ws.
    66.235.180.113           ns1.alojamentos.org.
    66.235.181.192           ns2.alojamentos.org.
    66.235.180.113           alojamentos.org.
    66.235.180.113           web6.alojamentos.org.
    216.234.234.30           ns1.theplanet.com.
    12.96.160.115            ns2.theplanet.com.
    216.185.111.52           mail1.theplanet.com.
    69.56.141.4              spooling.theplanet.com.
    216.185.111.40           theplanet.com.
    216.185.111.40           www.theplanet.com.
    216.185.111.52           mail.theplanet.com.

Checking Blacklist servers.
  Checking address 66.98.208.97
      Records from dnsbl.sorbs.net
  "Spam Received See: http://www.dnsbl.sorbs.net/lookup.shtml?66.98.208.97"
    Checking address 69.56.202.147
    Checking address 69.56.202.146
    Checking address 66.235.180.113
    Checking address 66.235.181.192
    Checking address 216.185.111.40
    Checking address 216.234.234.30
    Checking address 12.96.160.115
    Checking address 216.185.111.52
    Checking address 69.56.141.4

Advanced Bash Scripting Guide: is_spammer.bash, v2, 2004-msz

_is_spammer_outputs_

exit ${_hs_RC}

####################################################
#  The script ignores everything from here on down #
#+ because of the 'exit' command, just above.      #
####################################################



Quickstart
==========

 Prerequisites

  Bash version 2.05b or 3.00 (bash --version)
  A version of Bash which supports arrays. Array 
  support is included by default Bash configurations.

  'dig,' version 9.x.x (dig $HOSTNAME, see first line of output)
  A version of dig which supports the +short options. 
  See: dig_wrappers.bash for details.


 Optional Prerequisites

  'named,' a local DNS caching program. Any flavor will do.
  Do twice: dig $HOSTNAME 
  Check near bottom of output for: SERVER: 127.0.0.1#53
  That means you have one running.


 Optional Graphics Support

  'date,' a standard *nix thing. (date -R)

  dot Program to convert graphic description file to a 
  diagram. (dot -V)
  A part of the Graph-Viz set of programs.
  See: [http://www.research.att.com/sw/tools/graphviz||GraphViz]

  'dotty,' a visual editor for graphic description files.
  Also a part of the Graph-Viz set of programs.




 Quick Start

In the same directory as the is_spammer.bash script; 
Do: ./is_spammer.bash

 Usage Details

1. Blacklist server choices.

  (a) To use default, built-in list: Do nothing.

  (b) To use your own list: 

    i. Create a file with a single Blacklist server 
       domain name per line.

    ii. Provide that filename as the last argument to 
        the script.

  (c) To use a single Blacklist server: Last argument 
      to the script.

  (d) To disable Blacklist lookups:

    i. Create an empty file (touch spammer.nul)
       Your choice of filename.

    ii. Provide the filename of that empty file as the 
        last argument to the script.

2. Search depth limit.

  (a) To use the default value of 2: Do nothing.

  (b) To set a different limit: 
      A limit of 0 means: no limit.

    i. export SPAMMER_LIMIT=1
       or whatever limit you want.

    ii. OR provide the desired limit as the first 
       argument to the script.

3. Optional execution trace log.

  (a) To use the default setting of no log output: Do nothing.

  (b) To write an execution trace log:
      export SPAMMER_TRACE=spammer.log
      or whatever filename you want.

4. Optional graphic description file.

  (a) To use the default setting of no graphic file: Do nothing.

  (b) To write a Graph-Viz graphic description file:
      export SPAMMER_DATA=spammer.dot
      or whatever filename you want.

5. Where to start the search.

  (a) Starting with a single domain name:

    i. Without a command line search limit: First 
       argument to script.

    ii. With a command line search limit: Second 
        argument to script.

  (b) Starting with a single IP address:

    i. Without a command line search limit: First 
       argument to script.

    ii. With a command line search limit: Second 
        argument to script.

  (c) Starting with (mixed) multiple name(s) and/or address(es):
      Create a file with one name or address per line.
      Your choice of filename.

    i. Without a command line search limit: Filename as 
       first argument to script.

    ii. With a command line search limit: Filename as 
        second argument to script.

6. What to do with the display output.

  (a) To view display output on screen: Do nothing.

  (b) To save display output to a file: Redirect stdout to a filename.

  (c) To discard display output: Redirect stdout to /dev/null.

7. Temporary end of decision making. 
   press RETURN 
   wait (optionally, watch the dots and colons).

8. Optionally check the return code.

  (a) Return code 0: All OK

  (b) Return code 1: Script setup failure

  (c) Return code 2: Something was blacklisted.

9. Where is my graph (diagram)?

The script does not directly produce a graph (diagram). 
It only produces a graphic description file. You can 
process the graphic descriptor file that was output 
with the 'dot' program.

Until you edit that descriptor file, to describe the 
relationships you want shown, all that you will get is 
a bunch of labeled name and address nodes.

All of the script's discovered relationships are within 
a comment block in the graphic descriptor file, each 
with a descriptive heading.

The editing required to draw a line between a pair of 
nodes from the information in the descriptor file may 
be done with a text editor. 

Given these lines somewhere in the descriptor file:

# Known domain name nodes

N0000 [label="guardproof.info."] ;

N0002 [label="third.guardproof.info."] ;



# Known address nodes

A0000 [label="61.141.32.197"] ;



/*

# Known name->address edges

NA0000 third.guardproof.info. 61.141.32.197



# Known parent->child edges

PC0000 guardproof.info. third.guardproof.info.

 */

Turn that into the following lines by substituting node 
identifiers into the relationships:

# Known domain name nodes

N0000 [label="guardproof.info."] ;

N0002 [label="third.guardproof.info."] ;



# Known address nodes

A0000 [label="61.141.32.197"] ;



# PC0000 guardproof.info. third.guardproof.info.

N0000->N0002 ;



# NA0000 third.guardproof.info. 61.141.32.197

N0002->A0000 ;



/*

# Known name->address edges

NA0000 third.guardproof.info. 61.141.32.197



# Known parent->child edges

PC0000 guardproof.info. third.guardproof.info.

 */

Process that with the 'dot' program, and you have your 
first network diagram.

In addition to the conventional graphic edges, the 
descriptor file includes similar format pair-data that 
describes services, zone records (sub-graphs?), 
blacklisted addresses, and other things which might be 
interesting to include in your graph. This additional 
information could be displayed as different node 
shapes, colors, line sizes, etc.

The descriptor file can also be read and edited by a 
Bash script (of course). You should be able to find 
most of the functions required within the 
"is_spammer.bash" script.

# End Quickstart.



Additional Note
========== ====

Michael Zick points out that there is a "makeviz.bash" interactive
Web site at rediris.es. Can't give the full URL, since this is not
a publically accessible site.

Un altro script anti-spam.

Esempio A-29. Caccia allo spammer

#!/bin/bash
# whx.sh: "whois" spammer lookup
# Author: Walter Dnes
# Slight revisions (first section) by ABS Guide author.
# Used in ABS Guide with permission.

# Needs version 3.x or greater of Bash to run (because of =~ operator).
# Commented by script author and ABS Guide author.



E_BADARGS=65        # Missing command-line arg.
E_NOHOST=66         # Host not found.
E_TIMEOUT=67        # Host lookup timed out.
E_UNDEF=68          # Some other (undefined) error.
HOSTWAIT=10         # Specify up to 10 seconds for host query reply.
                    # The actual wait may be a bit longer.
OUTFILE=whois.txt   # Output file.
PORT=4321


if [ -z "$1" ]      # Check for (required) command-line arg.
then
  echo "Usage: $0 domain name or IP address"
  exit $E_BADARGS
fi


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}')
                                      # Doing host lookup to get IP address.
				      # Extract final field.
else
  IPADDR="$1"                         # Command-line arg was IP address.
fi

echo; echo "IP Address is: "$IPADDR""; echo

if [ -e "$OUTFILE" ]
then
  rm -f "$OUTFILE"
  echo "Stale output file \"$OUTFILE\" removed."; echo
fi


#  Sanity checks.
#  (This section needs more work.)
#  ===============================
if [ -z "$IPADDR" ]
# No response.
then
  echo "Host not found!"
  exit $E_NOHOST    # Bail out.
fi

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)]$" ]]
#  Host xxxxxxxxx.xxx not found: 3(NXDOMAIN)
then
  echo "Host not found!"
  exit $E_NOHOST    # Bail out.
fi

if [[ "$IPADDR" =~ "[(SERVFAIL)]$" ]]
#  Host xxxxxxxxx.xxx not found: 2(SERVFAIL)
then
  echo "Host not found!"
  exit $E_NOHOST    # Bail out.
fi




# ======================== Main body of script ========================

AFRINICquery() {
#  Define the function that queries AFRINIC. Echo a notification to the
#+ screen, and then run the actual query, redirecting output to $OUTFILE.

  echo "Searching for $IPADDR in whois.afrinic.net"
  whois -h whois.afrinic.net "$IPADDR" > $OUTFILE

#  Check for presence of reference to an rwhois.
#  Warn about non-functional rwhois.infosat.net server
#+ and attempt rwhois query.
  if grep -e "^remarks: .*rwhois\.[^ ]\+" "$OUTFILE"
  then
    echo " " >> $OUTFILE
    echo "***" >> $OUTFILE
    echo "***" >> $OUTFILE
    echo "Warning: rwhois.infosat.net was not working as of 2005/02/02" >> $OUTFILE
    echo "         when this script was written." >> $OUTFILE
    echo "***" >> $OUTFILE
    echo "***" >> $OUTFILE
    echo " " >> $OUTFILE
    RWHOIS=`grep "^remarks: .*rwhois\.[^ ]\+" "$OUTFILE" | tail -n 1 |\
    sed "s/\(^.*\)\(rwhois\..*\)\(:4.*\)/\2/"`
    whois -h ${RWHOIS}:${PORT} "$IPADDR" >> $OUTFILE
  fi
}

APNICquery() {
  echo "Searching for $IPADDR in whois.apnic.net"
  whois -h whois.apnic.net "$IPADDR" > $OUTFILE

#  Just  about  every  country has its own internet registrar.
#  I don't normally bother consulting them, because the regional registry
#+ usually supplies sufficient information.
#  There are a few exceptions, where the regional registry simply
#+ refers to the national registry for direct data.
#  These are Japan and South Korea in APNIC, and Brasil in LACNIC.
#  The following if statement checks $OUTFILE (whois.txt) for the presence
#+ of "KR" (South Korea) or "JP" (Japan) in the country field.
#  If either is found, the query is re-run against the appropriate
#+ national registry.

  if grep -E "^country:[ ]+KR$" "$OUTFILE"
  then
    echo "Searching for $IPADDR in whois.krnic.net"
    whois -h whois.krnic.net "$IPADDR" >> $OUTFILE
  elif grep -E "^country:[ ]+JP$" "$OUTFILE"
  then
    echo "Searching for $IPADDR in whois.nic.ad.jp"
    whois -h whois.nic.ad.jp "$IPADDR"/e >> $OUTFILE
  fi
}

ARINquery() {
  echo "Searching for $IPADDR in whois.arin.net"
  whois -h whois.arin.net "$IPADDR" > $OUTFILE

#  Several large internet providers listed by ARIN have their own
#+ internal whois service, referred to as "rwhois".
#  A large block of IP addresses is listed with the provider
#+ under the ARIN registry.
#  To get the IP addresses of 2nd-level ISPs or other large customers,
#+ one has to refer to the rwhois server on port 4321.
#  I originally started with a bunch of "if" statements checking for
#+ the larger providers.
#  This approach is unwieldy, and there's always another rwhois server
#+ that I didn't know about.
#  A more elegant approach is to check $OUTFILE for a reference
#+ to a whois server, parse that server name out of the comment section,
#+ and re-run the query against the appropriate rwhois server.
#  The parsing looks a bit ugly, with a long continued line inside
#+ backticks.
#  But it only has to be done once, and will work as new servers are added.
#@   ABS Guide author comment: it isn't all that ugly, and is, in fact,
#@+  an instructive use of Regular Expressions.

  if grep -E "^Comment: .*rwhois.[^ ]+" "$OUTFILE"
  then
    RWHOIS=`grep -e "^Comment:.*rwhois\.[^ ]\+" "$OUTFILE" | tail -n 1 |\
    sed "s/^\(.*\)\(rwhois\.[^ ]\+\)\(.*$\)/\2/"`
    echo "Searching for $IPADDR in ${RWHOIS}"
    whois -h ${RWHOIS}:${PORT} "$IPADDR" >> $OUTFILE
  fi
}

LACNICquery() {
  echo "Searching for $IPADDR in whois.lacnic.net"
  whois -h whois.lacnic.net "$IPADDR" > $OUTFILE

#  The  following if statement checks $OUTFILE (whois.txt) for the presence of
#+ "BR" (Brasil) in the country field.
#  If it is found, the query is re-run against whois.registro.br.

  if grep -E "^country:[ ]+BR$" "$OUTFILE"
  then
    echo "Searching for $IPADDR in whois.registro.br"
    whois -h whois.registro.br "$IPADDR" >> $OUTFILE
  fi
}

RIPEquery() {
  echo "Searching for $IPADDR in whois.ripe.net"
  whois -h whois.ripe.net "$IPADDR" > $OUTFILE
}

#  Initialize a few variables.
#  * slash8 is the most significant octet
#  * slash16 consists of the two most significant octets
#  * octet2 is the second most significant octet




slash8=`echo $IPADDR | cut -d. -f 1`
  if [ -z "$slash8" ]  # Yet another sanity check.
  then
    echo "Undefined error!"
    exit $E_UNDEF
  fi
slash16=`echo $IPADDR | cut -d. -f 1-2`
#                             ^ Period specified as 'cut" delimiter.
  if [ -z "$slash16" ]
  then
    echo "Undefined error!"
    exit $E_UNDEF
  fi
octet2=`echo $slash16 | cut -d. -f 2`
  if [ -z "$octet2" ]
  then
    echo "Undefined error!"
    exit $E_UNDEF
  fi


#  Check for various odds and ends of reserved space.
#  There is no point in querying for those addresses.

if [ $slash8 == 0 ]; then
  echo $IPADDR is '"This Network"' space\; Not querying
elif [ $slash8 == 10 ]; then
  echo $IPADDR is RFC1918 space\; Not querying
elif [ $slash8 == 14 ]; then
  echo $IPADDR is '"Public Data Network"' space\; Not querying
elif [ $slash8 == 127 ]; then
  echo $IPADDR is loopback space\; Not querying
elif [ $slash16 == 169.254 ]; then
  echo $IPADDR is link-local space\; Not querying
elif [ $slash8 == 172 ] && [ $octet2 -ge 16 ] && [ $octet2 -le 31 ];then
  echo $IPADDR is RFC1918 space\; Not querying
elif [ $slash16 == 192.168 ]; then
  echo $IPADDR is RFC1918 space\; Not querying
elif [ $slash8 -ge 224 ]; then
  echo $IPADDR is either Multicast or reserved space\; Not querying
elif [ $slash8 -ge 200 ] && [ $slash8 -le 201 ]; then LACNICquery "$IPADDR"
elif [ $slash8 -ge 202 ] && [ $slash8 -le 203 ]; then APNICquery "$IPADDR"
elif [ $slash8 -ge 210 ] && [ $slash8 -le 211 ]; then APNICquery "$IPADDR"
elif [ $slash8 -ge 218 ] && [ $slash8 -le 223 ]; then APNICquery "$IPADDR"

#  If we got this far without making a decision, query ARIN.
#  If a reference is found in $OUTFILE to APNIC, AFRINIC, LACNIC, or RIPE,
#+ query the appropriate whois server.

else
  ARINquery "$IPADDR"
  if grep "whois.afrinic.net" "$OUTFILE"; then
    AFRINICquery "$IPADDR"
  elif grep -E "^OrgID:[ ]+RIPE$" "$OUTFILE"; then
    RIPEquery "$IPADDR"
  elif grep -E "^OrgID:[ ]+APNIC$" "$OUTFILE"; then
    APNICquery "$IPADDR"
  elif grep -E "^OrgID:[ ]+LACNIC$" "$OUTFILE"; then
    LACNICquery "$IPADDR"
  fi
fi

#@  ---------------------------------------------------------------
#   Try also:
#   wget http://logi.cc/nw/whois.php3?ACTION=doQuery&DOMAIN=$IPADDR
#@  ---------------------------------------------------------------

#  We've  now  finished  the querying.
#  Echo a copy of the final result to the screen.

cat $OUTFILE
# Or "less $OUTFILE" . . .


exit 0

#@  ABS Guide author comments:
#@  Nothing fancy here, but still a very useful tool for hunting spammers.
#@  Sure, the script can be cleaned up some, and it's still a bit buggy,
#@+ (exercise for reader), but all the same, it's a nice piece of coding
#@+ by Walter Dnes.
#@  Thank you!

Front end di "Little Monster" per wget.

Esempio A-30. Rendere wget più semplice da usare

#!/bin/bash
# wgetter2.bash

# Author: Little Monster [monster@monstruum.co.uk]
# ==> Used in ABS Guide with permission of script author.
# ==> This script still needs debugging and fixups (exercise for reader).
# ==> It could also use some additional editing in the comments.


#  This is wgetter2 --
#+ a Bash script to make wget a bit more friendly, and save typing.

#  Carefully crafted by Little Monster.
#  More or less complete on 02/02/2005.
#  If you think this script can be improved,
#+ email me at: monster@monstruum.co.uk
# ==> and cc: to the author of the ABS Guide, please.
#  This script is licenced under the GPL.
#  You are free to copy, alter and re-use it,
#+ but please don't try to claim you wrote it.
#  Log your changes here instead.

# =======================================================================
# changelog:

# 07/02/2005.  Fixups by Little Monster.
# 02/02/2005.  Minor additions by Little Monster.
#              (See after # +++++++++++ )
# 29/01/2005.  Minor stylistic edits and cleanups by author of ABS Guide.
#              Added exit error codes.
# 22/11/2004.  Finished initial version of second version of wgetter:
#              wgetter2 is born.
# 01/12/2004.  Changed 'runn' function so it can be run 2 ways --
#              either ask for a file name or have one input on the CL.
# 01/12/2004.  Made sensible handling of no URL's given.
# 01/12/2004.  Made loop of main options, so you don't
#              have to keep calling wgetter 2 all the time.
#              Runs as a session instead.
# 01/12/2004.  Added looping to 'runn' function.
#              Simplified and improved.
# 01/12/2004.  Added state to recursion setting.
#              Enables re-use of previous value.
# 05/12/2004.  Modified the file detection routine in the 'runn' function
#              so it's not fooled by empty values, and is cleaner.
# 01/02/2004.  Added cookie finding routine from later version (which 
#              isn't ready yet), so as not to have hard-coded paths.
# =======================================================================

# Error codes for abnormal exit.
E_USAGE=67        # Usage message, then quit.
E_NO_OPTS=68      # No command-line args entered.
E_NO_URLS=69      # No URLs passed to script.
E_NO_SAVEFILE=70  # No save filename passed to script.
E_USER_EXIT=71    # User decides to quit.


#  Basic default wget command we want to use.
#  This is the place to change it, if required.
#  NB: if using a proxy, set http_proxy = yourproxy in .wgetrc.
#  Otherwise delete --proxy=on, below.
# ====================================================================
CommandA="wget -nc -c -t 5 --progress=bar --random-wait --proxy=on -r"
# ====================================================================



# --------------------------------------------------------------------
# Set some other variables and explain them.

pattern=" -A .jpg,.JPG,.jpeg,.JPEG,.gif,.GIF,.htm,.html,.shtml,.php"
                    # wget's option to only get certain types of file.
                    # comment out if not using
today=`date +%F`    # Used for a filename.
home=$HOME          # Set HOME to an internal variable.
                    # In case some other path is used, change it here.
depthDefault=3      # Set a sensible default recursion.
Depth=$depthDefault # Otherwise user feedback doesn't tie in properly.
RefA=""             # Set blank referring page.
Flag=""             #  Default to not saving anything,
                    #+ or whatever else might be wanted in future.
lister=""           # Used for passing a list of urls directly to wget.
Woptions=""         # Used for passing wget some options for itself.
inFile=""           # Used for the run function.
newFile=""          # Used for the run function.
savePath="$home/w-save"
Config="$home/.wgetter2rc"
                    #  This is where some variables can be stored, 
                    #+ if permanently changed from within the script.
Cookie_List="$home/.cookielist"
                    # So we know where the cookies are kept . . .
cFlag=""            # Part of the cookie file selection routine.

# Define the options available. Easy to change letters here if needed.
# These are the optional options; you don't just wait to be asked.

save=s   # Save command instead of executing it.
cook=c   # Change cookie file for this session.
help=h   # Usage guide.
list=l   # Pass wget the -i option and URL list.
runn=r   # Run saved commands as an argument to the option.
inpu=i   # Run saved commands interactively.
wopt=w   # Allow to enter options to pass directly to wget.
# --------------------------------------------------------------------


if [ -z "$1" ]; then   # Make sure we get something for wget to eat.
   echo "You must at least enter a URL or option!"
   echo "-$help for usage."
   exit $E_NO_OPTS
fi



# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# added added added added added added added added added added added added

if [ ! -e "$Config" ]; then   # See if configuration file exists.
   echo "Creating configuration file, $Config"
   echo "# This is the configuration file for wgetter2" > "$Config"
   echo "# Your customised settings will be saved in this file" >> "$Config"
else
   source $Config             # Import variables we set outside the script.
fi

if [ ! -e "$Cookie_List" ]; then
   # Set up a list of cookie files, if there isn't one.
   echo "Hunting for cookies . . ."
   find -name cookies.txt >> $Cookie_List # Create the list of cookie files.
fi #  Isolate this in its own 'if' statement,
   #+ in case we got interrupted while searching.

if [ -z "$cFlag" ]; then # If we haven't already done this . . .
   echo                  # Make a nice space after the command prompt.
   echo "Looks like you haven't set up your source of cookies yet."
   n=0                   #  Make sure the counter
                         #+ doesn't contain random values.
   while read; do
      Cookies[$n]=$REPLY # Put the cookie files we found into an array.
      echo "$n) ${Cookies[$n]}"  # Create a menu.
      n=$(( n + 1 ))     # Increment the counter.
   done < $Cookie_List   # Feed the read statement.
   echo "Enter the number of the cookie file you want to use."
   echo "If you won't be using cookies, just press RETURN."
   echo
   echo "I won't be asking this again. Edit $Config"
   echo "If you decide to change at a later date"
   echo "or use the -${cook} option for per session changes."
   read
   if [ ! -z $REPLY ]; then   # User didn't just press return.
      Cookie=" --load-cookies ${Cookies[$REPLY]}"
      # Set the variable here as well as in the config file.

      echo "Cookie=\" --load-cookies ${Cookies[$REPLY]}\"" >> $Config
   fi
   echo "cFlag=1" >> $Config  # So we know not to ask again.
fi

# end added section end added section end added section end added section
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



# Another variable.
# This one may or may not be subject to variation.
# A bit like the small print.
CookiesON=$Cookie
# echo "cookie file is $CookiesON" # For debugging.
# echo "home is ${home}"           # For debugging.
                                   # Got caught with this one!


wopts()
{
echo "Enter options to pass to wget."
echo "It is assumed you know what you're doing."
echo
echo "You can pass their arguments here too."
# That is to say, everything passed here is passed to wget.

read Wopts
# Read in the options to be passed to wget.

Woptions=" $Wopts"
#         ^  Why the leading space?
# Assign to another variable.
# Just for fun, or something . . .

echo "passing options ${Wopts} to wget"
# Mainly for debugging.
# Is cute.

return
}


save_func()
{
echo "Settings will be saved."
if [ ! -d $savePath ]; then  #  See if directory exists.
   mkdir $savePath           #  Create the directory to save things in
                             #+ if it isn't already there.
fi

Flag=S
# Tell the final bit of code what to do.
# Set a flag since stuff is done in main.

return
}


usage() # Tell them how it works.
{
    echo "Welcome to wgetter.  This is a front end to wget."
    echo "It will always run wget with these options:"
    echo "$CommandA"
    echo "and the pattern to match: $pattern \
(which you can change at the top of this script)."
    echo "It will also ask you for recursion depth, \
and if you want to use a referring page."
    echo "Wgetter accepts the following options:"
    echo ""
    echo "-$help : Display this help."
    echo "-$save : Save the command to a file $savePath/wget-($today) \
instead of running it."
    echo "-$runn : Run saved wget commands instead of starting a new one -"
    echo "Enter filename as argument to this option."
    echo "-$inpu : Run saved wget commands interactively --"
    echo "The script will ask you for the filename."
    echo "-$cook : Change the cookies file for this session."
    echo "-$list : Tell wget to use URL's from a list instead of \
from the command line."
    echo "-$wopt : Pass any other options direct to wget."
    echo ""
    echo "See the wget man page for additional options \
you can pass to wget."
    echo ""

    exit $E_USAGE  # End here. Don't process anything else.
}



list_func() #  Gives the user the option to use the -i option to wget,
            #+ and a list of URLs.
{
while [ 1 ]; do
   echo "Enter the name of the file containing URL's (press q to change
your mind)."
   read urlfile
   if [ ! -e "$urlfile" ] && [ "$urlfile" != q ]; then
       # Look for a file, or the quit option.
       echo "That file does not exist!"
   elif [ "$urlfile" = q ]; then # Check quit option.
       echo "Not using a url list."
       return
   else
      echo "using $urlfile."
      echo "If you gave url's on the command line, I'll use those first."
                            # Report wget standard behaviour to the user.
      lister=" -i $urlfile" # This is what we want to pass to wget.
      return
   fi
done
}


cookie_func() # Give the user the option to use a different cookie file.
{
while [ 1 ]; do
   echo "Change the cookies file. Press return if you don't want to change 
it."
   read Cookies
   # NB: this is not the same as Cookie, earlier.
   # There is an 's' on the end.
   # Bit like chocolate chips.
   if [ -z "$Cookies" ]; then                 # Escape clause for wusses.
      return
   elif [ ! -e "$Cookies" ]; then
      echo "File does not exist.  Try again." # Keep em going . . .
   else
       CookiesON=" --load-cookies $Cookies"   # File is good -- use it!
       return
   fi
done
}



run_func()
{
if [ -z "$OPTARG" ]; then
# Test to see if we used the in-line option or the query one.
   if [ ! -d "$savePath" ]; then      # If directory doesn't exist . . .
      echo "$savePath does not appear to exist."
      echo "Please supply path and filename of saved wget commands:"
      read newFile
         until [ -f "$newFile" ]; do  # Keep going till we get something.
            echo "Sorry, that file does not exist.  Please try again."
            # Try really hard to get something.
            read newFile
         done


# -----------------------------------------------------------------------
#       if [ -z ( grep wget ${newfile} ) ]; then
        # Assume they haven't got the right file and bail out.
#       echo "Sorry, that file does not contain wget commands.  Aborting."
#       exit
#       fi
#
# This is bogus code.
# It doesn't actually work.
# If anyone wants to fix it, feel free!
# -----------------------------------------------------------------------


      filePath="${newFile}"
   else
   echo "Save path is $savePath"
     echo "Please enter name of the file which you want to use."
     echo "You have a choice of:"
     ls $savePath                                    # Give them a choice.
     read inFile
       until [ -f "$savePath/$inFile" ]; do         #  Keep going till
                                                    #+ we get something.
          if [ ! -f "${savePath}/${inFile}" ]; then # If file doesn't exist.
             echo "Sorry, that file does not exist.  Please choose from:"
             ls $savePath                           # If a mistake is made.
             read inFile
          fi
         done
      filePath="${savePath}/${inFile}"  # Make one variable . . .
   fi
else filePath="${savePath}/${OPTARG}"   # Which can be many things . . .
fi

if [ ! -f "$filePath" ]; then           # If a bogus file got through.
   echo "You did not specify a suitable file."
   echo "Run this script with the -${save} option first."
   echo "Aborting."
   exit $E_NO_SAVEFILE
fi
echo "Using: $filePath"
while read; do
    eval $REPLY
    echo "Completed: $REPLY"
done < $filePath  # Feed the actual file we are using into a 'while' loop.

exit
}



# Fish out any options we are using for the script.
# This is based on the demo in "Learning The Bash Shell" (O'Reilly).
while getopts ":$save$cook$help$list$runn:$inpu$wopt" opt
do
  case $opt in
     $save) save_func;;   #  Save some wgetter sessions for later.
     $cook) cookie_func;; #  Change cookie file.
     $help) usage;;       #  Get help.
     $list) list_func;;   #  Allow wget to use a list of URLs.
     $runn) run_func;;    #  Useful if you are calling wgetter from,
                          #+ for example, a cron script.
     $inpu) run_func;;    #  When you don't know what your files are named.
     $wopt) wopts;;       #  Pass options directly to wget.
        \?) echo "Not a valid option."
            echo "Use -${wopt} to pass options directly to wget,"
            echo "or -${help} for help";;      # Catch anything else.
  esac
done
shift $((OPTIND - 1))     # Do funky magic stuff with $#.


if [ -z "$1" ] && [ -z "$lister" ]; then
                          #  We should be left with at least one URL
                          #+ on the command line, unless a list is 
			  #+ being used -- catch empty CL's.
   echo "No URL's given! You must enter them on the same line as wgetter2."
   echo "E.g.,  wgetter2 http://somesite http://anothersite."
   echo "Use $help option for more information."
   exit $E_NO_URLS        # Bail out, with appropriate error code.
fi

URLS=" $@"
# Use this so that URL list can be changed if we stay in the option loop.

while [ 1 ]; do
   # This is where we ask for the most used options.
   # (Mostly unchanged from version 1 of wgetter)
   if [ -z $curDepth ]; then
      Current=""
   else Current=" Current value is $curDepth"
   fi
       echo "How deep should I go? \
(integer: Default is $depthDefault.$Current)"
       read Depth   # Recursion -- how far should we go?
       inputB=""    # Reset this to blank on each pass of the loop.
       echo "Enter the name of the referring page (default is none)."
       read inputB  # Need this for some sites.

       echo "Do you want to have the output logged to the terminal"
       echo "(y/n, default is yes)?"
       read noHide  # Otherwise wget will just log it to a file.

       case $noHide in    # Now you see me, now you don't.
          y|Y ) hide="";;
          n|N ) hide=" -b";;
            * ) hide="";;
       esac

       if [ -z ${Depth} ]; then
       #  User accepted either default or current depth,
       #+ in which case Depth is now empty.
          if [ -z ${curDepth} ]; then
          #  See if a depth was set on a previous iteration.
             Depth="$depthDefault"
             #  Set the default recursion depth if nothing
             #+ else to use.
          else Depth="$curDepth" #  Otherwise, set the one we used before.
          fi
       fi
   Recurse=" -l $Depth"          # Set how deep we want to go.
   curDepth=$Depth               # Remember setting for next time.

       if [ ! -z $inputB ]; then
          RefA=" --referer=$inputB"   # Option to use referring page.
       fi

   WGETTER="${CommandA}${pattern}${hide}${RefA}${Recurse}\
${CookiesON}${lister}${Woptions}${URLS}"
   #  Just string the whole lot together . . .
   #  NB: no embedded spaces.
   #  They are in the individual elements so that if any are empty,
   #+ we don't get an extra space.

   if [ -z "${CookiesON}" ] && [ "$cFlag" = "1" ] ; then
       echo "Warning -- can't find cookie file"
       #  This should be changed,
       #+ in case the user has opted to not use cookies.
   fi

   if [ "$Flag" = "S" ]; then
      echo "$WGETTER" >> $savePath/wget-${today}
      #  Create a unique filename for today, or append to it if it exists.
      echo "$inputB" >> $savePath/site-list-${today}
      #  Make a list, so it's easy to refer back to,
      #+ since the whole command is a bit confusing to look at.
      echo "Command saved to the file $savePath/wget-${today}"
           # Tell the user.
      echo "Referring page URL saved to the file$ \
savePath/site-list-${today}"
           # Tell the user.
      Saver=" with save option"
      # Stick this somewhere, so it appears in the loop if set.
   else
       echo "*****************"
       echo "*****Getting*****"
       echo "*****************"
       echo ""
       echo "$WGETTER"
       echo ""
       echo "*****************"
       eval "$WGETTER"
   fi

       echo ""
       echo "Starting over$Saver."
       echo "If you want to stop, press q."
       echo "Otherwise, enter some URL's:"
       # Let them go again. Tell about save option being set.

       read
       case $REPLY in
       # Need to change this to a 'trap' clause.
          q|Q ) exit $E_USER_EXIT;;  # Exercise for the reader?
            * ) URLS=" $REPLY";;
       esac

       echo ""
done


exit 0

Esempio A-31. Uno script per il "podcasting"

#!/bin/bash

# bashpodder.sh:
# di Linc 10/1/2004
#  Trovate l'ultima versione dello script su 
#+ http://linc.homeunix.org:8080/scripts/bashpodder
# Ultima revisione 12/14/2004 - Molti collaboratori!
#  Se usate lo script e avete dei miglioramenti da suggerire o commenti da fare
#+ inviatemi un'email a linc punto fessenden chiocciola gmail punto com
# Lo gradirei!

# ==>  Commenti extra per Guida ASB.

# ==>  L'autore dello script ha gentilmente acconsentito al suo inserimento
# ==>+ in Guida ASB.


# ==> ###################################################################
# 
# ==> Cos'è il "podcasting"?

# ==> È la trasmissione su internet di "programmi radiofonici".
# ==> I programmi possono essere riascoltati con gli iPods e 
# ==> altri programmi di riproduzione di file musicali.

# ==> Questo script ha lo scopo di rendere tutto questo possibile.
# ==> Vedi la documentazione sul sito dell'autore dello script, più sopra.

# ==> ###################################################################


# Rende lo script adatto a crontab:
cd $(dirname $0)
# ==> Si sposta nella directory dove risiede lo script.

# dirdati è la directory dove vengono salvati gli ipodcast:
dirdati=$(date +%Y-%m-%d)
# ==> Verrà creata una directory con nome: AAAA-MM-GG

# Controlla e crea, se necessario, dirdati:
if test ! -d $dirdati
        then
        mkdir $dirdati
fi

# Cancella i file temporanei:
rm -f temp.log

#  Legge il file bp.conf file e usa wget su tutte le url non ancora 
#+ elencate nel file podcast.log:
while read podcast
        do # ==> Di seguito l'azione principale.
        file=$(wget -q $podcast -O - | tr '\r' '\n' | tr \' \" | \
sed -n 's/.*url="\([^"]*\)".*/\1/p')
 for url in $file
                do
                echo $url >> temp.log
                if ! grep "$url" podcast.log > /dev/null
                        then
                        wget -q -P $dirdati "$url"
                fi
                done
    done < bp.conf

# Trasforma i file di log creati dinamicamente in file di log permanenti:
cat podcast.log >> temp.log
sort temp.log | uniq > podcast.log
rm temp.log
# Crea una playlist m3u:
ls $dirdati | grep -v m3u > $dirdati/podcast.m3u


exit 0

#################################################
Per un diverso approccio al Podcasting tramite scripting,
vedi l'articolo di Phil Salkie
"Internet Radio to Podcast with Shell Tools"
nel numero del settembre 2005 di LINUX JOURNAL,
http://www.linuxjournal.com/article/8171
#################################################

Esempio A-32. Backup notturno su un HD firewire

#!/bin/bash
# nightly-backup.sh
# http://www.richardneill.org/source.php#nightly-backup-rsync
# Copyright (c) 2005 Richard Neill <backup@richardneill.org>.
# This is Free Software licensed under the GNU GPL.
# ==> Included in ABS Guide with script author's kind permission.
# ==> (Thanks!)

#  This does a backup from the host computer to a locally connected
#+ firewire HDD using rsync and ssh.
#  It then rotates the backups.
#  Run it via cron every night at 5am.
#  This only backs up the home directory.
#  If ownerships (other than the user's) should be preserved,
#+ then run the rsync process as root (and re-instate the -o).
#  We save every day for 7 days, then every week for 4 weeks,
#+ then every month for 3 months.

#  See: http://www.mikerubel.org/computers/rsync_snapshots/
#+ for more explanation of the theory.
#  Save as: $HOME/bin/nightly-backup_firewire-hdd.sh

#  Known bugs:
#  ----------
#  i)  Ideally, we want to exclude ~/.tmp and the browser caches.

#  ii) If the user is sitting at the computer at 5am,
#+     and files are modified while the rsync is occurring,
#+     then the BACKUP_JUSTINCASE branch gets triggered.
#      To some extent, this is a 
#+     feature, but it also causes a "disk-space leak".





##### BEGIN CONFIGURATION SECTION ############################################
LOCAL_USER=rjn                # User whose home directory should be backed up.
MOUNT_POINT=/backup           # Mountpoint of backup drive.
                              # NO trailing slash!
                              # This must be unique (eg using a udev symlink)
SOURCE_DIR=/home/$LOCAL_USER  # NO trailing slash - it DOES matter to rsync.
BACKUP_DEST_DIR=$MOUNT_POINT/backup/`hostname -s`.${LOCAL_USER}.nightly_backup
DRY_RUN=false                 #If true, invoke rsync with -n, to do a dry run.
                              # Comment out or set to false for normal use.
VERBOSE=false                 # If true, make rsync verbose.
                              # Comment out or set to false otherwise.
COMPRESS=false                # If true, compress.
                              # Good for internet, bad on LAN.
                              # Comment out or set to false otherwise.

### Exit Codes ###
E_VARS_NOT_SET=64
E_COMMANDLINE=65
E_MOUNT_FAIL=70
E_NOSOURCEDIR=71
E_UNMOUNTED=72
E_BACKUP=73
##### END CONFIGURATION SECTION ##############################################


# Check that all the important variables have been set:
if [ -z "$LOCAL_USER" ] ||
   [ -z "$SOURCE_DIR" ] ||
   [ -z "$MOUNT_POINT" ]  ||
   [ -z "$BACKUP_DEST_DIR" ]
then
   echo 'One of the variables is not set! Edit the file: $0. BACKUP FAILED.'
   exit $E_VARS_NOT_SET
fi

if [ "$#" != 0 ]  # If command-line param(s) . . .
then              # Here document(ation).
  cat <<-ENDOFTEXT
    Automatic Nightly backup run from cron.
    Read the source for more details: $0
    The backup directory is $BACKUP_DEST_DIR .
    It will be created if necessary; initialisation is no longer required.

    WARNING: Contents of $BACKUP_DEST_DIR are rotated.
    Directories named 'backup.\$i' will eventually be DELETED.
    We keep backups from every day for 7 days (1-8),
    then every week for 4 weeks (9-12),
    then every month for 3 months (13-15).

    You may wish to add this to your crontab using 'crontab -e'
    #  Back up files: $SOURCE_DIR to $BACKUP_DEST_DIR
    #+ every night at 3:15 am
         15 03 * * * /home/$LOCAL_USER/bin/nightly-backup_firewire-hdd.sh

    Don't forget to verify the backups are working,
    especially if you don't read cron's mail!"
	ENDOFTEXT
   exit $E_COMMANDLINE
fi


# Parse the options.
# ==================

if [ "$DRY_RUN" == "true" ]; then
  DRY_RUN="-n"
  echo "WARNING:"
  echo "THIS IS A 'DRY RUN'!"
  echo "No data will actually be transferred!"
else
  DRY_RUN=""
fi

if [ "$VERBOSE" == "true" ]; then
  VERBOSE="-v"
else
  VERBOSE=""
fi

if [ "$COMPRESS" == "true" ]; then
  COMPRESS="-z"
else
  COMPRESS=""
fi


#  Every week (actually of 8 days) and every month,
#+ extra backups are preserved.
DAY_OF_MONTH=`date +%d`            # Day of month (01..31).
if [ $DAY_OF_MONTH = 01 ]; then    # First of month.
  MONTHSTART=true
elif [ $DAY_OF_MONTH = 08 \
    -o $DAY_OF_MONTH = 16 \
    -o $DAY_OF_MONTH = 24 ]; then
    # Day 8,16,24  (use 8, not 7 to better handle 31-day months)
      WEEKSTART=true
fi



#  Check that the HDD is mounted.
#  At least, check that *something* is mounted here!
#  We can use something unique to the device, rather than just guessing
#+ the scsi-id by having an appropriate udev rule in
#+ /etc/udev/rules.d/10-rules.local
#+ and by putting a relevant entry in /etc/fstab.
#  Eg: this udev rule:
# BUS="scsi", KERNEL="sd*", SYSFS{vendor}="WDC WD16",
# SYSFS{model}="00JB-00GVA0     ", NAME="%k", SYMLINK="lacie_1394d%n"

if mount | grep $MOUNT_POINT >/dev/null; then
  echo "Mount point $MOUNT_POINT is indeed mounted. OK"
else
  echo -n "Attempting to mount $MOUNT_POINT..."	
           # If it isn't mounted, try to mount it.
  sudo mount $MOUNT_POINT 2>/dev/null

  if mount | grep $MOUNT_POINT >/dev/null; then
    UNMOUNT_LATER=TRUE
    echo "OK"
    #  Note: Ensure that this is also unmounted
    #+ if we exit prematurely with failure.
  else
    echo "FAILED"
    echo -e "Nothing is mounted at $MOUNT_POINT. BACKUP FAILED!"
    exit $E_MOUNT_FAIL
  fi
fi


# Check that source dir exists and is readable.
if [ ! -r  $SOURCE_DIR ] ; then
  echo "$SOURCE_DIR does not exist, or cannot be read. BACKUP FAILED."
  exit $E_NOSOURCEDIR
fi


# Check that the backup directory structure is as it should be.
# If not, create it.
# Create the subdirectories.
# Note that backup.0 will be created as needed by rsync.

for ((i=1;i<=15;i++)); do
  if [ ! -d $BACKUP_DEST_DIR/backup.$i ]; then
    if /bin/mkdir -p $BACKUP_DEST_DIR/backup.$i ; then
    #  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  No [ ] test brackets. Why?
      echo "Warning: directory $BACKUP_DEST_DIR/backup.$i is missing,"
      echo "or was not initialised. (Re-)creating it."
    else
      echo "ERROR: directory $BACKUP_DEST_DIR/backup.$i"
      echo "is missing and could not be created."
    if  [ "$UNMOUNT_LATER" == "TRUE" ]; then
        # Before we exit, unmount the mount point if necessary.
        cd
	sudo umount $MOUNT_POINT &&
	echo "Unmounted $MOUNT_POINT again. Giving up."
    fi
      exit $E_UNMOUNTED
  fi
fi
done


#  Set the permission to 700 for security
#+ on an otherwise permissive multi-user system.
if ! /bin/chmod 700 $BACKUP_DEST_DIR ; then
  echo "ERROR: Could not set permissions on $BACKUP_DEST_DIR to 700."

  if  [ "$UNMOUNT_LATER" == "TRUE" ]; then
  # Before we exit, unmount the mount point if necessary.
     cd ; sudo umount $MOUNT_POINT && echo "Unmounted $MOUNT_POINT again. Giving up."
  fi

  exit $E_UNMOUNTED
fi

# Create the symlink: current -> backup.1 if required.
# A failure here is not critical.
cd $BACKUP_DEST_DIR
if [ ! -h current ] ; then
  if ! /bin/ln -s backup.1 current ; then
    echo "WARNING: could not create symlink current -> backup.1"
  fi
fi


# Now, do the rsync.
echo "Now doing backup with rsync..."
echo "Source dir: $SOURCE_DIR"
echo -e "Backup destination dir: $BACKUP_DEST_DIR\n"


/usr/bin/rsync $DRY_RUN $VERBOSE -a -S --delete --modify-window=60 \
--link-dest=../backup.1 $SOURCE_DIR $BACKUP_DEST_DIR/backup.0/

#  Only warn, rather than exit if the rsync failed,
#+ since it may only be a minor problem.
#  E.g., if one file is not readable, rsync will fail.
#  This shouldn't prevent the rotation.
#  Not using, e.g., `date +%a`  since these directories
#+ are just full of links and don't consume *that much* space.

if [ $? != 0 ]; then
  BACKUP_JUSTINCASE=backup.`date +%F_%T`.justincase
  echo "WARNING: the rsync process did not entirely succeed."
  echo "Something might be wrong. Saving an extra copy at: $BACKUP_JUSTINCASE"
  echo "WARNING: if this occurs regularly, a LOT of space will be consumed,"
  echo "even though these are just hard-links!"
fi

# Save a readme in the backup parent directory.
# Save another one in the recent subdirectory.
echo "Backup of $SOURCE_DIR on `hostname` was last run on \
`date`" > $BACKUP_DEST_DIR/README.txt
echo "This backup of $SOURCE_DIR on `hostname` was created on \
`date`" > $BACKUP_DEST_DIR/backup.0/README.txt

# If we are not in a dry run, rotate the backups.
[ -z "$DRY_RUN" ] &&

  #  Check how full the backup disk is.
  #  Warn if 90%. if 98% or more, we'll probably fail, so give up.
  #  (Note: df can output to more than one line.)
  #  We test this here, rather than before
  #+ so that rsync may possibly have a chance.
  DISK_FULL_PERCENT=`/bin/df $BACKUP_DEST_DIR |
  tr "\n" ' ' | awk '{print $12}' | grep -oE [0-9]+ `
  echo "Disk space check on backup partition \
  $MOUNT_POINT $DISK_FULL_PERCENT% full."
  if [ $DISK_FULL_PERCENT -gt 90 ]; then
    echo "Warning: Disk is greater than 90% full."
  fi
  if [ $DISK_FULL_PERCENT -gt 98 ]; then
    echo "Error: Disk is full! Giving up."
      if  [ "$UNMOUNT_LATER" == "TRUE" ]; then
        # Before we exit, unmount the mount point if necessary.
        cd; sudo umount $MOUNT_POINT &&
        echo "Unmounted $MOUNT_POINT again. Giving up."
      fi
    exit $E_UNMOUNTED
  fi


 # Create an extra backup.
 # If this copy fails, give up.
 if [ -n "$BACKUP_JUSTINCASE" ]; then
   if ! /bin/cp -al $BACKUP_DEST_DIR/backup.0 $BACKUP_DEST_DIR/$BACKUP_JUSTINCASE
   then
     echo "ERROR: Failed to create extra copy \
     $BACKUP_DEST_DIR/$BACKUP_JUSTINCASE"
     if  [ "$UNMOUNT_LATER" == "TRUE" ]; then
       # Before we exit, unmount the mount point if necessary.
       cd ;sudo umount $MOUNT_POINT &&
       echo "Unmounted $MOUNT_POINT again. Giving up."
     fi
     exit $E_UNMOUNTED
   fi
 fi


 # At start of month, rotate the oldest 8.
 if [ "$MONTHSTART" == "true" ]; then
   echo -e "\nStart of month. \
   Removing oldest backup: $BACKUP_DEST_DIR/backup.15"  &&
   /bin/rm -rf  $BACKUP_DEST_DIR/backup.15  &&
   echo "Rotating monthly,weekly backups: \
   $BACKUP_DEST_DIR/backup.[8-14] -> $BACKUP_DEST_DIR/backup.[9-15]"  &&
     /bin/mv $BACKUP_DEST_DIR/backup.14 $BACKUP_DEST_DIR/backup.15  &&
     /bin/mv $BACKUP_DEST_DIR/backup.13 $BACKUP_DEST_DIR/backup.14  &&
     /bin/mv $BACKUP_DEST_DIR/backup.12 $BACKUP_DEST_DIR/backup.13  &&
     /bin/mv $BACKUP_DEST_DIR/backup.11 $BACKUP_DEST_DIR/backup.12  &&
     /bin/mv $BACKUP_DEST_DIR/backup.10 $BACKUP_DEST_DIR/backup.11  &&
     /bin/mv $BACKUP_DEST_DIR/backup.9 $BACKUP_DEST_DIR/backup.10  &&
     /bin/mv $BACKUP_DEST_DIR/backup.8 $BACKUP_DEST_DIR/backup.9

 # At start of week, rotate the second-oldest 4.
 elif [ "$WEEKSTART" == "true" ]; then
   echo -e "\nStart of week. \
   Removing oldest weekly backup: $BACKUP_DEST_DIR/backup.12"  &&
   /bin/rm -rf  $BACKUP_DEST_DIR/backup.12  &&

   echo "Rotating weekly backups: \
   $BACKUP_DEST_DIR/backup.[8-11] -> $BACKUP_DEST_DIR/backup.[9-12]"  &&
     /bin/mv $BACKUP_DEST_DIR/backup.11 $BACKUP_DEST_DIR/backup.12  &&
     /bin/mv $BACKUP_DEST_DIR/backup.10 $BACKUP_DEST_DIR/backup.11  &&
     /bin/mv $BACKUP_DEST_DIR/backup.9 $BACKUP_DEST_DIR/backup.10  &&
     /bin/mv $BACKUP_DEST_DIR/backup.8 $BACKUP_DEST_DIR/backup.9

 else
   echo -e "\nRemoving oldest daily backup: $BACKUP_DEST_DIR/backup.8"  &&
     /bin/rm -rf  $BACKUP_DEST_DIR/backup.8

 fi  &&

 # Every day, rotate the newest 8.
 echo "Rotating daily backups: \
 $BACKUP_DEST_DIR/backup.[1-7] -> $BACKUP_DEST_DIR/backup.[2-8]"  &&
     /bin/mv $BACKUP_DEST_DIR/backup.7 $BACKUP_DEST_DIR/backup.8  &&
     /bin/mv $BACKUP_DEST_DIR/backup.6 $BACKUP_DEST_DIR/backup.7  &&
     /bin/mv $BACKUP_DEST_DIR/backup.5 $BACKUP_DEST_DIR/backup.6  &&
     /bin/mv $BACKUP_DEST_DIR/backup.4 $BACKUP_DEST_DIR/backup.5  &&
     /bin/mv $BACKUP_DEST_DIR/backup.3 $BACKUP_DEST_DIR/backup.4  &&
     /bin/mv $BACKUP_DEST_DIR/backup.2 $BACKUP_DEST_DIR/backup.3  &&
     /bin/mv $BACKUP_DEST_DIR/backup.1 $BACKUP_DEST_DIR/backup.2  &&
     /bin/mv $BACKUP_DEST_DIR/backup.0 $BACKUP_DEST_DIR/backup.1  &&

 SUCCESS=true


if  [ "$UNMOUNT_LATER" == "TRUE" ]; then
  # Unmount the mount point if it wasn't mounted to begin with.
  cd ; sudo umount $MOUNT_POINT && echo "Unmounted $MOUNT_POINT again."
fi


if [ "$SUCCESS" == "true" ]; then
  echo 'SUCCESS!'
  exit 0
fi

# Should have already exited if backup worked.
echo 'BACKUP FAILED! Is this just a dry run? Is the disk full?) '
exit $E_BACKUP

Esempio A-33. Il comando cd esteso

###########################################################################
#
#       cdll
#       by Phil Braham
#
#       ############################################
#       Latest version of this script available from
#       http://freshmeat.net/projects/cd/
#       ############################################
#
#       .cd_new
#
#       An enhancement of the Unix cd command
#
#       There are unlimited stack entries and special entries. The stack
#       entries keep the last cd_maxhistory
#       directories that have been used. The special entries can be
#       assigned to commonly used directories.
#
#       The special entries may be pre-assigned by setting the environment
#       variables CDSn or by using the -u or -U command.
#
#       The following is a suggestion for the .profile file:
#
#               . cdll              #  Set up the cd command
#       alias cd='cd_new'           #  Replace the cd command
#               cd -U               #  Upload pre-assigned entries for
#                                   #+ the stack and special entries
#               cd -D               #  Set non-default mode
#               alias @="cd_new @"  #  Allow @ to be used to get history
#
#       For help type:
#
#               cd -h or
#               cd -H
#
#
###########################################################################
#
#       Version 1.2.1
#
#       Written by Phil Braham - Realtime Software Pty Ltd
#       (realtime@mpx.com.au)
#       Please send any suggestions or enhancements to the author (also at
#       phil@braham.net)
#
############################################################################

cd_hm ()
{
        ${PRINTF} "%s" "cd [dir] [0-9] [@[s|h] [-g [<dir>]] [-d] \
[-D] [-r<n>] [dir|0-9] [-R<n>] [<dir>|0-9]
   [-s<n>] [-S<n>] [-u] [-U] [-f] [-F] [-h] [-H] [-v]
    <dir> Go to directory
    0-n         Go to previous directory (0 is previous, 1 is last but 1 etc)
                n is up to max history (default is 50)
    @           List history and special entries
    @h          List history entries
    @s          List special entries
    -g [<dir>]  Go to literal name (bypass special names)
                This is to allow access to dirs called '0','1','-h' etc
    -d          Change default action - verbose. (See note)
    -D          Change default action - silent. (See note)
    -s<n> Go to the special entry <n>*
    -S<n> Go to the special entry <n>
                and replace it with the current dir*
    -r<n> [<dir>] Go to directory <dir>
                              and then put it on special entry <n>*
    -R<n> [<dir>] Go to directory <dir>
                              and put current dir on special entry <n>*
    -a<n>       Alternative suggested directory. See note below.
    -f [<file>] File entries to <file>.
    -u [<file>] Update entries from <file>.
                If no filename supplied then default file
                (${CDPath}${2:-"$CDFile"}) is used
                -F and -U are silent versions
    -v          Print version number
    -h          Help
    -H          Detailed help

    *The special entries (0 - 9) are held until log off, replaced by another
     entry or updated with the -u command

    Alternative suggested directories:
    If a directory is not found then CD will suggest any
    possibilities. These are directories starting with the same letters
    and if any are found they are listed prefixed with -a<n>
    where <n> is a number.
    It's possible to go to the directory by entering cd -a<n>
    on the command line.
    
    The directory for -r<n> or -R<n> may be a number.
    For example:
        $ cd -r3 4  Go to history entry 4 and put it on special entry 3
        $ cd -R3 4  Put current dir on the special entry 3
                    and go to history entry 4
        $ cd -s3    Go to special entry 3
    
    Note that commands R,r,S and s may be used without a number
    and refer to 0:
        $ cd -s     Go to special entry 0
        $ cd -S     Go to special entry 0 and make special
                    entry 0 current dir
        $ cd -r 1   Go to history entry 1 and put it on special entry 0
        $ cd -r     Go to history entry 0 and put it on special entry 0
    "
        if ${TEST} "$CD_MODE" = "PREV"
        then
                ${PRINTF} "$cd_mnset"
        else
                ${PRINTF} "$cd_mset"
        fi
}

cd_Hm ()
{
        cd_hm
        ${PRINTF} "%s" "
        The previous directories (0-$cd_maxhistory) are stored in the
        environment variables CD[0] - CD[$cd_maxhistory]
        Similarly the special directories S0 - $cd_maxspecial are in
        the environment variable CDS[0] - CDS[$cd_maxspecial]
        and may be accessed from the command line

        The default pathname for the -f and -u commands is $CDPath
        The default filename for the -f and -u commands is $CDFile

        Set the following environment variables:
            CDL_PROMPTLEN  - Set to the length of prompt you require.
                Prompt string is set to the right characters of the
                current directory.
                If not set then prompt is left unchanged
            CDL_PROMPT_PRE - Set to the string to prefix the prompt.
                Default is:
                    non-root:  \"\\[\\e[01;34m\\]\"  (sets colour to blue).
                    root:      \"\\[\\e[01;31m\\]\"  (sets colour to red).
            CDL_PROMPT_POST    - Set to the string to suffix the prompt.
                Default is:
                    non-root:  \"\\[\\e[00m\\]$\"
                                (resets colour and displays $).
                    root:      \"\\[\\e[00m\\]#\"
                                (resets colour and displays #).
            CDPath - Set the default path for the -f & -u options.
                     Default is home directory
            CDFile - Set the default filename for the -f & -u options.
                     Default is cdfile
        
"
    cd_version

}

cd_version ()
{
 printf "Version: ${VERSION_MAJOR}.${VERSION_MINOR} Date: ${VERSION_DATE}\n"
}

#
# Truncate right.
#
# params:
#   p1 - string
#   p2 - length to truncate to
#
# returns string in tcd
#
cd_right_trunc ()
{
    local tlen=${2}
    local plen=${#1}
    local str="${1}"
    local diff
    local filler="<--"
    if ${TEST} ${plen} -le ${tlen}
    then
        tcd="${str}"
    else
        let diff=${plen}-${tlen}
        elen=3
        if ${TEST} ${diff} -le 2
        then
            let elen=${diff}
        fi
        tlen=-${tlen}
        let tlen=${tlen}+${elen}
        tcd=${filler:0:elen}${str:tlen}
    fi
}

#
# Three versions of do history:
#    cd_dohistory  - packs history and specials side by side
#    cd_dohistoryH - Shows only hstory
#    cd_dohistoryS - Shows only specials
#
cd_dohistory ()
{
    cd_getrc
        ${PRINTF} "History:\n"
    local -i count=${cd_histcount}
    while ${TEST} ${count} -ge 0
    do
        cd_right_trunc "${CD[count]}" ${cd_lchar}
            ${PRINTF} "%2d %-${cd_lchar}.${cd_lchar}s " ${count} "${tcd}"

        cd_right_trunc "${CDS[count]}" ${cd_rchar}
            ${PRINTF} "S%d %-${cd_rchar}.${cd_rchar}s\n" ${count} "${tcd}"
        count=${count}-1
    done
}

cd_dohistoryH ()
{
    cd_getrc
        ${PRINTF} "History:\n"
        local -i count=${cd_maxhistory}
        while ${TEST} ${count} -ge 0
        do
          ${PRINTF} "${count} %-${cd_flchar}.${cd_flchar}s\n" ${CD[$count]}
          count=${count}-1
        done
}

cd_dohistoryS ()
{
    cd_getrc
        ${PRINTF} "Specials:\n"
        local -i count=${cd_maxspecial}
        while ${TEST} ${count} -ge 0
        do
          ${PRINTF} "S${count} %-${cd_flchar}.${cd_flchar}s\n" ${CDS[$count]}
          count=${count}-1
        done
}

cd_getrc ()
{
    cd_flchar=$(stty -a | awk -F \;
    '/rows/ { print $2 $3 }' | awk -F \  '{ print $4 }')
    if ${TEST} ${cd_flchar} -ne 0
    then
        cd_lchar=${cd_flchar}/2-5
        cd_rchar=${cd_flchar}/2-5
            cd_flchar=${cd_flchar}-5
    else
            cd_flchar=${FLCHAR:=75}
	    # cd_flchar is used for for the @s & @h history
            cd_lchar=${LCHAR:=35}
            cd_rchar=${RCHAR:=35}
    fi
}

cd_doselection ()
{
        local -i nm=0
        cd_doflag="TRUE"
        if ${TEST} "${CD_MODE}" = "PREV"
        then
                if ${TEST} -z "$cd_npwd"
                then
                        cd_npwd=0
                fi
        fi
        tm=$(echo "${cd_npwd}" | cut -b 1)
    if ${TEST} "${tm}" = "-"
    then
        pm=$(echo "${cd_npwd}" | cut -b 2)
        nm=$(echo "${cd_npwd}" | cut -d $pm -f2)
        case "${pm}" in
             a) cd_npwd=${cd_sugg[$nm]} ;;
             s) cd_npwd="${CDS[$nm]}" ;;
             S) cd_npwd="${CDS[$nm]}" ; CDS[$nm]=`pwd` ;;
             r) cd_npwd="$2" ; cd_specDir=$nm ; cd_doselection "$1" "$2";;
             R) cd_npwd="$2" ; CDS[$nm]=`pwd` ; cd_doselection "$1" "$2";;
        esac
    fi

    if ${TEST} "${cd_npwd}" != "." -a "${cd_npwd}" \
!= ".." -a "${cd_npwd}" -le ${cd_maxhistory} >>/dev/null 2>&1
    then
      cd_npwd=${CD[$cd_npwd]}
     else
       case "$cd_npwd" in
                @)  cd_dohistory ; cd_doflag="FALSE" ;;
               @h) cd_dohistoryH ; cd_doflag="FALSE" ;;
               @s) cd_dohistoryS ; cd_doflag="FALSE" ;;
               -h) cd_hm ; cd_doflag="FALSE" ;;
               -H) cd_Hm ; cd_doflag="FALSE" ;;
               -f) cd_fsave "SHOW" $2 ; cd_doflag="FALSE" ;;
               -u) cd_upload "SHOW" $2 ; cd_doflag="FALSE" ;;
               -F) cd_fsave "NOSHOW" $2 ; cd_doflag="FALSE" ;;
               -U) cd_upload "NOSHOW" $2 ; cd_doflag="FALSE" ;;
               -g) cd_npwd="$2" ;;
               -d) cd_chdefm 1; cd_doflag="FALSE" ;;
               -D) cd_chdefm 0; cd_doflag="FALSE" ;;
               -r) cd_npwd="$2" ; cd_specDir=0 ; cd_doselection "$1" "$2";;
               -R) cd_npwd="$2" ; CDS[0]=`pwd` ; cd_doselection "$1" "$2";;
               -s) cd_npwd="${CDS[0]}" ;;
               -S) cd_npwd="${CDS[0]}"  ; CDS[0]=`pwd` ;;
               -v) cd_version ; cd_doflag="FALSE";;
       esac
    fi
}

cd_chdefm ()
{
        if ${TEST} "${CD_MODE}" = "PREV"
        then
                CD_MODE=""
                if ${TEST} $1 -eq 1
                then
                        ${PRINTF} "${cd_mset}"
                fi
        else
                CD_MODE="PREV"
                if ${TEST} $1 -eq 1
                then
                        ${PRINTF} "${cd_mnset}"
                fi
        fi
}

cd_fsave ()
{
        local sfile=${CDPath}${2:-"$CDFile"}
        if ${TEST} "$1" = "SHOW"
        then
                ${PRINTF} "Saved to %s\n" $sfile
        fi
        ${RM} -f ${sfile}
        local -i count=0
        while ${TEST} ${count} -le ${cd_maxhistory}
        do
                echo "CD[$count]=\"${CD[$count]}\"" >> ${sfile}
                count=${count}+1
        done
        count=0
        while ${TEST} ${count} -le ${cd_maxspecial}
        do
                echo "CDS[$count]=\"${CDS[$count]}\"" >> ${sfile}
                count=${count}+1
        done
}

cd_upload ()
{
        local sfile=${CDPath}${2:-"$CDFile"}
        if ${TEST} "${1}" = "SHOW"
        then
                ${PRINTF} "Loading from %s\n" ${sfile}
        fi
        . ${sfile}
}

cd_new ()
{
    local -i count
    local -i choose=0

        cd_npwd="${1}"
        cd_specDir=-1
        cd_doselection "${1}" "${2}"

        if ${TEST} ${cd_doflag} = "TRUE"
        then
                if ${TEST} "${CD[0]}" != "`pwd`"
                then
                        count=$cd_maxhistory
                        while ${TEST} $count -gt 0
                        do
                                CD[$count]=${CD[$count-1]}
                                count=${count}-1
                        done
                        CD[0]=`pwd`
                fi
                command cd "${cd_npwd}" 2>/dev/null
        if ${TEST} $? -eq 1
        then
            ${PRINTF} "Unknown dir: %s\n" "${cd_npwd}"
            local -i ftflag=0
            for i in "${cd_npwd}"*
            do
                if ${TEST} -d "${i}"
                then
                    if ${TEST} ${ftflag} -eq 0
                    then
                        ${PRINTF} "Suggest:\n"
                        ftflag=1
                fi
                    ${PRINTF} "\t-a${choose} %s\n" "$i"
                                        cd_sugg[$choose]="${i}"
                    choose=${choose}+1
        fi
            done
        fi
        fi

        if ${TEST} ${cd_specDir} -ne -1
        then
                CDS[${cd_specDir}]=`pwd`
        fi

        if ${TEST} ! -z "${CDL_PROMPTLEN}"
        then
        cd_right_trunc "${PWD}" ${CDL_PROMPTLEN}
            cd_rp=${CDL_PROMPT_PRE}${tcd}${CDL_PROMPT_POST}
                export PS1="$(echo -ne ${cd_rp})"
        fi
}
#########################################################################
#                                                                       #
#                        Initialisation here                            #
#                                                                       #
#########################################################################
#
VERSION_MAJOR="1"
VERSION_MINOR="2.1"
VERSION_DATE="24-MAY-2003"
#
alias cd=cd_new
#
# Set up commands
RM=/bin/rm
TEST=test
PRINTF=printf              # Use builtin printf

#########################################################################
#                                                                       #
# Change this to modify the default pre- and post prompt strings.       #
# These only come into effect if CDL_PROMPTLEN is set.                  #
#                                                                       #
#########################################################################
if ${TEST} ${EUID} -eq 0
then
#   CDL_PROMPT_PRE=${CDL_PROMPT_PRE:="$HOSTNAME@"}
    CDL_PROMPT_PRE=${CDL_PROMPT_PRE:="\\[\\e[01;31m\\]"}  # Root is in red
    CDL_PROMPT_POST=${CDL_PROMPT_POST:="\\[\\e[00m\\]#"}
else
    CDL_PROMPT_PRE=${CDL_PROMPT_PRE:="\\[\\e[01;34m\\]"}  # Users in blue
    CDL_PROMPT_POST=${CDL_PROMPT_POST:="\\[\\e[00m\\]$"}
fi
#########################################################################
#
# cd_maxhistory defines the max number of history entries allowed.
typeset -i cd_maxhistory=50

#########################################################################
#
# cd_maxspecial defines the number of special entries.
typeset -i cd_maxspecial=9
#
#
#########################################################################
#
#  cd_histcount defines the number of entries displayed in
#+ the history command.
typeset -i cd_histcount=9
#
#########################################################################
export CDPath=${HOME}/
#  Change these to use a different                                      #
#+ default path and filename                                            #
export CDFile=${CDFILE:=cdfile}           # for the -u and -f commands  #
#
#########################################################################
                                                                        #
typeset -i cd_lchar cd_rchar cd_flchar
                        #  This is the number of chars to allow for the #
cd_flchar=${FLCHAR:=75} #+ cd_flchar is used for for the @s & @h history#

typeset -ax CD CDS
#
cd_mset="\n\tDefault mode is now set - entering cd with no parameters \
has the default action\n\tUse cd -d or -D for cd to go to \
previous directory with no parameters\n"
cd_mnset="\n\tNon-default mode is now set - entering cd with no \
parameters is the same as entering cd 0\n\tUse cd -d or \
-D to change default cd action\n"

# ==================================================================== #



: <<DOCUMENTATION

Written by Phil Braham. Realtime Software Pty Ltd.
Released under GNU license. Free to use. Please pass any modifications
or comments to the author Phil Braham:

realtime@mpx.com.au
=======================================================================

cdll is a replacement for cd and incorporates similar functionality to
the bash pushd and popd commands but is independent of them.

This version of cdll has been tested on Linux using Bash. It will work
on most Linux versions but will probably not work on other shells without
modification.

Introduction
============

cdll allows easy moving about between directories. When changing to a new
directory the current one is automatically put onto a stack. By default
50 entries are kept, but this is configurable. Special directories can be
kept for easy access - by default up to 10, but this is configurable. The
most recent stack entries and the special entries can be easily viewed.

The directory stack and special entries can be saved to, and loaded from,
a file. This allows them to be set up on login, saved before logging out
or changed when moving project to project.

In addition, cdll provides a flexible command prompt facility that allows,
for example, a directory name in colour that is truncated from the left
if it gets too long.


Setting up cdll
===============

Copy cdll to either your local home directory or a central directory
such as /usr/bin (this will require root access).

Copy the file cdfile to your home directory. It will require read and
write access. This a default file that contains a directory stack and
special entries.

To replace the cd command you must add commands to your login script.
The login script is one or more of:

    /etc/profile
    ~/.bash_profile
    ~/.bash_login
    ~/.profile
    ~/.bashrc
    /etc/bash.bashrc.local
    
To setup your login, ~/.bashrc is recommended, for global (and root) setup
add the commands to /etc/bash.bashrc.local
    
To set up on login, add the command:
    . <dir>/cdll
For example if cdll is in your local home directory:
    . ~/cdll
If in /usr/bin then:
    . /usr/bin/cdll

If you want to use this instead of the buitin cd command then add:
    alias cd='cd_new'
We would also recommend the following commands:
    alias @='cd_new @'
    cd -U
    cd -D

If you want to use cdll's prompt facilty then add the following:
    CDL_PROMPTLEN=nn
Where nn is a number described below. Initially 99 would be suitable
number.

Thus the script looks something like this:

    ######################################################################
    # CD Setup
    ######################################################################
    CDL_PROMPTLEN=21        # Allow a prompt length of up to 21 characters
    . /usr/bin/cdll         # Initialise cdll
    alias cd='cd_new'       # Replace the built in cd command
    alias @='cd_new @'      # Allow @ at the prompt to display history
    cd -U                   # Upload directories
    cd -D                   # Set default action to non-posix
    ######################################################################

The full meaning of these commands will become clear later.

There are a couple of caveats. If another program changes the directory
without calling cdll, then the directory won't be put on the stack and
also if the prompt facility is used then this will not be updated. Two
programs that can do this are pushd and popd. To update the prompt and
stack simply enter:

    cd .
    
Note that if the previous entry on the stack is the current directory
then the stack is not updated.

Usage
=====  
cd [dir] [0-9] [@[s|h] [-g <dir>] [-d] [-D] [-r<n>]
   [dir|0-9] [-R<n>] [<dir>|0-9] [-s<n>] [-S<n>]
   [-u] [-U] [-f] [-F] [-h] [-H] [-v]

    <dir>       Go to directory
    0-n         Goto previous directory (0 is previous,
                1 is last but 1, etc.)
                n is up to max history (default is 50)
    @           List history and special entries (Usually available as $ @)
    @h          List history entries
    @s          List special entries
    -g [<dir>]  Go to literal name (bypass special names)
                This is to allow access to dirs called '0','1','-h' etc
    -d          Change default action - verbose. (See note)
    -D          Change default action - silent. (See note)
    -s<n>       Go to the special entry <n>
    -S<n>       Go to the special entry <n>
                      and replace it with the current dir
    -r<n> [<dir>] Go to directory <dir>
                              and then put it on special entry <n>
    -R<n> [<dir>] Go to directory <dir>
                              and put current dir on special entry <n>
    -a<n>       Alternative suggested directory. See note below.
    -f [<file>] File entries to <file>.
    -u [<file>] Update entries from <file>.
                If no filename supplied then default file (~/cdfile) is used
                -F and -U are silent versions
    -v          Print version number
    -h          Help
    -H          Detailed help



Examples
========

These examples assume non-default mode is set (that is, cd with no
parameters will go to the most recent stack directory), that aliases
have been set up for cd and @ as described above and that cd's prompt
facility is active and the prompt length is 21 characters.

    /home/phil$ @
    # List the entries with the @
    History:
    # Output of the @ command
    .....
    # Skipped these entries for brevity
    1 /home/phil/ummdev               S1 /home/phil/perl
    # Most recent two history entries
    0 /home/phil/perl/eg              S0 /home/phil/umm/ummdev
    # and two special entries are shown
    
    /home/phil$ cd /home/phil/utils/Cdll
    # Now change directories
    /home/phil/utils/Cdll$ @
    # Prompt reflects the directory.
    History:
    # New history
    .....   
    1 /home/phil/perl/eg              S1 /home/phil/perl
    # History entry 0 has moved to 1
    0 /home/phil                      S0 /home/phil/umm/ummdev
    # and the most recent has entered
       
To go to a history entry:

    /home/phil/utils/Cdll$ cd 1
    # Go to history entry 1.
    /home/phil/perl/eg$
    # Current directory is now what was 1
    
To go to a special entry:

    /home/phil/perl/eg$ cd -s1
    # Go to special entry 1
    /home/phil/umm/ummdev$
    # Current directory is S1

To go to a directory called, for example, 1:

    /home/phil$ cd -g 1
    # -g ignores the special meaning of 1
    /home/phil/1$
    
To put current directory on the special list as S1:
    cd -r1 .        #  OR
    cd -R1 .        #  These have the same effect if the directory is
                    #+ . (the current directory)

To go to a directory and add it as a special  
  The directory for -r<n> or -R<n> may be a number.
  For example:
        $ cd -r3 4  Go to history entry 4 and put it on special entry 3
        $ cd -R3 4  Put current dir on the special entry 3 and go to
                    history entry 4
        $ cd -s3    Go to special entry 3

    Note that commands R,r,S and s may be used without a number and
    refer to 0:
        $ cd -s     Go to special entry 0
        $ cd -S     Go to special entry 0 and make special entry 0
                    current dir
        $ cd -r 1   Go to history entry 1 and put it on special entry 0
        $ cd -r     Go to history entry 0 and put it on special entry 0


    Alternative suggested directories:

    If a directory is not found, then CD will suggest any
    possibilities. These are directories starting with the same letters
    and if any are found they are listed prefixed with -a<n>
    where <n> is a number. It's possible to go to the directory
    by entering cd -a<n> on the command line.

        Use cd -d or -D to change default cd action. cd -H will show
        current action.

        The history entries (0-n) are stored in the environment variables
        CD[0] - CD[n]
        Similarly the special directories S0 - 9 are in the environment
        variable CDS[0] - CDS[9]
        and may be accessed from the command line, for example:
        
            ls -l ${CDS[3]}
            cat ${CD[8]}/file.txt

        The default pathname for the -f and -u commands is ~
        The default filename for the -f and -u commands is cdfile


Configuration
=============

    The following environment variables can be set:
    
        CDL_PROMPTLEN  - Set to the length of prompt you require.
            Prompt string is set to the right characters of the current
            directory. If not set, then prompt is left unchanged. Note
            that this is the number of characters that the directory is
            shortened to, not the total characters in the prompt.

            CDL_PROMPT_PRE - Set to the string to prefix the prompt.
                Default is:
                    non-root:  "\\[\\e[01;34m\\]"  (sets colour to blue).
                    root:      "\\[\\e[01;31m\\]"  (sets colour to red).

            CDL_PROMPT_POST    - Set to the string to suffix the prompt.
                Default is:
                    non-root:  "\\[\\e[00m\\]$"
                               (resets colour and displays $).
                    root:      "\\[\\e[00m\\]#"
                               (resets colour and displays #).

        Note:
            CDL_PROMPT_PRE & _POST only t

        CDPath - Set the default path for the -f & -u options.
                 Default is home directory
        CDFile - Set the default filename for the -f & -u options.
                 Default is cdfile


    There are three variables defined in the file cdll which control the
    number of entries stored or displayed. They are in the section labeled
    'Initialisation here' towards the end of the file.

        cd_maxhistory       - The number of history entries stored.
                              Default is 50.
        cd_maxspecial       - The number of special entries allowed.
                              Default is 9.
        cd_histcount        - The number of history and special entries
                              displayed. Default is 9.

    Note that cd_maxspecial should be >= cd_histcount to avoid displaying
    special entries that can't be set.


Version: 1.2.1 Date: 24-MAY-2003

DOCUMENTATION

Per terminare la sezione, un ripasso dei fondamenti . . . ed altro.

Esempio A-34. Fondamenti rivisitati

#!/bin/bash
# basics-reviewed.bash

# Per i file, l'estenzione == *.bash è specifica di Bash

#   Copyright (c) Michael S. Zick, 2003; Tutti i diritti riservati.
#   Licenza: Utilizzabile in qualsiasi forma e per qualsiasi scopo.
#   Revisione: $ID$
#
#           Dimostrazione commentata da M.C.
#   (autore de "Guida Avanzata di Scripting Bash")


#  Lo script è stato provato con le versioni Bash 2.04, 2.05a e 2.05b.
#  Potrebbe non funzionare con versioni precedenti.
#  Questo script dimostrativo genera il messaggio d'errore 
#+ --intenzionale-- "command not found". Vedi riga 407.

#  Chet Ramey, attuale manutentore di Bash, ha risolto i problemi qui 
#+ evidenziati. Le soluzioni verranno inserite in una prossima versione 
#+ di Bash.



        ###--------------------------------------------###
        ###  Collegate con una pipe lo script a 'more',###
        ###+ per evitare che la visualizzazione        ###
        ###+ dell'output scorra oltre lo schermo.      ###
        ###                                            ###
        ###  Potete anche redirigere l'output in un    ###
        ###+ file per un esame successivo.             ###  
        ###--------------------------------------------###



#  La maggior parte dei punti trattati di seguito sono stati dettagliatamente
#+ spiegati nella parte precedente della "Guida Avanzata di Scripting Bash."
#  Questo è uno script dimostrativo, una presentazione riorganizzata
#+ di alcuni degli argomenti trattati nel libro.
#      -- msz

# Se non diversamente specificate, le variabili non sono tipizzate.

#  Le variabili hanno dei nomi. I nomi devono essere formati da caratteri 
#+ diversi dalle cifre.
#  I nomi dei descrittori dei file (come, per esempio, 2>&1)
#+ sono formati SOLO da cifre.

# I parametri e gli elementi degli array Bash sono numerati
# (I parametri somo molto simili agli array Bash.)

# Una variabile può non essere definita (referenziazione nulla).
unset VarNulla

# Una vaiabile può essere definita, ma essere vuota (contenuto nullo).
VarVuota=''                         # Due apici singoli consecutivi.

# Una variabile può essere definita e inizializzata.
VarQualcosa='Letterale'

# Una variabile può contenere:
#   * Un intero di 32 bit (o maggiore) del tipo signed
#   * Una stringa
# Una variabile può anche essere un array.

#  Nel caso una stringa contenga degli spazi la si può trattare
#+ come se fosse il nome di una funzione seguito dai suoi argomenti.

#  I nomi delle variabili e quelli delle funzioni
#+ sono collocati in namespace differenti.*


#  È possibile definire un array Bash sia esplicitamente che implicitamente
#+ in base alla sintassi dell'enunciato di assegnamento utizzata.
#  Modo esplicito:
declare -a Array



# Il comando echo è un built-in.
echo $VarQualcosa

# Il comando printf è un built-in.
# Leggete %s come: Stringa-di-Formato
printf %s $VarQualcosa          #  Nessuna interruzione di riga specificata,
                                #+ nessun output.
echo                            #  Di default, un'interruzione di riga 
                                #+ (riga vuota).



# Bash considera come parola ogni stringa separata da una spaziatura.
# Gli spazi, o la loro mancanza, sono significativi.
# (Questo in generale, naturalmente ci sono delle eccezioni.)




# Leggete il simbolo del DOLLARO come: Contenuto-Di.

# Sintassi-Estesa per Contenuto-Di:
echo ${VarQualcosa}

#  La Sintassi-Estesa ${ ... } consente molto di più di una semplice
#+ referenziazione ad una variabile.
#  In generale, $VarQualcosa può sempre essere scritto 
#+ nella forma: ${VarQualcosa}.

# Richiamate lo script con degli argomenti per vedere all'opera ciò che segue.



#  Il comportamento dei caratteri speciali @ e *,
#+ al di fuori dei doppi apici, è identico.
#  Potete leggerli come: Tutti-Gli-Elementi-Di.

#  Se non viene specificato un nome, essi fanno riferimento
#+ ai parametri pre-definiti.



# Referenziazioni Globali
echo $*                         #  Tutti i parametri passati allo script 
                                #+ o a una funzione
echo ${*}                       #  Stessa cosa

#  Nella referenziazione Globale, Bash disabilita l'espansione 
#+ dei nomi dei file
#  Resta attiva solo la corrispondenza di carattere.


# Referenziazioni Tutti-Gli-Elementi-Di
echo $@                         # Uguale ai precedenti
echo ${@}                       # Uguale ai precedenti




#  All'interno degli apici doppi, il comportamento delle referenziazioni 
#+ Globali dipende dall'impostazione di IFS (Input Field Separator).
#  All'interno degli apici doppi, le referenziazioni Tutti-Gli-Elementi-Di
#+ si comportano in modo uguale.

#  Specificando un nome di variabile contenente una stringa il
#+ riferimento è a tutti gli elemeni (caratteri) della stringa.


#  Per specificare un elemento (carattere) di una stringa,
#+ si DEVE usare la Sintassi-Estesa (vedi sopra).




#  Specificando il nome di un array il riferimento
#+ è all'elemento con indice zero,
#+ NON il PRIMO ELEMENTO DEFINITO né il PRIMO NON VUOTO.

#  Per la referenziazione agli altri elementi è necessaria una notazione
#+ specifica che RICHIEDE la Sintassi-Estesa.
#  La forma generale è: ${nome[indice]}.

#  Per gli Array-Bash, per far riferimento all'elemento con indice zero, 
#+ si può usare il formato stringa: ${nome:indice}


#  Gli Array-Bash vengono implementati internamente come liste collegate, non 
#+ come un'area di memoria fissa, come in alcuni linguaggi di programmazione.


#   Caratteristiche degli array in Bash (Array-Bash):
#   ------------------------------------------------

#   Se non altrimenti specificato, gli indici degli Array-Bash
#+  iniziano dal numero zero. Letteralmente: [0]
#   Questa viene chiamata indicizzazione in base zero.
###
#   Se non altrimenti specificato, gli Array-Bash sono ad indicizzazione 
#+  raggruppata (indici sequenziali senza interruzioni).
###
#   Non sono permessi indici negativi.
###
#   Non è necessario che gli elementi di un Array-Bash siano dello stesso tipo.
###
#   Un Array-Bash può avere degli elementi non definiti 
#+  (referenziazioni nulle). Sono gli Array-Bash a "indicizzazione sparsa."
###
#   Gli elementi di un Array-Bash possono essere definiti, ma vuoti
#   (contenuto nullo).
###
#   Gli elementi di un Array-Bash possono essere:
#     * Un intero di 32-bit (o maggiore) del tipo signed
#     * Una stringa
#     * Una stringa contenente spazi tanto da assomigliare al nome di una 
#     + funzione seguita dai suoi argomenti
###
#       Elementi di un Array-Bash precedentemente definiti
#   +   possono essere successivamente annullati (unset).
#       Questo significa che un Array-Bash ad indicizzazione raggruppata può
#   +   essere trasformato in uno ad indicizzazione sparsa.
###
#   Si possono aggiungere degli elementi ad un Array-Bash semplicemente 
#+  definendo un elemento che non era stato definito precedentemente.
###
# Per queste ragioni si è usato il termine "Array-Bash".
# Da questo punto in avanti verrà riutilizzato il termine generico "array".
#     -- msz




#  Tempo di dimostrazioni -- inizializziamo l'array Array, precedentemente
#+ dichiarato, come array ad indicizzazione sparsa.
#  (Il comando 'unset ... ' viene qui usato a solo scopo dimostrativo.)

unset Array[0]                   # Per l'elemento specificato
Array[1]=uno                     # Stringa letterale senza quoting
Array[2]=''                      # Definito, ma vuoto
unset Array[3]                   # Per l'elemento specificato
Array[4]='quattro'               # Stringa letterale con quoting



# Leggete la stringa di formato %q come: Quoting-Conforme-Alle-Regole-IFS.
echo
echo '- - Al di fuori dei doppi apici - -'
###
printf %q ${Array[*]}            # Referimento Globale Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'${Array[*]}
###
printf %q ${Array[@]}            # Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'${Array[@]}

# L'uso dei doppi apici potrebbe essere letto come: Abilita-Sostituzione.

# Si possono riconoscere cinque casi di impostazione di IFS.

echo
echo "- - Tra doppi apici - Impostazione di default di IFS spazio-tabulazione\
-a_capo - -"
IFS=$'\x20'$'\x09'$'\x0A'        #  Questi tre byte,
                                 #+ esattamente nello stesso ordine.


printf %q "${Array[*]}"          # Riferimento Globale Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[*]}"
###
printf %q "${Array[@]}"          # Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[@]}"


echo
echo '- - Tra doppi apici - Primo carattere di IFS: ^ - -'
#  Qualsiasi carattere stampabile, che non sia una spaziatura,
#+ dovrebbe comportarsi allo stesso modo.
IFS='^'$IFS                      # ^ + spazio tabulazione a_capo
###
printf %q "${Array[*]}"          # Riferimento Globale Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[*]}"
###
printf %q "${Array[@]}"          # Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[@]}"


echo
echo '- - Tra doppi apici - Nessuna spaziatura in IFS - -'
IFS='^:%!'
###
printf %q "${Array[*]}"          # Riferimento Globale Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[*]}"
###
printf %q "${Array[@]}"          # Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[@]}"


echo
echo '- - Tra doppi apici - IFS impostata, ma vuota - -'
IFS=''
###
printf %q "${Array[*]}"          # Riferimento Globale Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[*]}"
###
printf %q "${Array[@]}"          # Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[@]}"


echo
echo '- - Tra doppi apici - IFS non definita - -'
unset IFS
###
printf %q "${Array[*]}"          # Riferimento Globale Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[*]}"
###
printf %q "${Array[@]}"          # Tutti-Gli-Elementi-Di
echo
echo 'comando echo:'"${Array[@]}"


# Reimposta IFS ai valori di default.
# Precisamnete questi tre byte.
IFS=$'\x20'$'\x09'$'\x0A'        # Esattamente nello stesso ordine.

# Interpretazione dei precedenti output:
#   Il riferimento Globale vale per I/O; contano le impostazioni di IFS.
###
#   Tutti-Gli-Elementi-Di non tiene in considerazione le impostazioni di IFS.
###
#   Notate i differenti output del comando echo e del comando
#+  printf usato con la stringa di formato.


#  Ricordate:
#   I parametri sono simili agli array ed hanno comportamento analogo.
###
#  Gli esempi precedenti dimostrano le possibili variazioni.
#  Per mantenere la struttura di un array sparso, è necessaria
#+ una notazione aggiuntiva.
###
#  Il codice sorgente di Bash possiede una routine che consente
#+ l'assegnamento di un array nella forma [indice]=valore.
#  Dalla versione 2.05b tale routine non viene più usata,
#+ ma questo potrebbe cambiare nelle future release.



# Lunghezza di una stringa, misurata sugli elementi non-nulli (caratteri):
echo
echo '- - Riferimenti senza quoting - -'
echo 'Conteggio dei caratteri Non-Nulli: '${#VarQualcosa}' caratteri.'

# test='Lett'$'\x00''erale'         # $'\x00' è un carattere nullo.
# echo ${#test}                     # Lo vedete?



#  Lunghezza di un array, misurata sugli elementi definiti,
#+ compresi quelli con contenuto nullo.
echo
echo 'Conteggio dei contenuti definiti: '${#Array[@]}' elementi.'
# NON è l'indice massimo (4).
# NON è l'intervallo degli indici (1 . . 4 compresi).
# È la lunghezza della lista collegata.
###
#  Sia l'indice massimo che l'intervallo degli indici possono
#+ essere ricavati con altre istruzioni.

# Lunghezza di una stringa, misurata sugli elementi non-nulli (caratteri):
echo
echo '- - Riferimento Globale con quoting - -'
echo 'Conteggio caratteri Non-Nulli: '"${#VarQualcosa}"' caratteri.'

#  Lunghezza di un array, misurata sugli elementi definiti,
#+ compresi gli elementi con contenuto nullo.
echo
echo 'Conteggio elementi definiti: '"${#Array[*]}"' elementi.'

#  Interpretazione: la sostituzione non ha effetti nell'operazione ${# ... }.
#  Suggerimento:
#  Per i caratteri utilizzate sempre Tutti-Gli-Elementi-Di
#+ se è quello che volete veramente (independente da IFS).



#  Definizione di una semplice funzione.
#  Nel nome è stato inserito il trattino di sottolineatura (underscore)
#+ per distinguerlo dagli esempi precedenti.
###
#  Bash separa i nomi delle variabili e quelli delle funzioni
#+ in differenti namespace.
#  The Mark-One eyeball isn't that advanced.**
###
_semplice() {
    echo -n 'Funzione_semplice'$@   #  Nei risultati non viene
}                                   #+ mai eseguito l'a_capo.


# La notazione ( ... ) invoca un comando o una funzione.
# La notazione $( ... ) va letta come: Risultato-Di.


# Invocazione della funzione _semplice
echo
echo '- - Output della funzione _semplice - -'
_semplice                           # Provate a passare degli argomenti.
echo
# oppure
(_semplice)                         # Provate a passare degli argomenti.
echo

echo '- Esiste una variabile con quel nome? -'
echo $_semplice non definita        # Nessuna variabile con quel nome.

# Invoca il risultato della funzione _semplice 
# (messaggio d'errore intenzionale)

###
$(_semplice)                        # Dà un messaggio d'errore:
#                          line 407: Funzione_semplice: command not found
#                          ----------------------------------------------

echo
###

#  La prima parola del risultato della funzione _semplice
#+ non è un camando Bash valido né il nome di una funzione definita.
###
# Questo dimostra che l'output di _semplice è soggetto a valutazione.
###
# Interpretazione:
#   Una funzione può essere usata per generare dei comandi Bash in-line.


# Una semplice funzione dove la prima parola del risultato È un comando Bash:
###
_print() {
    echo -n 'printf %q '$@
}

echo '- - Risultati della funzione _print - -'
_print param1 param2                # Un output NON un comando.
echo

$(_print param1 param2)             #  Esegue: printf %q param1 param2
                                    #  Vedi i precedenti esempi di IFS
                                    #+ per le diverse possibilità.
echo

$(_print $VarQualcosa)              #  Risultato prevedibile.
echo



# Variabili funzione
# ------------------

echo
echo '- - Variabili funzione - -'
# Una variabile può contenere un intero con segno, una stringa o un array.
# Si può usare una stringa come se fosse il nome di una funzione con i 
# relativi argomenti opzionali.

# set -vx                           #  Se desiterate, abilitatelo
declare -f varFunz                  #+ nel namespace delle funzioni

varFunz=_print                      # Contiene il nome della funzione.
$varFunz param1                     # Uguale a _print.
echo

varFunz=$(_print )                  # Contiene il risultato della funzione.
$varFunz                            # Nessun input, nessun output.
$varFunz $VarQualcosa               # Risultato prevedibile.
echo

varFunz=$(_print $VarQualcosa)      #  QUI $VarQualcosa viene sostituita.
$varFunz                            #  L'espansione è parte del
echo                                #+ contenuto della variabile.

varFunz="$(_print $VarQualcosa)"    #  QUI $VarQualcosa viene sostituita.
$varFunz                            #  L'espansione diventa parte del
echo                                #+ contenuto della variabile.

#  La differenza tra la versione con il quoting e quella senza
#+ la si può analizzare nell'esempio "protect_literal.sh".
#  Nel primo caso viene elaborata come avente due Parole-Bash, senza quoting.
#  Nel secondo come avente un'unica Parola-Bash, con il quoting.




# Sostituzione ritardata
# ----------------------

echo
echo '- - Sostituzione ritardata - -'
varFunz="$(_print '$VarQualcosa')"  # Nessuna sostituzione, Parola-Bash singola.
eval $varFunz                       # QUI $VarQualcosa viene sostituita.
echo

VarQualcosa='Nuovovalore'
eval $varFunz                       # QUI $VarQualcosa viene sostituita.
echo

# Ripristino dell'impostazione originale precedentemente modificata.
VarQualcosa=Letterale

#  Ci sono due funzioni dimostrative negli esempi
#+ "protect_literal.sh" e "unprotect_literal.sh".
#  Si tratta di funzioni generiche per la sostituzione ritardata
#+ di stringhe letterali.





# RIEPILOGO:
# ---------

#  Una stringa può essere considerata come un Classico-Array
#+ di elementi (caratteri).
#  Un'operazione stringa agisce su tutti gli elementi (caratteri) della stringa
#+ (a livello concettuale, almeno).
###
#  La notazione: ${nome_array[@]} rappresenta tutti gli elementi
#+ dell'Array-Bash nome_array.
###
#  Le operazioni stringa con Notazione Estesa si possono applicare a tutti
#+ gli elementi di un array.
###
#  Questo andrebbe letto come operazione Per-Ogni su un vettore di stringhe.
###
#  I parametri sono simili ad un array.
#  L'inizializzazione di un array di parametri per uno script e quella 
#+ di un array di parametri per una funzione differiscono solo per quanto 
#+ riguarda ${0} che, nel primo caso, non cambia mai la propria impostazione.
###
#  L'elemento con indice zero di un array di parametri di uno script 
#+ contiene sempre il nome dello script.
###
#  L'elemento con indice zero di un array di parametri di una funzione NON
#+ contiene il nome della funzione.
#  Il nome della funzione corrente si ottiene dalla variabile $FUNCNAME.
###
#  Ecco un rapido elenco (rapido, non breve).

echo
echo '- - Verifica (ma senza cambiamenti) - -'
echo '- referenziazione nulla -'
echo -n ${VarNulla-'NonImpostata'}' '   # Non impostata
echo ${VarNulla}                        # Solo a_capo
echo -n ${VarNulla:-'NonImpostata'}' '  # Non impostata
echo ${VarNulla}                        # Solo a_capo

echo '- contenuto nullo -'
echo -n ${VarVuota-'Vuota'}' '          # Spazio
echo ${VarVuota}                        # Solo a_capo
echo -n ${VarVuota:-'Vuota'}' '         # Vuota
echo ${VarVuota}                        # Solo a_capo

echo '- impostata -'
echo ${VarQualcosa-'Contenuto'}         # Letterale
echo ${VarQualcosa:-'Contenuto'}        # Letterale

echo '- Array Sparso -'
echo ${Array[@]-'non impostato'}

# Tempo di ASCII-Art
# Stato             S==si,  N==no
#                   -       :-
# Non impostata     S       S       ${# ... } == 0
# Vuota             N       S       ${# ... } == 0
# Impostata         N       N       ${# ... } > 0

#  Sia la prima parte che/o la seconda delle verifiche possono essere formate
#+ da una stringa d'invocazione di un comando o di una funzione.
echo
echo '- - Verifica 1 - indefiniti - -'
declare -i t
_decT() {
    t=$t-1
}

# Referenziazione nulla, t == -1
t=${#VarNulla}                          # Dà come risultato zero.
${VarNulla- _decT }                     # Viene eseguita la funzione, t ora -1.
echo $t

# Contenuto nullo, t == 0
t=${#VarVuota}                          # Dà come risultato zero.
${VarVuota- _decT }                     # La funzione _decT NON viene eseguita.
echo $t

# Contenuto valido, t == numero di caratteri non nulli
VarQualcosa='_semplice'                 # Impostata al nome di una funzione.
t=${#VarQualcosa}                       # Lunghezza diversa da zero
${VarQualcosa- _decT }                  # Viene eseguita la funzione _semplice.
echo $t                                 # Notate l'azione Accoda-A.

# Esercizio: sistemate l'esempio precedente.
unset t
unset _decT
VarQualcosa=Letterale

echo
echo '- - Verifica con cambiamenti - -'
echo '- Assegnamento in caso di referenziazione nulla -'
echo -n ${VarNulla='NonImpostata'}' '   # NonImpostata NonImpostata
echo ${VarNulla}
unset VarNulla

echo '- Assegnamento in caso di referenziazione nulla -'
echo -n ${VarNulla:='NonImpostata'}' '  # NonImpostata NonImpostata
echo ${VarNulla}
unset VarNulla

echo '- Nessun assegnamento se il contenuto è nullo -'
echo -n ${VarVuota='Vuota'}' '          # Solo uno spazio
echo ${VarVuota}
VarVuota=''

echo '- Assegnamento in caso di contenuto nullo -'
echo -n ${VarVuota:='Vuota'}' '         # Vuota Vuota
echo ${VarVuota}
VarVuota=''

echo '- Nessuna modifica se il contenuto della variabile non è nullo -'
echo ${VarQualcosa='Contenuto'}         # Letterale
echo ${VarQualcosa:='Contenuto'}        # Letterale


# Array-Bash ad "indicizzazione sparsa"
###
#  In modo predefinito, gli Array-Bash sono ad indicizzazione raggruppata
#+ e iniziano dall'indice zero, se non altrimenti specificato.
###
#  L'inizializzazione di un Array viene effettuata come appena descritto
#+ se non "altrimenti specificato".  Ecco il metodo alternativo:
###
echo
declare -a ArraySparso
ArraySparso=( [1]=uno [2]='' [4]='quattro' )
# [0]=referenziazione nulla, [2]=contenuto nullo, [3]=referenziazione nulla

echo '- - Elencazione di Array-Sparso - -'
# Tra doppi apici, impostazione di default di IFS, Corrispondenza globale

IFS=$'\x20'$'\x09'$'\x0A'
printf %q "${ArraySparso[*]}"
echo

#  Notate che l'output non fa distinzione tra "contenuto nullo"
#+ e "referenziazione nulla".
#  Entrambi vengono visualizzati come spazi preceduti dal carattere di escape.
###
#  Notate ancora che l'output NON visualizza lo spazio preceduto dall'escape
#+ per la/e "referenziazione/i nulla/e" precedenti il primo elemento definito.
###
#  Questo comportamento, delle versioni 2.04, 2.05a e 2.05b, è stato 
#+ segnalato e potrebbe cambiare in una futura versione di Bash.

#  La visualizzazione di un array sparso che mantenga intatta la relazione 
#+ [indice]=valore, richiede un po' di programmazione.
#  Una soluzione possibile è rappresentata dal seguente frammento di codice:
###
# local l=${#ArraySparso[@]}        # Conteggio degli elementi definiti
# local f=0                         # Conteggio degli indici rilevati
# local i=0                         # Indice da verificare
(                                   # Funzione anonima ***
    for (( l=${#ArraySparso[@]}, f = 0, i = 0 ; f < l ; i++ ))
    do
        # 'se definito allora...'
        ${ArraySparso[$i]+ eval echo '\ ['$i']='${ArraySparso[$i]} ; (( f++ )) }
    done
)

#  Il lettore che volesse riflettere sul precedente frammento di codice 
#+ potrebbe dover ripassare "elenco di comandi" e 
#+ "comandi multipli su una riga" nell'antecedente 
#+ "Guida avanzata di scripting Bash."
###
#  Nota:
#  La versione "read -a nome_array" del comando "read"
#+ incomincia l'inizializzazione di nome_array dall'indice zero.
#  ArraySparso non definisce nessun valore per l'elemento con indice zero.
###
#  L'utente che avesse bisogno di leggere/scrivere un array sparso sia da/su un
#+ dispositivo di memorizzazione esterno che da/su un socket di comunicazione
#+ dovrebbe inventarsi un codice di lettura/scrittura adatto allo scopo.
###
# Esercizio: scrivete un tale codice.

unset ArraySparso

echo
echo '- - Condizione alternativa (ma senza modifica)- -'
echo '- Nessuna sostituzione in caso di referenziazione nulla -'
echo -n ${VarNulla+'NonImpostata'}' '
echo ${VarNulla}
unset VarNulla

echo '- Nessuna sostituzione in caso di referenziazione nulla -'
echo -n ${VarNulla:+'NonImpostata'}' '
echo ${VarNulla}
unset VarNulla

echo '- Sostituzione in caso di contenuto nullo -'
echo -n ${VarVuota+'Vuota'}' '              # Vuota
echo ${VarVuota}
VarVuota=''

echo '- Nessuna sostituzione in caso di contenuto nullo -'
echo -n ${VarVuota:+'Vuota'}' '             # Solo uno spazio
echo ${VarVuota}
VarVuota=''

echo '- Sostituzione solo in presenza di un contenuto -'

# Sostituzione letterale
echo -n ${VarQualcosa+'Contenuto'}' '       # Contenuto Letterale
echo ${VarQualcosa}

# Invocazione di funzione
echo -n ${VarQualcosa:+ $(_semplice) }' '   # Funzione_semplice Letterale
echo ${VarQualcosa}
echo

echo '- - Array Sparso - -'
echo ${Array[@]+'Vuoto'}                    # Un array di 'Vuoto'
echo

echo '- - Verifica 2 per gli indefiniti - -'

declare -i t
_incT() {
    t=$t+1
}

#  Nota:
#  Si tratta della stessa verifica impiegata nel frammento di codice
#+ dell'elencazione di un array sparso.

# Referenziazione nulla, t == -1
t=${#VarNulla}-1                    # Risultato: meno uno.
${VarNulla+ _incT }                 # Non viene eseguita.
echo $t' Referenziazione nulla'

# Contenuto nullo, t == 0
t=${#VarVuota}-1                    # Risultato: zero.
${VarVuota+ _incT }                 # Viene eseguita.
echo $t'  Contenuto nullo'

# Contenuto, t == numero di caratteri non-nulli
t=${#VarQualcosa}-1                 # numero dei non-nulli meno uno
${VarQualcosa+ _incT }              # Viene eseguita.
echo $t'  Contenuto'

# Esercizio: sistemate l'esempio precedente.
unset t
unset _incT

#  ${nome?msg_err} ${nome:?msg_err}
#  Queste seguono le stesse regole, ma, nel caso sia stata specificata 
#+ un'azione dopo il punto interrogativo, terminano lo script dopo l'esecuzione 
#+ di tale azione.
#  L'azione che segue il punto interrogativo può essere una stringa
#+ o il risultato di una funzione.
###
#  ${nome?} ${nome:?} sono delle semplici verifiche. Il valore di ritorno può
#+ essere passato a un costrutto condizionale.




# Operazioni sugli elementi
# -------------------------

echo
echo '- - Selezione di elementi dal fondo - -'

#  Stringhe, Array e Parametri posizionali

#  Per effettuare la selezione dei parametri, richiamate lo script
#+ passandogli diversi argomenti.

echo '- Tutti -'
echo ${VarQualcosa:0}               # tutti i caratteri non-nulli
echo ${Array[@]:0}                  # tutti gli elementi con un contenuto
echo ${@:0}                         # tutti i parametri con un contenuto;
                                    # ad eccezione del parametro[0]

echo
echo '- Tutti dopo -'
echo ${VarQualcosa:1}               #  tutti i non-nulli dopo il carattere[0]
echo ${Array[@]:1}                  #  tutti quelli con un contenuto dopo 
                                    #+ l'elemento[0]
echo ${@:2}                         #  i parametri non vuoti dopo param[1]

echo
echo '- Intervallo dopo -'
echo ${VarQualcosa:4:3}             # era
                                    # I tre caratteri che si trovano dopo
                                    # il carattere[3]

echo '- Array sparso -'
echo ${Array[@]:1:2}        #  quattro - L'unico elemento con un contenuto.
                            #  Dopo due elementi (se esistono).
                            #  il PRIMO CON UN CONTENUTO
                            #+ (il PRIMO CON UN CONTENUTO viene
                            #+ considerato come se avesse indice zero).
#  Bash considera SOLO gli elementi dell'array con un CONTENUTO
#  printf %q "${Array[@]:0:3}"      # Provate questo

#  Nelle versioni 2.04, 2.05a e 2.05b,
#+ Bash non tratta nel modo atteso, usando questa notazione, gli array sparsi.
#
#  L'attuale manutentore di Bash, Chet Ramey, correggerà questa anomalia
#+ in una prossima versione.


echo '- Array non-sparso -'
echo ${@:2:2}               # I due parametri successivi al parametro[1]

# Nuovi candidati per gli esempi sui vettori stringa:
stringaZ=abcABC123ABCabc
arrayZ=( abcabc ABCABC 123123 ABCABC abcabc )
sparsoZ=( [1]='abcabc' [3]='ABCABC' [4]='' [5]='123123' )

echo
echo ' - - Stringa cavia - -'$stringaZ'- - '
echo ' - - Array cavia - -'${arrayZ[@]}'- - '
echo ' - - Array sparso - -'${sparsoZ[@]}'- - '
echo ' - [0]==ref nulla, [2]==ref nulla, [4]==contenuto nullo - '
echo ' - [1]=abcabc [3]=ABCABC [5]=123123 - '
echo ' - conteggio dei non-nulli: '${#sparsoZ[@]}' elementi'

echo
echo '- - Rimozione di elementi iniziali - -'
echo '- - La verifica del Modello-Globale deve includere il primo carattere.- -'
echo "- - Il Modello-Globale può essere una stringa letterale \
o il risultato di una funzione. - -"
echo


# Funzione che restituisce un semplice, letterale, Modello-Globale
_abc() {
    echo -n 'abc'
}

echo '- Occorrenza più breve -'
echo ${stringaZ#123}                # Inalterata (nessuna corrispondenza).
echo ${stringaZ#$(_abc)}            # ABC123ABCabc
echo ${arrayZ[@]#abc}               # Applicato ad ogni elemento.

# Verrà corretto da Chet Ramey in una prossima versione di Bash.
# echo ${sparsoZ[@]#abc}            # La versione-2.05b scarica i registri.

# Il -sarebbe bello- Primo-Indice-Di
# echo ${#sparsoZ[@]#*}             # Notazione NON valida per Bash.

echo
echo '- Occorrenza più lunga -'
echo ${stringaZ##1*3}               # Inalterata (nessuna corrispondenza)
echo ${stringaZ##a*C}               # abc
echo ${arrayZ[@]##a*c}              # ABCABC 123123 ABCABC

# Verrà corretto da Chet Ramey in una prossima versione di Bash.
# echo ${sparsoZ[@]##a*c}           # La versione-2.05b scarica i registri.

echo
echo '- - Rimozione di elementi finali. - -'
echo "- - Il Modello-Globale deve includere l'ultimo carattere. - -"
echo "- - Il Modello-Globale può essere una stringa letterale \
o il risultato di una funzione. - -"
echo
echo '- Corrispondenza più breve -'
echo ${stringaZ%1*3}                # Inalterata (nessuna corrispondenza).
echo ${stringaZ%$(_abc)}            # abcABC123ABC
echo ${arrayZ[@]%abc}               # Applicato ad ogni elemento.

# Verrà corretto da Chet Ramey in una prossima versione di Bash.
# echo ${sparsoZ[@]%abc}            # La versione-2.05b scarica i registri.

# Lo -sarebbe bello- Ultimo-Indice-Di
# echo ${#sparsoZ[@]%*}             # Notazione NON valida per Bash.

echo
echo '- Corrispondenza più lunga -'
echo ${stringaZ%%1*3}               # Inalterata (nessuna corrispondenza).
echo ${stringaZ%%b*c}               # a
echo ${arrayZ[@]%%b*c}              # a ABCABC 123123 ABCABC a

# Verrà corretto da Chet Ramey in una prossima versione di Bash.
# echo ${sparsoZ[@]%%b*c}           # La versione-2.05b scarica i registri.

echo
echo '- - Sostituzione di elementi - -'
echo '- - Elementi in una qualsiasi posizione nella stringa. - -'
echo '- - La prima specifica è il Modello-Globale - -'
echo "- - Il Modello-Globale può essere una stringa letterale \
o il risultato di una funzione. - -"
echo "- - La seconda specifica può essere una stringa letterale \
o il risultato di una funzione. - -"
echo '- - La seconda specifica può essere omessa. Leggetelo'
echo '    come: Sostituisci-Con-Niente (Cancella) - -'
echo



# Funzione che restituisce un semplice, letterale, Modello-Globale
_123() {
    echo -n '123'
}

echo '- Sostituzione della prima occorrenza -'
echo ${stringaZ/$(_123)/999}        # Sostituito (123 era parte della stringa).
echo ${stringaZ/ABC/xyz}            # xyzABC123ABCabc
echo ${arrayZ[@]/ABC/xyz}           # Applicato a ciascun elemento.
echo ${sparsoZ[@]/ABC/xyz}          # Comportamento atteso.

echo
echo '- Cancellazione della prima occorrenza -'
echo ${stringaZ/$(_123)/}
echo ${stringaZ/ABC/}
echo ${arrayZ[@]/ABC/}
echo ${sparsoZ[@]/ABC/}

#  Non occorre che il sostituto sia una stringa letterale,
#+ dal momento che è permesso il risultato di una funzione.
#  Questo vale per qualsiasi forma di sostituzione.
echo
echo '- Sostituzione della prima occorrenza con Risultato-Di -'
echo ${stringaZ/$(_123)/$(_semplice)} # Funziona nel modo atteso.
echo ${arrayZ[@]/ca/$(_semplice)}     # Applicato a ciascun elemento.
echo ${sparsoZ[@]/ca/$(_semplice)}    # Funziona nel modo atteso.

echo
echo '- Sostituzione di tutte le occorrenze -'
echo ${stringaZ//[b2]/X}            # X sostituisce le b e i 2
echo ${stringaZ//abc/xyz}           # xyzABC123ABCxyz
echo ${arrayZ[@]//abc/xyz}          # Applicato a ciascun elemento.
echo ${sparsoZ[@]//abc/xyz}         # Funziona nel modo atteso.

echo
echo '- Cancellazione di tutte le occorrenze -'
echo ${stringaZ//[b2]/}
echo ${stringaZ//abc/}
echo ${arrayZ[@]//abc/}
echo ${sparsoZ[@]//abc/}

echo
echo '- - Sostituzione di elemento iniziale - -'
echo '- - La verifica deve includere il primo carattere. - -'
echo

echo '- Sostituzione di occorrenze iniziali -'
echo ${stringaZ/#[b2]/X}            #  Inalterata (nessuna delle due è 
                                    #+ un'occorrenza iniziale).
echo ${stringaZ/#$(_abc)/XYZ}       #  XYZABC123ABCabc
echo ${arrayZ[@]/#abc/XYZ}          #  Applicato a ciascun elemento.
echo ${sparsoZ[@]/#abc/XYZ}         #  Funziona nel modo atteso.

echo
echo '- Cancellazione di occorrenze iniziali -'
echo ${stringaZ/#[b2]/}
echo ${stringaZ/#$(_abc)/}
echo ${arrayZ[@]/#abc/}
echo ${sparsoZ[@]/#abc/}

echo
echo '- - Sostituzione di elemento finale - -'
echo "- - La verifica deve includere l'ultimo carattere. - -"
echo

echo '- Sostituzione di occorrenze finali -'
echo ${stringaZ/%[b2]/X}            #  Inalterata (nessuna delle due è
                                    #+ un'occorrenza finale).
echo ${stringaZ/%$(_abc)/XYZ}       #  abcABC123ABCXYZ
echo ${arrayZ[@]/%abc/XYZ}          #  Applicato a ciascun elemento.
echo ${sparsoZ[@]/%abc/XYZ}         #  Funziona nel modo atteso.

echo
echo '- Cancellazione di occorrenze finali -'
echo ${stringaZ/%[b2]/}
echo ${stringaZ/%$(_abc)/}
echo ${arrayZ[@]/%abc/}
echo ${sparsoZ[@]/%abc/}

echo
echo '- - Casi particolari di Modello-Globale nullo - -'
echo

echo '- Tutte le occorrenza iniziali -'
# il modello di sottostringa nullo significa 'iniziale'
echo ${stringaZ/#/NUOVO}            # NUOVOabcABC123ABCabc
echo ${arrayZ[@]/#/NUOVO}           # Applicato a ciascun elemento.
echo ${sparsoZ[@]/#/NUOVO}          # Applicato anche ai contenuti nulli.
                                    # Questo sembra ragionevole.

echo
echo '- Tutte le occorrenze finali -'
# il modello di sottostringa nullo significa 'finale'
echo ${stringaZ/%/NUOVO}            # abcABC123ABCabcNUOVO
echo ${arrayZ[@]/%/NUOVO}           # Applicato a ciascun elemento.
echo ${sparsoZ[@]/%/NUOVO}          # Applicato anche ai contenuti nulli.
                                    # Questo sembra ragionevole.

echo
echo '- - Casi particolari di Modello-Globale Per-Ogni - -'
echo '- - - - È bello sognare - - - -'
echo

_GenFunz() {
    echo -n ${0}                    # Solo a scopo dimostrativo.
    # In realtà sarebbe un computo arbitrario.
}

# Tutte le occorrenze che verificano il modello QualsiasiCosa.
# Attualmente //*/ non verifica il contenuto nullo né la referenziazione nulla.
# /#/ e /%/ verificano il contenuto nullo ma non la referenziazione nulla.
echo ${sparsoZ[@]//*/$(_GenFunz)}


#  Una sintassi possibile potrebbe essere quella di rendere
#+ la notazione dei parametri usati conforme al seguente schema:
#   ${1} - L'elemento completo
#   ${2} - L'occorrenza iniziale, se esiste, della sottostringa verificata
#   ${3} - La sottostringa verificata
#   ${4} - L'occorrenza finale, se esiste, della sottostringa verificata
#
# echo ${sparsoZ[@]//*/$(_GenFunz ${3})}   # Inquesto caso uguale a ${1}.
# Verrà forse implementato in una futura versione di Bash.


exit 0

# [N.d.T.]
# * 
#  Il termine "namespace" (spazio del nome) indica un "contenitore"
#+ adatto a risolvere il problema dei conflitti tra identificatori uguali.
#+ Nel caso specifico significa che in Bash è possibile usare lo stesso
#+ nome sia per una variabile che per una funzione, senza che ciò provochi
#+ problemi nell'esecuzione del programma. Sebbene questo sia permesso è,
#+ però, caldamente sconsigliato dallo stesso M. Cooper a causa della
#+ confusione che una tale pratica può generare nella comprensione
#+ del funzionamento di uno script.

# **
#  La solita battuta che, stavolta, si è preferito non tradurre.
#  "Mark-One eyeball" è un'espressione militare che indica la
#+ "visione a occhio nudo" riferita alla navigazione (navale e aerea) e che
#+ rimane ancora il mezzo più sicuro rispetto alla navigazione strumentale.
  
# ***
#  Più che una funzione anonima sembrerebbe una subshell.