PDA

View Full Version : Bash script: non capisco dove sbaglio


Cobra78
08-06-2010, 18:46
#check if master virtualmachine is off or unreachable
if [[ ${NOMASTER} || "${STATUS1}" == "off" ]]
then


NOMASTER vale 1, STATUS vale off, eppure mi entra nel then, perchè?

Non dovrebbe farlo cavoli!!!

sacarde
08-06-2010, 19:33
ma l'OR è indicato con "||" ?

Cobra78
08-06-2010, 19:35
ma l'OR è indicato con "||" ?

Si, esattamente!

sacarde
08-06-2010, 19:59
se

STATUS1 == "off"

va bene

killercode
08-06-2010, 20:15
mi sembra giusto che entri, il then viene eseguito se l'if è true; come di fatto è (sempre se nomaster vale sempre 1)

Cobra78
08-06-2010, 22:02
mi sembra giusto che entri, il then viene eseguito se l'if è true; come di fatto è (sempre se nomaster vale sempre 1)

Alt...in bash 0 è ture, in quanto codice d'uscita di comandi senza errori, mentre quello che è diverso da 0 è false in quanto codice d'uscita con errore :)

Ho controllato 10 volte sta cosa, perchè mi pareva strano, eppure...

killercode
08-06-2010, 23:27
visto che come si è capito non ho mai usato la bash, == è anche qui un operatore di ugualianza?

In questo caso, valendo off status dall'ugualianza restituisce un true e entra nel then

Gimli[2BV!2B]
08-06-2010, 23:58
Come dice killercode ti consiglierei di testare esplicitamente il valore (esempio che ho sotto mano):if [[ ${$?} -ne 0 ]]; then
echo "fallito"
else
echo "riuscito"
fi

Senz'altro l'"exit(0)" di un programma riuscito viene interpretato come true con questa sintassi:if ping -c1 www.google.it 2>&1 > /dev/null ; then
echo "true: $?"
fi

Il test tra doppie quadre che stai usando testa la stringa: ritorna true per qualsiasi stringa che contenga almeno un carattere.gimli@kwankey ~$ if [[ ${NON_ESISTO} ]] ; then echo "true" ; fi
gimli@kwankey ~$ ESISTO=""
gimli@kwankey ~$ if [[ ${ESISTO} ]] ; then echo "true" ; fi
gimli@kwankey ~$ ESISTO="0"
gimli@kwankey ~$ if [[ ${ESISTO} ]] ; then echo "true" ; fi
true
gimli@kwankey ~$ ESISTO="PIPPO"
gimli@kwankey ~$ if [[ ${ESISTO} ]] ; then echo "true" ; fi
true

Cobra78
09-06-2010, 06:20
Solo che 0 l'ho inserito come valore numerico, non come stringa, e ho testato anche con gli operatori di confronto......mmmh....
Oggi faccio qualche altro test.

Cobra78
09-06-2010, 09:55
Alla fine ho risolto usando -eq, è che mi pareva di averci già provato e non avesse funzionato....BOH!