Logical expression in POSIX compliant Korn Shell

Hi,
i want to check if a variable var1 is not a or b or c
pseudo code:
If NOT (var1 = a or var1 = b or var1 = c)
then
...
fi

I want to use POSIX complaint Korn shell, and for string comparison
For the following code, logical.sh

#!/usr/bin/ksh
var="j"
echo "Var : $var"

if ! { [ "$var" = a ] || [ "$var" = b ] || [ "$var" = b ] ;}
then    
   echo "var not in the list"
else    
   echo "var in the list"
fi

var="b"
echo "Var : $var"

if ! { [ "$var" = a ] || [ "$var" = b ] || [ "$var" = b ] ;}
then    
   echo "var not in the list"
else    
   echo "var in the list"
fi

I gave execute permissions to the script, however it only seems to run good when run by 'sh ' and not by the script itself.
We run all our jobs without specifying 'sh ' ahead

$ chmod 777 logical.sh
$ logical.sh
Var : j
./logical.sh[3]: syntax error at line 6 : `}' unexpected
$ sh logical.sh
Var : j
var not in the list
Var : b
var in the list
$

Where am i going wrong?
Is it necessary to enclose the variable in double quotes but not the literals on the other side of comparison operator?

Thanks,
-srinivas yelamanchili

Invert the logic so you don't need that ! in front.

if [ "$var" != "a" ] && [ "$var" != "b" ] && [ "$var" != "c" ]
then
...
fi

Your shell may not be POSIX compliant. What ksh are you using? pdksh? On what system?

The box is HP-UX 11.11

echo $SHELL

Version M-11/16/88f

$

whereis ksh

ksh: /usr/bin/ksh /usr/dt/man/man1/ksh.1 /usr/dt/share/man/man1/ksh.1 /usr/share/man/man1.Z/ksh.1

$

 ksh

Version 11/16/88

What's the latest recommended version of Korn shell (and POSIX compliant) that I can use?

Thanks,
-srinivas yelamanchili

Kornshell 88 is not 100% POSIX compliant

Try to run it as sh:

#!/usr/bin/sh

# (I think it is in /usr/bin or was it #!/bin/posix/sh ??)
It will still use ksh, but it should emulate a POSIX shell...
(See man sh-posix)

---------- Post updated at 09:49 ---------- Previous update was at 09:27 ----------

I forgot, there is usually also a POSIX compliant ksh93 on HPUX:

/usr/dt/bin/dtksh
1 Like

Thanks Scrutinizer, the dtksh helped !!!

-srinivas

Good, out of interest, did you happen to try the posix shell? Did that work?

#!/bin/posix/sh
worked too, i just checked that

I am debating which one to pick, the dtksh or posix
We are migrating to Redhat Linux soon and i don't know the version of Korn it comes with. I want to code to be Korn and POSIX compliant and not break after migration

Thanks,
-srinivas

Thanks for reporting srinivas.. Redhat Linux comes with ksh93.

If you want pure POSIX compliance, use pure POSIX, not KSH. Pure POSIX shell code will work in SH and KSH, while things using KSH features(arrays, etc) might only work in KSH...

1 Like

On my login i default to:

$ echo $SHELL
/usr/bin/sh
$

Now what is this shell called? Can every unix/linux box can have their 'sh' be bash or korn or csh ... or is 'sh' is it's own shell and independant of bash, korn, ...?

Thanks,
-srinivas

Depends what sh they have... :shrug: The generic filename doesn't tell you much.

On some linux, sh is BASH, on others its DASH. Someone might even custom-configure their system to have it be one of the various kinds of KSH and there'd be nothing wrong with that. It can be any Bourne shell.

But they ought to be all compatible with posix SH even if they're not restricted to posix SH features. That's what POSIX is there for -- a coherent standard that ought to be obeyed by UNIX in general, even if other features are offered. So if you write your code for posix SH, it should be portable.

sh will never, ever, ever be csh though, becase sh is supposed to be a bourne shell -- and csh is not a bourne in any way. Even on systems which feature csh prevalently, like some BSD's, csh does not get shoehorned under the guise of sh. Only the very most trivial csh code bears any resemblance to bourne shell code.

From man sh:

 DESCRIPTION
    Remarks:
      The POSIX.2 standard requires that, on a POSIX-compliant system,
      executing the command sh activates the POSIX shell (located in file
      /usr/bin/sh on HP-UX systems), and executing the command man sh
      produces an on-line manual entry that displays the syntax of the POSIX
      shell command-line.