I am trying to understand the difference between coding for if and while statements in bash.
In the script below, if I use IF I get the expected result using == OR -eq etc. However with the while loop each while seems to evaluate as true for all tests. While loops 1 & 3 should fail, correct?
A != B,
A == B,
A == C
I have tried different combinations of [ "" "" ] or [[ "" "" ]] etc.
What is the correct syntax for while
thx Art
#!/bin/sh
set -x
myVarA="abc"
myVarB="abc"
myVarC="def"
while [[ ${myVarA} != ${myVarB} ]]
echo "A does not equal B"
i=$(($i+1))
do sleep 1s
break
done
while [[ ${myVarA} == ${myVarB} ]]
echo "A does equal B"
i=$(($i+1))
do sleep 1s
break
done
while [[ ${myVarA} == ${myVarC} ]]
echo "A does equal C"
i=$(($i+1))
do sleep 1s
break
done
renders the following,
+ myVarA=abc
+ myVarB=abc
+ myVarC=def
+ [[ abc != abc ]]
+ echo 'A does not equal B'
A does not equal B
+ i=1
+ sleep 1s
+ break
+ [[ abc == abc ]]
+ echo 'A does equal B'
A does equal B
+ i=2
+ sleep 1s
+ break
+ [[ abc == def ]]
+ echo 'A does equal C'
A does equal C
+ i=3
+ sleep 1s
+ break
dopart of yourwhileloops are a few lines down the block. Possibly you should move them up to the beginning, i.e.while [[ condition ]]; do