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.

echo "Il confronto ha funzionato."<br />

else<br />

echo "Il confronto non ha funzionato."<br />

fi # Il confronto non ha funzionato.<br />

# Stessa cosa con a=" 273" e a="0273".<br />

Capitolo 31. Precauzioni<br />

# Allo stesso modo, si hanno problemi ad usare "-eq" con valori non interi.<br />

if [ "$a" -eq 273.0 ]<br />

then<br />

echo "a = $a"<br />

fi # Si interrompe con un messaggio d’errore.<br />

# test.sh: [: 273.0: integer expression expected<br />

Non usare in modo scorretto gli operatori per il confronto <strong>di</strong> stringhe.<br />

Esempio 31-1. I confronti numerici e quelli <strong>di</strong> stringhe non si equivalgono<br />

#!/bin/bash<br />

# bad-op.sh: Tentativo <strong>di</strong> usare il confronto <strong>di</strong> stringhe con gli interi.<br />

echo<br />

numero=1<br />

# Il "ciclo while" seguente contiene due errori:<br />

#+ uno vistoso, l’altro subdolo.<br />

while [ "$numero" < 5 ] # Errato! Dovrebbe essere: while [ "$numero" -lt 5 ]<br />

do<br />

echo -n "$numero "<br />

let "numero += 1"<br />

done<br />

# La sua esecuzione provoca il messaggio d’errore:<br />

#+ bad-op.sh: line 10: 5: No such file or <strong>di</strong>rectory<br />

# All’interno delle parentesi quadre singole si deve applicare il quoting a"

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

Saved successfully!

Ooh no, something went wrong!