Operating System - Linux
1828366 Members
2830 Online
109976 Solutions
New Discussion

Re: if statement issues....

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

if statement issues....

The problem I am having here is that only the 1st option is executed, no matter if I pick yes or no. What am I doing wrong? How can I get this working right without resorting to a case statement?


-----------------------------------------------
echo "This is the max size your lvol can be:"
echo $MAXSIZE
echo
echo Do you want to max out the size of your lvol?
print -n "Enter 'y' for Yes or 'n' for No :"
read ANSWER3
if [ $ANSWER3="n" ] || [ $ANSWER3="N" ] || [ $ANSWER3="no" ] || [ $ANSWER3="No" ]
then
exit
else
continue
fi
if [ $ANSWER3="y" ] || [ $ANSWER3="Y" ] || [ $ANSWER3="yes" ] || [ $ANSWER3="Yes" ]
then
echo
echo "vxassist -g $DISKGROUP growto $LVOL $MAXSIZE"
vxassist -g $DISKGROUP growto $LVOL $MAXSIZE
exit
else
exit
fi

-----------------------------------------------
11 REPLIES 11
VK2COT
Honored Contributor
Solution

Re: if statement issues....

Hello,

I will try not to make any major changes in
your script, although your problem can be
rewritten in much better ways :)

A) There is no print statement in standard Shells.

b) Your "if" loops were wrong.

c) It is good to wrap $ANSWER3 in quotes so
if a user types nothing, you do not get an
error.

echo "This is the max size your lvol can be:"
echo $MAXSIZE
echo
echo Do you want to max out the size of your lvol?
#print -n "Enter 'y' for Yes or 'n' for No :"
echo -n "Enter 'y' for Yes or 'n' for No :"
read ANSWER3
if [ "$ANSWER3" = "n" -o "$ANSWER3" = "N" -o "$ANSWER3" = "no" -o $"ANSWER3" = "No" ]
then
exit
else
continue
fi
if [ "$ANSWER3" = "y" -o "$ANSWER3" = "Y" -o "$ANSWER3" = "yes" -o "$ANSWER3" = "Yes" ]
then
echo
echo "vxassist -g $DISKGROUP growto $LVOL $MAXSIZE"
vxassist -g $DISKGROUP growto $LVOL $MAXSIZE
exit
else
exit
fi

Cheers,

VK2COT
VK2COT - Dusan Baljevic
VK2COT
Honored Contributor

Re: if statement issues....

Ahh, my bad typing.

Replace the first "if" statement with:

if [ "$ANSWER3" = "n" -o "$ANSWER3" = "N" -o "$ANSWER3" = "no" -o "$ANSWER3" = "No" ]


VK2COT
VK2COT - Dusan Baljevic
Patrick Ware_1
Super Advisor

Re: if statement issues....

Perfect! Thanks for the direction!!
James R. Ferguson
Acclaimed Contributor

Re: if statement issues....

Hi Patrick:

You can eliminate having to test to lowercase and uppercase replies (or MiXeD caes!) by using 'typeset -l':

typeset -l ANSWER3

...now, whenever your perform:

read ANSWER3

...the variable will contain only lowercase characters.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: if statement issues....

You may want to consider using a case to be more structured:
case $ANSWER in
n|N|no|No|NO|NO!) exit ;;
y|Y|yes|Yes) ;; # fall thru
*) exit ;;
esac

>Dusan: A) There is no print statement in standard Shells.

Sure there is. :-) A real shell has it. In fact I've seen people that frown on using echo and want you to use print. But both are builtin in a real shell.

>b) Your "if" loops were wrong.

Yes. But if you still want to use ||:
if [[ "$ANSWER3" = "n" || "$ANSWER3" = "N" || "$ANSWER3" = "no" || "$ANSWER3" = "No" ]]

>c) It is good to wrap $ANSWER3 in quotes

I've seen pedantic cases where they go further to handle leading "-":
X"$ANSWER3" = X"n"
VK2COT
Honored Contributor

Re: if statement issues....

Hello,

OK, now that things work, let's make
improvements.

With all nice inputs from others, we can
make it a bit simpler and more efficient.

a) You do not really need two "if"
statements. The small case-statement will
handle it safely.

b) Since standard HP-UX Shells are not Posix shells, "print" statement is still better
to be left out and "echo" used instead.

c) You can narrow down your input
to the first character only and then check
if it is "n" or "y".

echo "This is the max size your lvol can be:"
echo $MAXSIZE
echo
echo Do you want to max out the size of your lvol?
echo -n "Enter 'y' for Yes or 'n' for No :"

read ANS
if [ "$ANS" ]
then
ANSWER3="`echo $ANS |cut -c1`"
typeset ANSWER3
else
ANSWER3=""
fi

case $ANSWER3 in
n*|N*) exit
;;
y*|Y*) echo
echo "vxassist -g $DISKGROUP growto $LVOL $MAXSIZE"
vxassist -g $DISKGROUP growto $LVOL $MAXSIZE
;;
*) exit ;;
esac

exit 0


As you can see, many choices :)

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Dennis Handly
Acclaimed Contributor

Re: if statement issues....

>Dusan: b) Since standard HP-UX Shells are not Posix shells, "print" statement is still better to be left out and "echo" used instead.

You are confused only on your first point. sh IS the Posix shell (a real shell).
http://h20000.www2.hp.com/bc/docs/support/SupportManual/c02267598/c02267598.pdf

OldSchool
Honored Contributor

Re: if statement issues....

Dennis...

the linux types are pushing "dash" as *the* standard shell, and substituting c-style 'print' instead of 'echo'....

claim it "eliminates incompatibilities" between implementations of "echo"..

the def. shell in ubuntu is now dash, and you'd be amazed how many previously working installation scripts are now broken, at least temporarily

then they hit real unix....and wonder where all the non-std gnu options went
James R. Ferguson
Acclaimed Contributor

Re: if statement issues....

Hi Patrick:

...and while we are discussing favorite shells, remember NEVER change root's default shell from '/sbin/sh' to anything else!

The '/sbin/sh' is a statically-linked POSIX shell that does not need any libraries found in '/usr'. Thus, in single-user mode it is capable of doing everything it needs.

For most users, the POSIX shell which uses dynamically linked libraries is preferred. This is '/usr/bin/sh'. This improves the overall memory footprint somewhat, since multiple instantiations can share some code.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: if statement issues....

>JRF: This is /usr/bin/sh. This improves the overall memory footprint somewhat, since multiple instantiations can share some code.

You'll find that if you get a bean counter to add up the numbers, it probably doesn't.
The size of a process is 1 X the text size. And N X the data size. You'll find that the N X D far out weighs the 1 X T vs 1 X T' savings for the shared version.

That said, you should still use /usr/bin/sh so you can use fancy smancy locales.
Mark Ellzey
Valued Contributor

Re: if statement issues....

Patrick,

Actually, do your really need to test for both a 'y' and an 'n'? Aren't you really interested in only one answer, the 'y' one?

I have a small function I use any time I need this functionality. Here it it:

ask_yn() {
echo "Enter [y] to continue: \c"
read ans
if [ -z "$ans" -o "$ans" != "y" ] ; then
echo "Aborting"
exit
fi
}

In other words, I really don't care if the answer is anything other than 'y', becuase I'm bailing out if it's not.


Yes, you can do all the checks to see if it's cap, whole word, etc. but I'm just illustrating a point.

Regards,
Mark