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.

strings<br />

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

Il comando strings viene usato per cercare le stringhe visualizzabili in un file dati o binario. Elenca<br />

le sequenze <strong>di</strong> caratteri trovate nel file <strong>di</strong> riferimento. E’ utile per un esame rapido e sommario <strong>di</strong> un<br />

file core <strong>di</strong> scarico della memoria o per dare un’occhiata ad un file <strong>di</strong> immagine sconosciuto<br />

(strings file-immagine | more potrebbe restituire qualcosa come JFIF che in<strong>di</strong>ca un<br />

file grafico jpeg). In uno script, si può controllare l’output <strong>di</strong> strings con grep o sed. Ve<strong>di</strong> Esempio<br />

10-7 e Esempio 10-9.<br />

Esempio 12-31. Un comando strings “migliorato”<br />

#!/bin/bash<br />

# wstrings.sh: "word-strings" (comando "strings" migliorato)<br />

#<br />

# Questo script filtra l’output <strong>di</strong> "strings" confrontandolo<br />

#+ con un file <strong>di</strong>zionario.<br />

# In questo modo viene eliminato efficacemente il superfluo,<br />

#+ restituendo solamente le parole riconosciute.<br />

# ====================================================================<br />

# Verifica standard del/degli argomento/i dello script<br />

ARG=1<br />

E_ERR_ARG=65<br />

E_NOFILE=66<br />

if [ $# -ne $ARG ]<br />

then<br />

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

exit $E_ERR_ARG<br />

fi<br />

if [ ! -f "$1" ] # Verifica l’esistenza del file.<br />

then<br />

echo "Il file \"$1\" non esiste."<br />

exit $E_NOFILE<br />

fi<br />

# ====================================================================<br />

LUNMINSTR=3 # Lunghezza minima della stringa.<br />

DIZIONARIO=/usr/share/<strong>di</strong>ct/linux.words # File <strong>di</strong>zionario.<br />

# Può essere specificato un file<br />

#+ <strong>di</strong>zionario <strong>di</strong>verso purché<br />

#+ <strong>di</strong> una parola per riga.<br />

elenco=‘strings "$nome_file" | tr A-Z a-z | tr ’[:space:]’ Z | \<br />

tr -cs ’[:alpha:]’ Z | tr -s ’\173-\377’ Z | tr Z ’ ’‘<br />

# Mo<strong>di</strong>fica l’output del comando ’strings’ me<strong>di</strong>ante <strong>di</strong>versi passaggi a ’tr’.<br />

# "tr A-Z a-z" trasforma le lettere maiuscole in minuscole.<br />

# "tr ’[:space:]’ Z" trasforma gli spazi in Z.<br />

# "tr -cs ’[:alpha:]’ Z" trasforma i caratteri non alfabetici in Z,<br />

283

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

Saved successfully!

Ooh no, something went wrong!