Operating System - HP-UX
1820554 Members
2709 Online
109626 Solutions
New Discussion юеВ

Re: no negation in if-clause constructs of ksh?

 
Ralph Grothe
Honored Contributor

no negation in if-clause constructs of ksh?

Hello ksh scriptors,

I guess this one should be an easy 10 pointer.
I usually prefer Perl or Bourne shell as scripting languages of 1st choice.
Thus I'm not too familiar with the Korn shell's obscurities and idiosyncracies.
I only accidentally stumbled over this (to me) odd behaviour while I made an additional entry to a user's .profile who happens to have ksh as login shell.

The entry I initially made was something like this, knowing this should work in sh:

if ! echo $PATH|grep -q sbin; then
PATH=${PATH}:/usr/sbin
fi

Amazingly, ksh didn't seem to be happy with the negatory bang.
Of course, as with grep this could easily be overcome by removing the bang and inserting grep's -v flag in the if-clause.
But I was quite puzzled to learn from HP's ksh manpage that my intended kind of negation isn't reflected for ksh.
OK, there isn't a real need for ksh since HP's sh is a Posix Bourne shell which adopted most of the additional features of ksh (e.g. arrays, extended variable substitutions etc.), and when I'm in need of advanced data structures (i.e. LoLs) I would always go for Perl.
But this arouse my curiosity, and I wonder if ksh isn't just using a different syntax I might have missed.

Regards
Ralph
Madness, thy name is system administration
5 REPLIES 5
Vladislav Demidov
Honored Contributor

Re: no negation in if-clause constructs of ksh?

The "!" symbol is unary negation operator of the "test" command. "if" command does not recognise it. If you you want to use "!" operator then you have to construct test equation.
Rainer_1
Honored Contributor

Re: no negation in if-clause constructs of ksh?

try this
echo $PATH|grep -q sbin
if [ $? != 0 ] ; then
PATH=${PATH}:/usr/sbin
fi
CHRIS_ANORUO
Honored Contributor

Re: no negation in if-clause constructs of ksh?

The symbol ! is a parameter and can be assigned values and attributes by using the typeset special command. The symbol ! is only a negation in a test command ( if [ $1 != hp ]).
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Tommy Palo
Trusted Contributor

Re: no negation in if-clause constructs of ksh?

This one-liner is an alternative construct.
echo $PATH|grep -q sbin && PATH=${PATH}:/usr/sbin


Keep it simple
Ralph Grothe
Honored Contributor

Re: no negation in if-clause constructs of ksh?

Thank you all for your comments and suggestions.
What you suggested is all familiar to me.
Of course, do I know the logical concatantion of commands (viz. &&, ||).
Actually in Perl this is sort of standard fare, especially with || to warn, die, croak etc.
Also the test of the last statement's exit code through $? is familiar to me, but seems a little clumsy instead of a single test.
From what I conclude from HP's ksh manpage it really seems to be the case that in ksh you only may use ! is in a test construct (i.e. test command or its alias []).
Why haven't they (i.e. ksh's developers) adopted the valid ! use as in the Bourne shell.
E.g. excerpt from sh-posix manpage:

...
A pipeline is a sequence of one or more commands separated by a bar (|) and optionally preceded by an exclamation mark (!)
...
If ! does not precede the pipeline, the exit status of the pipeline is the exit status of the last command in the pipeline.
Otherwise, the exit status of the pipeline is the logical negation of the exit status of the last command in the pipeline.
...

I cannot find such a statement in the ksh manpage :-(
Madness, thy name is system administration