Pergunta

I'm just starting out in Bash scripting, and I can't seem to work out what these arguments mean (-ne and -n). Are they Bash arguments or self created arguments?

# Run as root
if [ "$UID" -ne "$ROOT_UID" ]
then
        echo "This must be run as root"
        exit $E_NOTROOT
fi

if [ -n "S1" ]
then
Foi útil?

Solução

The mnemonic for -ne is 'not equal'; it does an arithmetic comparison on the two values for inequality.

The mnemonic for -n is 'not empty'; it tests whether the argument ("$1" in this case) is an empty string. If $1 is defined and has a value other than the empty string, the test will be true.

See Bash conditional expressions for more details.

The test command, also known as [, supports the other numeric comparison operators too: -lt (less than), -le (less than or equal to), -gt (greater than), -ge (greater than or equal to), and -eq (equal). The -z operator tests for a zero length string — but note that the argument must be enclosed in double quotes, though ([ -z "$variable" ]) as otherwise there is no argument for -z to test.

There are many other test operators; this is not an exhaustive list.

Outras dicas

from man test :

-n STRING

     the length of STRING is nonzero

INTEGER1 -ne INTEGER2

     INTEGER1 is not equal to INTEGER2
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top