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.

# Ricerca gli script sh e <strong>Bash</strong> in una data <strong>di</strong>rectory:<br />

DIRECTORY=/usr/local/bin<br />

PAROLACHIAVE=Bourne<br />

# Script <strong>di</strong> shell Bourne e Bourne-Again<br />

file $DIRECTORY/* | fgrep $PAROLACHIAVE<br />

# Risultato:<br />

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

# /usr/local/bin/burn-cd: Bourne-Again shell script text executable<br />

# /usr/local/bin/burnit: Bourne-Again shell script text executable<br />

# /usr/local/bin/cassette.sh: Bourne shell script text executable<br />

# /usr/local/bin/copy-cd: Bourne-Again shell script text executable<br />

# . . .<br />

Esempio 12-29. Togliere i commenti da sorgenti C<br />

#!/bin/bash<br />

# strip-comment.sh: Toglie i commenti (/* COMMENTO */) in un programma C.<br />

E_NOARG=0<br />

E_ERR_ARG=66<br />

E_TIPO_FILE_ERRATO=67<br />

if [ $# -eq "$E_NOARG" ]<br />

then<br />

echo "Utilizzo: ‘basename $0‘ file-C" >&2 # Messaggio d’errore allo stderr.<br />

exit $E_ERR_ARG<br />

fi<br />

# Verifica il corretto tipo <strong>di</strong> file.<br />

tipo=‘file $1 | awk ’{ print $2, $3, $4, $5 }’‘<br />

# "file $1" restituisce nome e tipo <strong>di</strong> file . . .<br />

# quin<strong>di</strong> awk rimuove il primo campo, il nome . . .<br />

# Dopo <strong>di</strong> che il risultato è posto nella variabile "tipo".<br />

tipo_corretto="ASCII C program text"<br />

if [ "$tipo" != "$tipo_corretto" ]<br />

then<br />

echo<br />

echo "Questo script funziona solo su file sorgenti C."<br />

echo<br />

exit $E_TIPO_FILE_ERRATO<br />

fi<br />

279

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

Saved successfully!

Ooh no, something went wrong!