Bash script - Infinite loop caused by a logical OR operator
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash script - Infinite loop caused by a logical OR operator
Hello everyone. After a few hours trying to debug and searching LQ.org and Google and nothing I decided to post so someone could point me somewhere.
Let's get to the point, here's the code:
Code:
freespacecd() {
#it will help to know that "vazio" means empty and multisessao means wheter the disk is appendable ornot.
while [[ "$MULTISESSAO" != "yes" || "$VAZIO" = "no" ]]; do
VAZIO=$(cdrdao disk-info --device /dev/cdrom | grep "CD-R empty" | cut -c24-27)
if [ "$VAZIO" != "yes" ]; then
MULTISESSAO=$(cdrdao disk-info --device /dev/cdrom | grep "Appendable" | cut -c24-27)
if [ "$MULTISESSAO" != "yes" ]; then
echo "A mídia não está vazia e não é multisessão. Troquea-a para que o backup prossiga. Aguardando 15 segundos..." #just tell something to the person.
eject
sleep 15
checkcd #check if there's a cd in the drive
fi
fi
done
The issue is at the while loop which never ends. It seems so right to me. Surely there's something making it not to work.
First time the loop goes $MULTISESSAO is evaluated true and $VAZIO is evaluated true and second time and so it's the opposite: $MULTISESSAO is now true and $VAZIO is false.
I have no ideas anymore. I also tried without with single square brackets "[ ... ]" but it didn't work either.
Ok, sorry for the bad question. What I meant was: "is there anything wrong with the condition formation?". Its syntax is right to me and I think for you too since you said nothing about it... Thanks for the answer.
Last edited by skuzye; 02-02-2009 at 10:20 AM.
Reason: spell error correction
The conditional syntax looks fine to me (and bash should give an error message if it's not), but I think there is a problem with the cut statement you use:
Code:
| cut -c24-27
This is probably returning three letters, so you get "no " instead of "no".
Problem solved. I seems that while doesn't support multiple conditions. Here's the resulting code:
Code:
freespacecd() {
FREECDGO="1"
while [ "$FREECDGO" -ne "0" ]; do
VAZIO=$(cdrdao disk-info --device /dev/cdrom | grep "CD-R empty" | cut -c24-27)
sleep 5
if [ "$VAZIO" != "yes" ]; then
MULTISESSAO=$(cdrdao disk-info --device /dev/cdrom | grep "Appendable" | cut -c24-27)
if [ "$MULTISESSAO" != "yes" ]; then
echo "A mídia não está vazia e não é multisessão. Troquea-a para que o backup prossiga. Aguardando 15 segundos..."
eject
sleep 15
checkcd
fi
fi
if [ "$MULTISESSAO" = "yes" ] || [ "$VAZIO" = "yes" ]; then
FREECDGO="0"
fi
done
}
I[t] seems that while doesn't support multiple conditions. Here's the resulting code:
Yes it does. Note that in your modified code you only test against "yes", whereas in the original you were also testing against "no". I think this is evidence that my earlier point is correct.
Yes, && only runs the second statement if the first succeeds and || runs the second statement if the first fai
I usually use them like this:
[ "$exp1" -a "$exp2" ]
[[ "$exp1" ]] && [[ "$exp2" ]]
Yes it does. Note that in your modified code you only test against "yes", whereas in the original you were also testing against "no". I think this is evidence that my earlier point is correct.
Sorry I forgot to note that cut is only cutting "no" and not "no ".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.