2013-01-24 13 views
32

Voglio scrivere uno script bash che controlla se v'è almeno un parametro e se ce n'è uno, se questo parametro è 0 o un 1. Questo è lo script:Bash confronto intero

#/bin/bash 
if (("$#" < 1)) && ((("$0" != 1)) || (("$0" -ne 0q))) ; then 
echo this script requires a 1 or 0 as first parameter. 
fi 
xinput set-prop 12 "Device Enabled" $0 

Questo dà i seguenti errori:

./setTouchpadEnabled: line 2: ((: ./setTouchpadEnabled != 1: syntax error: operand expected (error token is "./setTouchpadEnabled != 1") 
./setTouchpadEnabled: line 2: ((: ./setTouchpadEnabled -ne 0q: syntax error: operand expected (error token is "./setTouchpadEnabled -ne 0q") 

che cosa sto facendo di sbagliato?

+0

Sembra che si sta eseguendo lo script utilizzando 'sh./SetTouchpadEnabled' invece di usare bash. – jordanm

+0

@ jordanm ti riferisci alla mancanza di un botto nella linea dello shebang? – Kev

risposta

32

Questo script funziona!

#/bin/bash 
if [[ ("$#" < 1) || (!("$1" == 1) && !("$1" == 0)) ]] ; then 
    echo this script requires a 1 or 0 as first parameter. 
else 
    echo "first parameter is $1" 
    xinput set-prop 12 "Device Enabled" $0 
fi 

Ma questo funziona anche, e inoltre mantiene la logica del PO, poiché la domanda riguarda calcoli. Qui è solo arithmetic expressions:

#/bin/bash 
if (($#)) && (($1 == 0 || $1 == 1)); then 
    echo "first parameter is $1" 
    xinput set-prop 12 "Device Enabled" $0 
else 
    echo this script requires a 1 or 0 as first parameter. 
fi 

L'uscita è la stessa :

$ ./tmp.sh 
this script requires a 1 or 0 as first parameter. 

$ ./tmp.sh 0 
first parameter is 0 

$ ./tmp.sh 1 
first parameter is 1 

$ ./tmp.sh 2 
this script requires a 1 or 0 as first parameter. 

[1] secondo fallisce se il primo argomento è una stringa

+0

Grazie mille, l'errore è perché il comando è xinput e non input – Cheiron

+0

Un motivo particolare per cui non si attaccano le espressioni aritmetiche usate anche nella domanda? Cioè uso di '((' e '))'. – 0xC0000022L

+1

@ user828193: chiaramente si tratta di calcoli, quindi le espressioni aritmetiche sono * la * strada da fare. Ecco perché ho trovato irritante il fatto che tu abbia cambiato questo aspetto della domanda nella tua risposta. – 0xC0000022L

5

Il parametro zeroth di un comando shell è il comando stesso (o talvolta la shell stessa). Dovresti usare $1.

(("$#" < 1)) && ((("$1" != 1)) || (("$1" -ne 0q))) 

La logica booleana è anche un po 'confuso:

(("$#" < 1 && # If the number of arguments is less than one… 
    "$1" != 1 || "$1" -ne 0)) # …how can the first argument possibly be 1 or 0? 

Questo è probabilmente ciò che si vuole:

(("$#")) && (($1 == 1 || $1 == 0)) # If true, there is at least one argument and its value is 0 or 1 
+0

Questo è un miglioramento, grazie. Tuttavia mi dà ancora errori: ./setTouchpadEnabled: riga 2: ((:! = 1: errore di sintassi: operando previsto (token di errore è "! = 1") ./setTouchpadEnabled: riga 2: ((:! = 0: errore di sintassi: operando previsto (il token di errore è "! = 0") – Cheiron

+1

Le virgolette non sono strettamente necessarie all'interno di '(())', ma rendono più intelligente l'evidenziazione di StackOverflow – kojiro

+0

La rimozione delle virgolette dà lo stesso errore, sfortunatamente – Cheiron

10

Soluzione più semplice;

#/bin/bash 
if ((${1:-2} >= 2)); then 
    echo "First parameter must be 0 or 1" 
fi 
# rest of script... 

uscita

$ ./test 
First parameter must be 0 or 1 
$ ./test 0 
$ ./test 1 
$ ./test 4 
First parameter must be 0 or 1 
$ ./test 2 
First parameter must be 0 or 1 

Spiegazione

  • (()) - valuta l'espressione con numeri interi.
  • ${1:-2} - Utilizza l'espansione parametro per impostare un valore di 2 se non definito.
  • >= 2 - Vero se il numero intero è maggiore o uguale a due 2.
4

So che questo è stato risposto, ma qui è il mio solo perché penso che il caso sia uno strumento sottovalutato. (Forse perché la gente pensa che è lento, ma è almeno altrettanto velocemente come se, a volte più veloce.)

case "$1" in 
    0|1) xinput set-prop 12 "Device Enabled" $1 ;; 
     *) echo "This script requires a 1 or 0 as first parameter." ;; 
esac 
Problemi correlati