11

Hi I have this sentence and I want to know what does it means please.

if [[ -z "$1" ]]; then   #  --> this is if the value of the parameter $1 is zero
    PASO=1
elif [[ "$1" -gt 1 ]] ; then  # but i don't know what this flags mean? .."-gt"
    LOG "[$(date +%T)] Parametros incorrectos"
    exit 255
else
    PASO=$1
fi

What does -gt mean?

Jonas Stein
  • 4,078
  • 4
  • 36
  • 55
Sergio
  • 119

3 Answers3

14

-gt means "greater than". It is used to compare integers for the inequality that is usually written > in other languages (in some shells, with the test utility or inside [ ... ], > compares two strings for lexicographical ordering, so it has a very different meaning from -gt).

-gt is documented in the manual for test or [, or in your shell's manual if these are built-in utilities, as

n1 -gt n2

True if the integer n1 is algebraically greater than the integer n2; otherwise, false.

(the above is taken from the POSIX standard text about the test utility)

Fortran also uses this abbreviation in its .GT. relational operator for numbers.

The other relevant operators for comparing integers in the shell with test or in [ ... ] are -ge ("greater-than or equal"), -lt ("less-than"), -le ("less-than or equal"), -eq ("equal") and -ne ("not equal").

Interestingly, all of these are the same in Fortran (.GT., .GE., .LT., .LE., .EQ. and .NE.).

Kusalananda
  • 333,661
13
$ help test
test: test [expr]
    Evaluate conditional expression.
...
      arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                     -lt, -le, -gt, or -ge.

    Arithmetic binary operators return true if ARG1 is equal, not-equal,
    less-than, less-than-or-equal, greater-than, or greater-than-or-equal
    than ARG2.
glenn jackman
  • 85,964
  • On a UNIX system help testresults in the message: ERROR: Key 'test' not found (he1) ;-) – schily May 29 '18 at 16:24
  • 1
    @schily There are so many UNIX systems, and then so many more non-UNIX systems that most "UNIX" systems probably aren't. help is a bash built-in command and as such has not much to do with UNIX (as opposed to man which is usually an executable and was probably about the third command conceived during the development of UNIX, after sh and cc). If you try man test you'll get a description of a command which you probably never execute but which is helpful anyway because the various shell built-ins faithfully emulate its original behavior. – Peter - Reinstate Monica May 29 '18 at 16:46
  • @schily, what shell are you using on "UNIX"? – glenn jackman May 29 '18 at 16:47
  • 1
    help is a command that is part of SCCS. Shells that introduced a builtin named help with different behavior show that their author did not try to inform himself about existing practice before creating that builtin. – schily May 29 '18 at 16:49
  • 2
    @schily On the contrary: Any proprietary software offering executables named help (or test, or exit, or any other of a number of common English words) is immediately self-disqualified because they likely didn't think much about a number of other critical decisions either. – Peter - Reinstate Monica May 29 '18 at 17:00
  • Do help test, bash has is built in an an optimization only. The non-built in one should be available on all Unixes (except busybox only). – ctrl-alt-delor May 29 '18 at 17:50
  • Well, the help builtin in bash is proprietary, the external helpcommand is UNIX standard software since 1977. So bash disqualifies itself. – schily May 29 '18 at 18:33
  • OK, help is problematic. man test and/or https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions – glenn jackman May 29 '18 at 18:51
  • @schily I admit I was cocky. For example I didn't know that sccs and Unix were so close. I wrote a question regarding sccs help. – Peter - Reinstate Monica May 30 '18 at 07:18
3

You can start with help test, which will display the help of the POSIX subset of the syntax supported by the [[ operator.

A comprehensive documentation is in the CONDITIONAL EXPRESSIONS section of man bash.

Specifically:

Other operators:
  ...
  arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                 -lt, -le, -gt, or -ge.

Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.
bobah
  • 245
  • 1
  • 6
  • 1
    Yes I know that... what i need to know is what does it means " -gt " :-D – Sergio May 29 '18 at 13:59
  • 1
    Read that section of the man page, it's documented right in there. – glenn jackman May 29 '18 at 14:01
  • I'd say "display the help of the POSIX subset of the syntax supported by the [ operator" because [[...]] is actually part of the bash grammar, while /bin/[ is a POSIX command. – Peter - Reinstate Monica May 29 '18 at 16:55
  • @PeterA.Schneider - /bin/[ is a command line tool, [ is a Bash built-in, and [[ is a Bash operator. With my statement I wanted to emphasize that the [[ is a Bash extension of the POSIX [. – bobah May 29 '18 at 21:18