2

This is what I'm trying to do:

#!/bin/bash

check_for_int()
{
    if [ "$1" -eq "$1" ] 2>/dev/null
    then
        return 0
    else
        return 1
    fi
}

if [[check_for_int($1) == 1]]
    echo "FATAL: argument must be an integer!"
    exit 1
fi

# do stuff

However, no matter how I put it, shellcheck is complaining:

if [[ check_for_int($1) == 1 ]]; then
^-- SC1009: The mentioned parser error was in this if expression.
   ^-- SC1073: Couldn't parse this test expression.
               ^-- SC1036: '(' is invalid here. Did you forget to escape it?
               ^-- SC1072: Expected "]". Fix any mentioned problems and try again.

I have no idea why this isn't working...

codeforester
  • 39,467
  • 16
  • 112
  • 140
dan9er
  • 369
  • 2
  • 17
  • Closely related: [How do I test if a variable is a number in bash?](https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash) – Charles Duffy Oct 21 '17 at 16:03

2 Answers2

4

The way you pass argument to check_for_int is not valid in bash. Modify it to:

if ! check_for_int "$1"; then
    echo "FATAL: argument must be an integer!"
    exit 1
fi
P.P
  • 117,907
  • 20
  • 175
  • 238
  • 3
    `check_for_int "$1"`, rather. Otherwise you'd get a result that `"1 abc"` is an integer, because only the `1` would end up in `$1`, and the `abc` would be in `$2`. – Charles Duffy Oct 21 '17 at 16:02
0

Like asked from title this is a working solution (Updated with better method used from @usr of pass arguments to a function in the if statement)

#!/bin/bash

check_for_int () {
    if [[ $1 =~ ^\+?[0-9]+$ ]] || [[ $1 =~ ^\-?[0-9]+$ ]]; then
        return 0
    else
        return 1
    fi
}

if ! check_for_int "$1"; then
    echo "FATAL: argument must be an integer!"
    exit 1
else
    echo "${0}: ${1} is a integer!"
    exit 0
fi

NOTE:

This include positive and negative sign

"$var" -eq "$var" have some negative side: How do I test if a variable is a number in Bash?

OUTPUT

darby@Debian:~/Scrivania$ bash ex -+33
FATAL: argument must be an integer!
darby@Debian:~/Scrivania$ bash ex +33
ex: +33 is a integer!
darby@Debian:~/Scrivania$ bash ex -33
ex: -33 is a integer!
darby@Debian:~/Scrivania$ bash ex 3e33
FATAL: argument must be an integer!
darby@Debian:~/Scrivania$ bash ex '333 8'
FATAL: argument must be an integer!
darby@Debian:~/Scrivania$
Darby_Crash
  • 446
  • 3
  • 6
  • You'd get identical behavior from `check_for_int() { (( $1 == $1 )) 2>/dev/null; }` -- there's no point to the `if` and the explicit `return`s. – Charles Duffy Oct 21 '17 at 16:02
  • 1
    Also, you're comparing the _output_ of the function to `1`, but you don't actually print anything. – Benjamin W. Oct 21 '17 at 16:03
  • (and using the `function` keyword breaks POSIX compatibility for no good reason -- sure, `(( ))` does too, but that at least adds value in terms of enabling more idiomatic code). – Charles Duffy Oct 21 '17 at 16:05
  • Moreover, `(( $1 == $1 ))` isn't a safe way to check for integer-ness -- note that `abc` will evaluate to `0` if evaluated in an arithmetic context, unless there's a variable by that name defined with a different value. Thus, `set -- abc; unset abc; (( $1 == $1 ))` returns true with no error. – Charles Duffy Oct 21 '17 at 16:06
  • @Charles Duffy This solution is what the question ask us. POSIX regex is the better way to do it. – Darby_Crash Oct 21 '17 at 17:11
  • @Darby_Crash, if how to detect whether a value is a number is *truly* what the OP asked (rather than how to use a function with parameters in an if statement), then we should close the question as duplicate of https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash. That said, yes, (as you'll note from my answer on that question), I *do* agree that an ERE is an appropriate tool for the job. – Charles Duffy Oct 21 '17 at 17:18
  • @Charles Duffy, I like your comments, you have always a right point of view. ;) – Darby_Crash Oct 21 '17 at 17:30