Operating System - Linux
1752786 Members
5860 Online
108789 Solutions
New Discussion юеВ

Re: If loop in the Korn Shell in HP Ia 64

 
SOLVED
Go to solution
CA1490051
Frequent Advisor

If loop in the Korn Shell in HP Ia 64

Hi all,

I am trying to write a small shell script in HP UX Ia64.

read Option

if [ $option == 1 ]; then
echo One
else
echo Two
fi

But, it says
== unknown test operator

But i think we should use == for korn and
if ( $Option eq 1 )
for csh and tcsh.

Can any one Please Correct me where i am going wrong.

thanks in advance
Vikram
9 REPLIES 9
Ivan Krastev
Honored Contributor
Solution

Re: If loop in the Korn Shell in HP Ia 64

For compare use '=', not '=='. See more - http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html#comparisons


regards,
ivan

Re: If loop in the Korn Shell in HP Ia 64

Vikram,

Try double square braces:

if [[ $option == 1 ]]; then
echo One
else
echo Two
fi


HTH

Duncan

I am an HPE Employee
Accept or Kudo
James R. Ferguson
Acclaimed Contributor

Re: If loop in the Korn Shell in HP Ia 64

Hi Vikram:

The manpages for 'test(1)' will guide you.

Remember, if you are using '/usr/bin/sh' as your script interpreter, then you are using the HP-UX POSIX shell --- a superset of the Korn88 provided in '/usr/bin/ksh' by HP.

Regards!

...JRF...
CA1490051
Frequent Advisor

Re: If loop in the Korn Shell in HP Ia 64

Hi All,

thank you very much for the reply.

Ivan's solution is working fine for me.
Also before closing the thread i request Ivan to send the similar links for Tcsh and csch shell scripting if they are existing .

I tried going back in the present link but it seems only ksh and awk scripting is present in the link.

thanks and regards
Vikram
Ivan Krastev
Honored Contributor

Re: If loop in the Korn Shell in HP Ia 64

Use google - there are too many similar how-to's.

regards,
ivan
Peter Nikitka
Honored Contributor

Re: If loop in the Korn Shell in HP Ia 64

Hi,

use the variable reference in double quotes - so there will be no syntax error, when it is completely unset:
if [ "$option" = 1 ]
then
...

Same construct for tcsh/csh:
if ("$option" == 1) then
echo One
else
echo NOT-One
endif

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Bill Hassell
Honored Contributor

Re: If loop in the Korn Shell in HP Ia 64

And for completeness, testing using = is for strings which may or may not contain non-numeric values. For pure numbers, use -eq as in:

if [ $option -eq 1 ]; then
echo One
else
echo Two
fi

I prefer to use -eq (and relatives like -ne -gt -le ...) for numbers even though some string comparisons will work. The problem with string compares is that they won't check invalid numbers and magnitude comparisons such as > or < can have unexpected results.

For simple actions, you can simplify the test to a one-liner like this:

[ $option -eq 1 ] && echo One || echo Two

The man page for ksh is helpful here.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: If loop in the Korn Shell in HP Ia 64

Hi (again):

If you have found a solution, please assign points and close this thread. Thanks!

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: If loop in the Korn Shell in HP Ia 64

OK - who are you and what have you done with the real Jim Ferguson????


Pete

Pete