Operating System - HP-UX
1753954 Members
7614 Online
108811 Solutions
New Discussion

ksh: if statement negation and arithmetic evaluation

 
SOLVED
Go to solution
support_billa
Valued Contributor

ksh: if statement negation and arithmetic evaluation

Hello,

 

I use KSH  Version M-11/16/88f with HP-UX 11.31.

 

How can use in a "if statement" with a "function" the negation .

 

Only this works with ":"

 

if ( function "${parameter1}" )
then :
else
cmd1
cmd2
..
fi

 

 

i get an error when i use "!" to negate :

 

if ! ( function "${parameter1}" )
then

cmd1
fi

 


syntax error at line XXX : `(' unexpected

 

second question : when i want to check if the sum of two numbers is lower then another number ,

then i use this :

n1=10
n2=10

max_n=40

if [ $(( ${n1} + ${n2} )) -le ${max_n} ]
then
  echo ok
fi

 



i tested with input of this thread :

 

if statement negation and Arithmetic evaluation

 

2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: ksh: if statement negation and arithmetic evaluation

>How can use in a "if statement" with a "function" the negation .

 

You can't use ksh 88 with that "!".  You must use ksh 93 (dtksh) or the Posix shell.

 

>if ! ( function "${parameter1}" )

 

This works fine in the Posix shell, sh.

 

Also, you shouldn't be using those (...), that creates a subshell.

If you like some type of balanced punctuation, you can use:

if ! { function "${parameter1}"; } then

 

>when I want to check if the sum of two numbers is lower then another number,

 

Your code works, what was your question?

But it would look better if you switched to arithmetic expressions:

if (( n1 + n2 <= max_n )); then

 

>I tested with input of this thread :
if statement negation and Arithmetic evaluation

 

Your hyperlink isn't valid.  It is missing a ":" after "http".

You should be able to use Post Options > Edit Reply, to correct it.  (I fixed my copy.)

support_billa
Valued Contributor

Re: ksh: if statement negation and arithmetic evaluation

hello,

 

thx a lot and i corrected it

 

regards