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.

# Grazie anche a Florian Wisser per la "citazione iniziale".<br />

Capitolo 7. Verifiche<br />

# (*) L’intestazione <strong>di</strong> commento originaria recita "Testing null strings<br />

#+ and unquoted strings, but not strings and sealing wax, not to<br />

# mention cabbages and kings ..." attribuita a Florian Wisser. La<br />

# seconda riga non è stata tradotta in quanto, la sua traduzione<br />

# letterale, non avrebbe avuto alcun senso nel contesto attuale<br />

# (N.d.T.).<br />

Esempio 7-7. zmore<br />

#!/bin/bash<br />

#zmore<br />

# Visualizza i file gzip con ’more’<br />

NOARG=65<br />

NONTROVATO=66<br />

NONGZIP=67<br />

if [ $# -eq 0 ] # stesso risultato <strong>di</strong>: if [ -z "$1" ]<br />

# $1 può esserci, ma essere vuota: zmore "" arg2 arg3<br />

then<br />

echo "Utilizzo: ‘basename $0‘ nomefile" >&2<br />

# Messaggio d’errore allo stderr.<br />

exit $NOARG<br />

# Restituisce 65 come exit status dello script (co<strong>di</strong>ce d’errore).<br />

fi<br />

nomefile=$1<br />

if [ ! -f "$nomefile" ] # Il quoting <strong>di</strong> $nomefile mantiene gli spazi.<br />

then<br />

echo "File $nomefile non trovato!" >&2<br />

# Messaggio d’errore allo stderr.<br />

exit $NONTROVATO<br />

fi<br />

if [ ${nomefile##*.} != "gz" ]<br />

# Uso delle parentesi graffe nella sostituzione <strong>di</strong> variabile.<br />

then<br />

echo "Il file $1 non è un file gzip!"<br />

exit $NONGZIP<br />

fi<br />

zcat $1 | more<br />

# Usa il filtro ’more.’<br />

# Lo si può sostituire con ’less’, se si desidera.<br />

exit $? # Lo script restituisce l’exit status della pipe.<br />

80

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

Saved successfully!

Ooh no, something went wrong!