-1

I would like to do the following in shell scripting:

if [ (is_number $arg1) -ne 0 ] || [ (is_number $arg2) -ne 0 ] ; then
    printf "Exit. Valid input must be positive integer.\n"
    return 1
fi

But it gave me syntax error. Do you have better solution? Thanks!

I want a solution without accessing $? or storing return value into variable, unless such answer doesn't exist.

I see you guys taking advantage of 0 and not 0. It make sense since 0 signify whether succeed or not.

Now, speaking it in a pure syntax way, I want to do the following:

if [ (is_number $arg1) -ne int ] || [ (is_number $arg2) -ne int] ; then
    printf "Exit. Valid input must be positive integer.\n"
    return 1
fi

Please provide a way, thanks.

Han XIAO
  • 1,148
  • 9
  • 20
  • 1
    `if ! is_number "$arg1" || ! is_number "$arg2"; then...`. See [Why is testing `$?` to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – Gordon Davisson Feb 19 '21 at 22:09
  • 1
    `[` is not part of `if` syntax; it's just another name for the command `test`. Don't use it unless you have a reason to use the `test` command. – Charles Duffy Feb 19 '21 at 22:19

1 Answers1

1

Do you have better solution?

Call the actual functions.

is_number() { [[ "$1" =~ ^[0-9]+$ ]]; }
if ! is_number "$arg1" || ! is_number "$arg2"; then
KamilCuk
  • 120,984
  • 8
  • 59
  • 111