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.

Esempio 33-13. Visualizzare testo colorato<br />

#!/bin/bash<br />

# color-echo.sh: Visualizza messaggi colorati.<br />

# Mo<strong>di</strong>ficate lo script secondo le vostre necessità.<br />

# Più facile che co<strong>di</strong>ficare i colori.<br />

nero=’\E[30;47m’<br />

rosso=’\E[31;47m’<br />

verde=’\E[32;47m’<br />

giallo=’\E[33;47m’<br />

blu=’\E[34;47m’<br />

magenta=’\E[35;47m’<br />

cyan=’\E[36;47m’<br />

bianco=’\E[37;47m’<br />

Capitolo 33. Miscellanea<br />

alias Reset="tput sgr0" # Ripristina gli attributi <strong>di</strong> testo normali<br />

#+ senza pulire lo schermo.<br />

cecho () # Colora-echo.<br />

# Argomento $1 = messaggio<br />

# Argomento $2 = colore<br />

{<br />

local msg_default="Non è stato passato nessun messaggio."<br />

# Veramente, non ci sarebbe bisogno <strong>di</strong> una<br />

#+ variabile locale.<br />

messaggio=${1:-$msg_default} # Imposta al messaggio predefinito se non ne<br />

#+ viene fornito alcuno.<br />

colore=${2:-$nero} # Il colore preimpostato è il nero, se<br />

#+ non ne viene specificato un altro.<br />

}<br />

echo -e "$colore"<br />

echo "$messaggio"<br />

Reset # Ripristina i valori normali.<br />

return<br />

# Ora lo mettiamo alla prova.<br />

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

"Mi sento triste..." $blu<br />

cecho "Il magenta assomiglia molto al porpora." $magenta<br />

cecho "Sono verde dall’invi<strong>di</strong>a." $verde<br />

cecho "Ve<strong>di</strong> rosso?" $rosso<br />

cecho "Cyan, più familiarmente noto come acqua." $cyan<br />

cecho "Non è stato passato nessun colore (nero <strong>di</strong> default)."<br />

# Omesso l’argomento $colore.<br />

cecho "Il colore passato è \"nullo\" (nero <strong>di</strong> default)." ""<br />

562

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

Saved successfully!

Ooh no, something went wrong!