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.

Esempio finale <strong>di</strong> [lista] risultante dalla sostituzione <strong>di</strong> comando.<br />

Esempio 10-9. Verificare tutti i file binari <strong>di</strong> una <strong>di</strong>rectory in cerca degli autori<br />

Capitolo 10. Cicli ed alternative<br />

#!/bin/bash<br />

# findstring.sh:<br />

# Cerca una stringa particolare nei binari <strong>di</strong> una <strong>di</strong>rectory specificata.<br />

<strong>di</strong>rectory=/usr/bin/<br />

stringa="Free Software Foundation" # Vede quali file sono della FSF.<br />

for file in $( find $<strong>di</strong>rectory -type f -name ’*’ | sort )<br />

do<br />

strings -f $file | grep "$stringa" | sed -e "s%$<strong>di</strong>rectory%%"<br />

# Nell’espressione "sed", è necessario sostituire il normale<br />

#+ delimitatore "/" perché si dà il caso che "/" sia uno dei<br />

#+ caratteri che deve essere filtrato.<br />

done<br />

exit 0<br />

# Esercizio (facile):<br />

# ------------------<br />

# Mo<strong>di</strong>ficate lo script in modo tale che accetti come parametri da<br />

#+ riga <strong>di</strong> comando $<strong>di</strong>rectory e $stringa.<br />

L’output <strong>di</strong> un ciclo for può essere collegato con una pipe ad un comando o ad una serie <strong>di</strong> coman<strong>di</strong>.<br />

Esempio 10-10. Elencare i link simbolici presenti in una <strong>di</strong>rectory<br />

#!/bin/bash<br />

# symlinks.sh: Elenca i link simbolici presenti in una <strong>di</strong>rectory.<br />

<strong>di</strong>rectory=${1-‘pwd‘}<br />

# Imposta come predefinita la <strong>di</strong>rectory <strong>di</strong> lavoro corrente, nel caso non ne<br />

#+ venga specificata alcuna.<br />

# Corrisponde al seguente blocco <strong>di</strong> co<strong>di</strong>ce.<br />

# -------------------------------------------------------------------<br />

# ARG=1 # Si aspetta un argomento da riga <strong>di</strong> comando.<br />

#<br />

# if [ $# -ne "$ARG" ] # Se non c’è 1 argomento...<br />

# then<br />

# <strong>di</strong>rectory=‘pwd‘ # <strong>di</strong>rectory <strong>di</strong> lavoro corrente<br />

# else<br />

# <strong>di</strong>rectory=$1<br />

# fi<br />

# -------------------------------------------------------------------<br />

echo "Link simbolici nella <strong>di</strong>rectory \"$<strong>di</strong>rectory\""<br />

for file in "$( find $<strong>di</strong>rectory -type l )" # -type l = link simbolici<br />

165

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

Saved successfully!

Ooh no, something went wrong!