Operating System - HP-UX
1833589 Members
4178 Online
110061 Solutions
New Discussion

trouble with "||" operand

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

trouble with "||" operand

Hi all,

I cant get my if statement to work and I believe it may be to do with the || in the script:

print "Please enter a hdisk or vpath you would like to check and potentially remove: \c"

while true
do

read ANS

if [[ $ANS != hdisk[0-9]* || $ANS != vpath[0-9]* ]] ; then

print "incorrect selection please enter a valid hdisk or vpath: \c"

else

break

fi
done

what is wrong here?

is it to do with [[ ]] - should I be using [
many thanks

Chris.
hello
15 REPLIES 15
OFC_EDM
Respected Contributor

Re: trouble with "||" operand

if [[ $ANS != hdisk[0-9]* || $ANS != vpath[0-9]* ]]

wouldn't it be

if [[ $ANS != hdisk[0-9]* ] || [ $ANS != vpath[0-9]* ]]
The Devil is in the detail.
lawrenzo_1
Super Advisor

Re: trouble with "||" operand

no that is not correct ...

Please enter a hdisk or vpath you would like to check and potentially remove: ./check_disk.ksh[5]: 0403-057 Syntax error at line 12 : `]' is not expected.
hello
piyush mathiya
Trusted Contributor

Re: trouble with "||" operand

No man you have to use -a (And) or -o (OR) operator instad of "||"

Regards,
Piyush Mathiya
Dennis Handly
Acclaimed Contributor
Solution

Re: trouble with "||" operand

You have the wrong operator. You need:
if [[ $ANS != hdisk[0-9]* && $ANS != vpath[0-9]* ]]; then

Yours will always be true.
You may want to reverse your condition so it is more obvious:
if [[ $ANS = hdisk[0-9]* || $ANS = vpath[0-9]* ]] ; then
break
fi

Note your pattern matching will match hdisk, followed by 0 .. 9, followed by anything.

>Piyush: you have to use -a (And) or -o (OR) operator instead of "||"

No, [[ ]] supports && and ||. [] only supports -a and -o.
Oviwan
Honored Contributor

Re: trouble with "||" operand

hey

try it with quotes:

if [[ "$ANS" != "hdisk[0-9]*" || "$ANS" != "vpath[0-9]*" ]] then

regards
Dennis Handly
Acclaimed Contributor

Re: trouble with "||" operand

>is it to do with [[ ]] - should I be using [ ] for testing a pattern?

Only [[ ]] does pattern matching, [ ] does exact match.

>Oviwan: try it with quotes:
if [[ "$ANS" != "hdisk[0-9]*" || "$ANS" != "vpath[0-9]*" ]]

No, that will break pattern matching.
piyush mathiya
Trusted Contributor

Re: trouble with "||" operand

Hi dear,
As I already post a solution, you can use the following command.

if [[ $ANS != hdisk[0-9]* -a $ANS != vpath[0-9]* ]] ; then


Regards,
Piyush Mathiya
OFC_EDM
Respected Contributor

Re: trouble with "||" operand

Which shell are you using?
The Devil is in the detail.
Aashique
Honored Contributor

Re: trouble with "||" operand

Hi,
your pattern hdisk[0-9]* will not work in if condition.

print "Please enter a hdisk or vpath you would like to check and potentially remove: \c"

while true
do

read ANS
a=`echo $ANS|awk '/hdisk[0-9]/ {print $0}'`
b=`echo $ANS|awk '/vpath[0-9]/ {print $0}'`

if [ $a ] || [ $b ] ;
then

print "incorrect selection please enter a valid hdisk or vpath: \c"

else

break

fi
done


thanks & regards

aashique
Dennis Handly
Acclaimed Contributor

Re: trouble with "||" operand

>O'Kevin: Which shell are you using?

Chris is obviously using a real shell and not the scummy C shell.

>Aashique: your pattern hdisk[0-9]* will not work in if condition.

Why not? Pattern matching will work in [[ ]].
Aashique
Honored Contributor

Re: trouble with "||" operand

Hi,
I check this way and its running.
$ more test
ANS=vpath02
a=`echo $ANS|awk '/hdisk[0-9]/ {print $0}'`
b=`echo $ANS|awk '/vpath[0-9]/ {print $0}'`

if [ $a ] || [ $b ]
then
echo " YES"
else
echo "NO"
fi
$ ./test
YES
lawrenzo_1
Super Advisor

Re: trouble with "||" operand

just back from lunch

Ok thanks all,

I'll give take a look at these suggestions and let you know ...
hello
lawrenzo_1
Super Advisor

Re: trouble with "||" operand

thanks all for the help and suggestions - Dennis gave the best solution for the korn shell!

#!/bin/ksh

print "Please enter a hdisk or vpath you would like to check and potentially remove: \c"

while true
do

read ANS

if [[ $ANS = hdisk[0-9]* || $ANS = vpath[0-9]* ]] ; then

break
print "incorrect selection please enter a valid hdisk or vpath: \c"

else

print "incorrect selection please enter a valid hdisk or vpath: \c"

fi
done

echo $ANS
hello
Yogeeraj_1
Honored Contributor

Re: trouble with "||" operand

hi chris,

have a look the following url:
http://www.injunea.demon.co.uk/pages/page205.htm

should look something like:
if [ $ANS != hdisk[0-9]* ] || [ $ANS != vpath[0-9]* ]
then
..

(unfortunately, i do not have any machine at hand to validate this right now!)

The [[ ]] are not properly stated.

hope this helps!

kind regards
yogeeraj

No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Dennis Handly
Acclaimed Contributor

Re: trouble with "||" operand

>Yogeeraj: have a look the following URL:
http://www.injunea.demon.co.uk/pages/page205.htm

This appears to be Borne shell document, not ksh or posix.
http://docs.hp.com/en/B2355-60130/sh-posix.1.html
http://docs.hp.com/en/B2355-60130/ksh.1.html