Risposte:
Nella maggior parte dei casi, [è una shell integrata ed è equivalente a test. Tuttavia, ad esempio test, esiste anche come eseguibile autonomo: è quello che /bin/[hai visto. Puoi provarlo con type -a [(su un sistema Arch Linux, in esecuzione bash):
$ type -a [
[ is a shell builtin
[ is /bin/[
Quindi, sul mio sistema, ne ho due [: la shell incorporata e l'eseguibile /bin. L'eseguibile è documentato in man test:
TEST(1) User Commands TEST(1)
NAME
test - check file types and compare values
SYNOPSIS
test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION
DESCRIPTION
Exit with the status determined by EXPRESSION.
[ ... ]
Come puoi vedere nell'estratto della pagina man sopra citata, teste [sono equivalenti. I comandi /bin/[e /bin/testsono specificati da POSIX ed è per questo che li troverai nonostante il fatto che molte shell li forniscano anche come builtin. La loro presenza assicura che costruisce come:
[ "$var" -gt 10 ] && echo yes
funzionerà anche se la shell che li esegue non ha un [builtin. Ad esempio, in tcsh:
> which [
/sbin/[
> set var = 11
> [ "$var" -gt 10 ] && echo yes
yes
bashè solo uno dei tanti programmi (shell) progettati per eseguire lavori simili. Bash è uno dei più popolari ma molti sistemi sono forniti con diverse shell predefinite. Alcuni di quelli più noti sono sh, bash, zsh, dash, ksh, tcsh, cshe fish. Puoi vedere quelli disponibili sul tuo sistema con cat /etc/shellse un elenco parziale qui .
shè, dashma pensavo che il sistema di salvataggio /bin/shnon usasse la casella di controllo. Sei sicuro?
Viene utilizzato per il test delle condizioni negli script di shell. Un altro nome di questo programma è test:
if [ 1 -lt 2 ]; then ...
Sembra grammatica della shell ma non lo è. Di solito [è un built-in di shell ma probabilmente come fallback esiste come comando esterno.
Vedere il blocco "ESPRESSIONI CONDIZIONATE" in man bash.
[è lo stesso comando di test. Su alcuni sistemi * nix, uno è solo un collegamento all'altro. Ad esempio, se si esegue:
strings /usr/bin/test
strings /usr/bin/[
vedrai lo stesso output.
La maggior parte delle sh-shell / posix-shell include builtin [e testcomandi. Lo stesso vale per echo. C'è sia un /bin/echocomando che un builtin nella maggior parte delle shell. Questo è il motivo per cui a volte ritieni che, ad esempio, echonon funzioni allo stesso modo su sistemi diversi.
testo [restituisce solo un codice di uscita di 0o 1. Se il test ha avuto esito positivo, il codice di uscita è 0.
# you can use [ command but last argument must be ]
# = inside joke for programmers
# or use test command. Args are same, but last arg can't be ] :)
# so you can't write
# [-f file.txt] because [-f is not command and last argument is not ]
# after [ have to be delimiter as after every commands
[ -f file.txt ] && echo "file exists" || echo "file does not exist"
test -f file.txt && echo "file exists" || echo "file does not exist"
[ 1 -gt 2 ] && echo yes || echo no
test 1 -gt 2 && echo yes || echo no
# use external command, not builtin
/usr/bin/[ 1 -gt 2 ] && echo yes || echo no
Puoi anche usare [con if:
if [ -f file.txt ] ; then
echo "file exists"
else
echo "file does not exist"
fi
# is the same as
if test -f file.txt ; then
echo "file exists"
else
echo "file does not exist"
fi
Ma puoi usarlo ifcon ogni comando, ifè per testare il codice di uscita. Per esempio:
cp x y 2>/dev/null && echo cp x y OK || echo cp x y not OK
Oppure, usando if:
if cp x y 2>/dev/null ; then
echo cp x y OK
else
echo cp x y not OK
fi
È possibile ottenere lo stesso risultato utilizzando solo il testcomando per testare il codice di uscita che viene salvato nella variabile stat:
cp x y 2>/dev/null
stat=$?
if test "$stat" = 0 ; then
echo cp x y OK
else
echo cp x y not OK
fi
Puoi anche usare [[ ]]e (( ))per i test, ma quelli non sono gli stessi di [e test, nonostante abbiano quasi la stessa sintassi:
Infine, per scoprire cos'è un comando, puoi usare:
type -a command
cmp /usr/bin/[ /usr/bin/testo forse hash sha256sum /usr/bin/[ /usr/bin/testma non strings. Sul mio sistema (openSUSE Tumbleweed) BTW non sono gli stessi (perché).
[builtin è solo una shell i cui autori hanno deciso di non aggiungerne una.tcshnon ha un[builtin per esempio.