11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

exit 0<br />

+<br />

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

James R. Van Zandt ha fornito questo script che fa uso delle named pipe ed è, parole sue, “vero esercizio<br />

<strong>di</strong> quoting ed escaping”.<br />

Esempio A-15. fifo: eseguire backup giornalieri utilizzando le named pipe<br />

#!/bin/bash<br />

# ==> Script <strong>di</strong> James R. Van Zandt, e qui usato con il suo permesso.<br />

# ==> Commenti aggiunti dall’aurore del libro.<br />

+<br />

QUI=‘uname -n‘ # ==> hostname<br />

LA=bilbo<br />

echo "inizio <strong>di</strong> un backup remoto in $LA alle ‘date +%r‘"<br />

# ==> ‘date +%r‘ restituisce l’ora nel formato 12-ore, cioè "08:08:34 PM".<br />

# si accerta che /pipe sia veramente una pipe e non un file regolare<br />

rm -rf /pipe<br />

mkfifo /pipe # ==> Crea una "named pipe", <strong>di</strong> nome "/pipe".<br />

# ==> ’su xyz’ esegue i coman<strong>di</strong> come utente "xyz".<br />

# ==> ’ssh’ invoca secure shell (client per il login remoto).<br />

su xyz -c "ssh $LA \"cat >/home/xyz/backup/${QUI}-daily.tar.gz\" < /pipe"&<br />

cd /<br />

tar -czf - bin boot dev etc home info lib man root sbin share usr var >/pipe<br />

# ==> Impiaga la named pipe, /pipe, per la comunicazione tra processi:<br />

# ==>+ ’tar/gzip’ scrive in /pipe e ’ssh’ legge da /pipe.<br />

# ==> Il risultato finale è il backup delle principali <strong>di</strong>rectory,<br />

# ==>+ da / in giù.<br />

# ==> Quali sono i vantaggi <strong>di</strong> una "named pipe" in una situazione come<br />

# ==>+ questa, contrapposti a quelli <strong>di</strong> una "pipe anonima", con |?<br />

# ==> In questo caso funzionerebbe anche una pipe anonima?<br />

exit 0<br />

Questo script, inviato da Stéphane Chazelas, <strong>di</strong>mostra che si possono generare numeri primi senza<br />

ricorrere agli array.<br />

636

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!