Operating System - Linux
1830178 Members
9290 Online
109999 Solutions
New Discussion

Re: bash and sh difference in break this while do loop

 
jamesps
Regular Advisor

bash and sh difference in break this while do loop

This is an extract from /opt/VRTS/bin/vxrootmir

tfound=no
tmpdg=
mdm=
mda=

vxdisk -q list | \
while read da type dm grp state rest
do
if [ "$dm" = "$1" ]; then
tmpdg=$grp
mdm=$1
tfound=yes
break
elif [ "$da" = "$1" ]; then
if [ "$dm" != "-" ]; then
tmpdg=$grp
mdm=$dm
fi
tfound=yes
break
fi
done

if [ "$tfound" = "no" ]; then
abort "Disk $1 not found in configuration" vxvmshm:0
fi


Here's what I would like to explain: under bash (3.00.14) the tfound variable is set to "no" again after it is set to "yes" and breaks after the first if. Because of that the disk not found message is displayed and the script exits unsuccessfully.

This behavior does not happen under sh. It looks so simple but... why?

Thanks!
james
4 REPLIES 4
jamesps
Regular Advisor

Re: bash and sh difference in break this while do loop

I am using B.11.11, latest security patches.
James R. Ferguson
Acclaimed Contributor

Re: bash and sh difference in break this while do loop

Hi James:

I'd use 'set -x' and perhaps add 'set -v' and trace the execution and variable setting.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: bash and sh difference in break this while do loop

I suspect that your read is the problem.

Try this very simple test:

echo "One Two Three" | read A B C
echo "A = \"${A}\""
echo "B = \"${B}\""
echo "C = \"${C}\""

I think you will find different results in the POSIX shell and bash -- at least some implementations of bash. It's one of the reasons I'm not a huge fan of bash. My shell of choice under Linix is zsh.
If it ain't broke, I can fix that.
Kent Ostby
Honored Contributor

Re: bash and sh difference in break this while do loop

Perhaps it should be == instead of = in

if [ "$tfound" = "no" ]; then

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"