-2

I want to check function arguments, and check whether $2 and $3 are numeric Is the following line the way to use the AND statement? Am I using = the correct way to check for numeric digits?

if [[ "$2" = +([[:digit:]]) ]] && [[ "$3" = +([[:digit:]]) ]]; then
Vera
  • 1,173
  • 4
  • 17
  • Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process. – waltinator Nov 17 '21 at 05:32
  • Also, what you really want to match is: <1 or more digits>. What you have matches a string with a digit in it. Test cases: `"a1a", "1a", "129"` – waltinator Nov 17 '21 at 05:36
  • Does this answer your question? [Checking if an input number is an integer](https://unix.stackexchange.com/questions/151654/checking-if-an-input-number-is-an-integer) – Kusalananda Nov 17 '21 at 07:48
  • @waltinator It is not so. `"a1a"` returns false, whilst `"123"` returns true. – Vera Nov 17 '21 at 14:54
  • I do get confused on whether to use `=` or `==`. Is there any general rule? – Vera Nov 17 '21 at 15:18

1 Answers1

1

I suggest creating a function, perhaps called "IsInt", which returns true if the parameters passed is numeric. You would then say

if IsInt "$2" && IsInt "$3" ; then
...
fi

You then have the simpler task of creating "IsInt", or looking at an existing answer

icarus
  • 17,420
  • 1
  • 37
  • 54