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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Capitolo 12. Filtri, programmi e coman<strong>di</strong> esterni<br />

for a in ‘seq $INIZIO $INTERVALLO $FINE‘<br />

# Fornendo tre argomenti "seq" inizia il conteggio partendo dal primo, usa il<br />

#+ secondo come passo (incremento) e continua fino a raggiungere il terzo.<br />

do<br />

echo -n "$a "<br />

done # 45 50 55 60 65 70 75 80<br />

echo; echo<br />

exit 0<br />

Un esempio più semplice:<br />

# Crea 10 file<br />

#+ <strong>di</strong> nome file.1, file.2 . . . file.10.<br />

CONTO=10<br />

PREFISSO=file<br />

for nomefile in ‘seq $CONTO‘<br />

do<br />

touch $PREFISSO.$nomefile<br />

# O effettuare altre operazioni,<br />

#+ con rm, grep, ecc.<br />

done<br />

Esempio 12-50. Conta lettere<br />

#!/bin/bash<br />

# letter-count.sh: Conta le occorrenze <strong>di</strong> lettere in un file <strong>di</strong> testo.<br />

# Scritto da Stefano Palmeri.<br />

# Usato in <strong>Guida</strong> ABS con il consenso dell’autore.<br />

# Leggermente mo<strong>di</strong>ficato dall’autore del libro.<br />

MINARG=2 # Lo script richiede almento due argomenti.<br />

E_ERR_ARG=65<br />

FILE=$1<br />

let LETTERE=$#-1 # Quantità <strong>di</strong> lettere specificate<br />

# (come argomenti da riga <strong>di</strong> comando).<br />

# (Sottrae 1 dal numero degli argomenti.)<br />

visualizza_help(){<br />

echo<br />

echo Utilizzo: ‘basename $0‘ file lettere<br />

echo Nota: gli argomenti per ‘basename $0‘ sono \"case sensitive\".<br />

echo Esempio: ‘basename $0‘ foobar.txt G n U L i N U x.<br />

echo<br />

}<br />

# Verifica del numero degli argomenti.<br />

328

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

Saved successfully!

Ooh no, something went wrong!