user2885133
user2885133

Reputation: 21

Linux script behaves differently on ksh in two servers

Below code block is from Linux script. It provides different output in different Linux servers with same OS.

SECURE="YES"
if [[ !((-n "$SECURE") && (( "$SECURE" == "YES") || ("$SECURE" == "NO"))) ]]
then        
  echo -e "Validation failed for varilable SECURE: $SECURE"
else
  echo "Validation passed"
fi

Server-1 output:

Validation failed for varilable SECURE: YES

Server-2 output:

Validation passed

Not sure why output is different. Any details about this will be helpful. Thanks.

Update: I added #!/bin/bash as first line in both servers and they gave same output as "Validation passed".

When I add #!/bin/ksh I get:

Server-1 output:

Validation failed for varilable SECURE: YES

Server-2 output:

syntax error: `!( ( -n "$SECURE" ) && ( ( "$SECURE" == "YES" ) || ( "$SECURE" == "NO" )))' missing expression operator

By default both servers are using ksh. Calling this command:

env | grep SHELL 

shows:

SHELL=/bin/ksh

But why output is different?

Update 2: I found that KSH version is different in two servers. One server has pdksh and another has ksh93. Can any one tell me the difference between the two versions please?

Upvotes: 2

Views: 268

Answers (1)

Wilfredo Pomier
Wilfredo Pomier

Reputation: 1121

I know it's an old question, but...

I think KSH-93 Frequently Asked Questions - Q17 has a brief answer.

Q17. What is pdksh and is it related to ksh or KornShell?

A17. pdksh is a public domain version of a UNIX shell that is unrelated to ksh. It supports most of the 1988 KornShell language features and some of the 1993 features. Some KornShell scripts will not run with pdksh.

Now, according to manual for ksh88, te OP's script should have worked. Maybe the parser was expecting spaces between the operators or even he may have found a bug.

Anyway, this should work in both versions (even with SECURE=""):

SECURE="YES"
if [[ "$SECURE" == "YES" || "$SECURE" == "NO" ]]
then
  echo "Validation passed"
else
  echo "Validation failed for varilable SECURE: '$SECURE'"
fi

Upvotes: 1

Related Questions