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?
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
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...
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, ...?
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.
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.