IF OR with two conditions

I have this IF working fine, testing if a char is a digit:

      if [ $_CHAR -eq $_CHAR 2>/dev/null ]; then
         _VALUE=$_VALUE$_CHAR
      else
         _ISDIGIT="false"
      fi

Then I add a second condition to test if the char is either a digit or a *

      if [[ $_CHAR -eq $_CHAR 2>/dev/null || $_CHAR == "*" ]]; then
         _VALUE=$_VALUE$_CHAR
      else
         _ISDIGIT="false"
      fi

I get this:

./statusSIGTRAN_TSP5.sh: line 35: syntax error in conditional expression
./statusSIGTRAN_TSP5.sh: line 35: syntax error near `2>'
./statusSIGTRAN_TSP5.sh: line 35: `      if [[ $_CHAR -eq $_CHAR 2>/dev/null || $_CHAR == "*" ]]; then'
 

What's wrong?

You do not need the "2>/dev/null" part in a boolean (if) construct. It doesn't evaluate.
And. I do not know why your first line works. It should not.

---------- Post updated at 09:01 ---------- Previous update was at 08:45 ----------

FWIW:

#!/bin/ksh
isdigit()
{
		case "$1" in
		  ?(1|2|3|4|5|6|7|8|9|0)) ok=1;;
		  *) ok=0;;
		esac
		echo $ok 
} 

for i in 1 2 3 4 a d
do
  echo "isdigit $i returns $(isdigit $i)"
done

is a traditional way to test a given character for character type, in this case a digit.
Traditional meaning "use the case statement"

1 Like

try with "-o" option instead of "||".

So,

1 Like

I don't know what the dev/null does, got the example from the internet and it doesn't work without it:

 
if [ $_CHAR -eq $_CHAR ]; then
 
./test.sh: line 23: [: -: integer expression expected
34622991111
./test.sh: line 23: [: -: integer expression expected
34622991211
./test.sh: line 23: [: -: integer expression expected
34622999213
./test.sh: line 23: [: too many arguments

Do not put 2>/dev/null inside the brackets

1 Like

This is getting more and more awkward. -o option works from a syntax point of view, but the expression doesn't return true when it encounters a star.

Furthermore:

      if [$_CHAR == "*" ]; then
         _VALUE=$_VALUE$_CHAR
      else
         _ISDIGIT="false"
      fi

./test.sh: line 23: [3: command not found
./test.sh: line 23: [3: command not found
./test.sh: line 23: [3: command not found
./test.sh: line 23: [*: command not found

Really annoying crap

Put a space between [ and $

1 Like

Jesus

 
if [ $_CHAR == "*" ]; then
_VALUE=$_VALUE$_CHAR
else
_ISDIGIT="false"
fi
 
./test.sh: line 23: [: too many arguments
 

Use single =
put double quotes around $_CHAR

1 Like

Thanks, this works alone but when I combine it with the second condition it doesn't return true when it encounters a star:

#!/bin/sh
_CHAR="*"
      if [ $_CHAR -eq $_CHAR 2>/dev/null -o "$_CHAR" = "*" ]; then
        echo STAR
      fi
 
Proc_m2_s1$ ./test2.sh
Proc_m2_s1$

I do not know what you are are trying to accomplish, but try this:

if [ "$_CHAR" -eq "$_CHAR" ] 2>/dev/null || [ "$_CHAR" = "*" ]; then
1 Like

Yes, this worked, thanks a lot! Returns true for both digits and star