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 10. Cicli ed alternative<br />

for parola in $( strings "$2" | grep "$1" )<br />

# Il comando "strings" elenca le stringhe nei file binari.<br />

# L’output viene collegato (pipe) a "grep" che verifica la stringa cercata.<br />

do<br />

echo $parola<br />

done<br />

# Come ha sottolineato S.C., le righe 23 - 29 potrebbero essere<br />

#+ sostituite con la più semplice<br />

# strings "$2" | grep "$1" | tr -s "$IFS" ’[\n*]’<br />

# Provate qualcosa come "./bin-grep.sh mem /bin/ls" per esercitarvi<br />

#+ con questo script.<br />

exit 0<br />

Sempre sullo stesso tema.<br />

Esempio 10-8. Elencare tutti gli utenti del sistema<br />

#!/bin/bash<br />

# userlist.sh<br />

FILE_PASSWORD=/etc/passwd<br />

n=1 # Numero utente<br />

for nome in $(awk ’BEGIN{FS=":"}{print $1}’ < "$FILE_PASSWORD" )<br />

# Separatore <strong>di</strong> campo = :^^^^^^<br />

# Visualizza il primo campo ^^^^^^^^<br />

# Ottiene l’input dal file delle password ^^^^^^^^^^^^^^^^^<br />

do<br />

echo "UTENTE nr.$n = $nome"<br />

let "n += 1"<br />

done<br />

# UTENTE nr.1 = root<br />

# UTENTE nr.2 = bin<br />

# UTENTE nr.3 = daemon<br />

# ...<br />

# UTENTE nr.30 = bozo<br />

exit 0<br />

# Esercizio:<br />

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

# Com’è che un utente or<strong>di</strong>nario (o uno script eseguito dallo stesso)<br />

#+ riesce a leggere /etc/passwd?<br />

# Non si tratta <strong>di</strong> una falla per la sicurezza? Perché o perché no?<br />

164

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

Saved successfully!

Ooh no, something went wrong!