I'm new to programming. I have this loop (it's part of a data processing script) and I have it all good until this section cause I can't figure out why it stay looping in the second 'while'

Take a look:


while [ $cdp -le $cdpmax ]
echo $cdp

while [ ok=false ]
echo "It's correct? (y/n)" | tr -d "\012" >/dev/tty
read response

case $response in  
       ok=false ;;  
       ok=true ;;  




Thanks a lot


You aren't comparing what you think you are, you are comparing the literal "ok" with the word "false". Instead, you need to compare the variable $ok with false.

With that adjustment, it would look like:

while [[ "$ok" = "false" ]]

I believe you will also run into an error with cdp=$[$cdp + $dcpd], in bash, addition usually looks like:

cdp=$(($cdp + $dcpd))

A tip: Since the condition for the loop you were stuck in was ok=false, it is clear that the issue is in the condition not changing as you expect. This is one of those easy to overlook issues though that is a face palm after the fact. Any time I run into these, I go for a walk or something to take my mind off it. More often than not, when I come back - the issue is obvious.

  • Thanks for the "$ok" correction! Nonetheless I had it in the original script so that's not the problem. :/ I just realized that the words "false" and "true" were bold, so i guessed they both were functions then I changed them for "1" and "0" but it didnt work either. P.S.: this syntax addition "cdp=$[$cdp + $dcpd]" does work. :) – Angel Sep 10 '13 at 16:00
  • I think technically, they should be quoted for all of those, so ok="false" and then while [ "$ok" = "false" ] ... n) ok="false" etc. Try that. Also, in new shells - the [[ condition ]] is supposedly more powerful due to globbing (I think). It's strange that doesn't work for you since using what you posted with those changes works fine for me.... – nerdwaller Sep 10 '13 at 16:02
  • So if you use what I posted '$cdp' reaches "2100"? // I've put 'echo=$cdp' cause I wanna check the loop was going well but it always print "1800" which is the first value... so I guess it's stuck in the second while cause never add the new value and keep asking me "It's correct (y/n)" – Angel Sep 10 '13 at 16:18
  • See the second part of my answer as to why that probably is. Did you update the addition in bash? – nerdwaller Sep 10 '13 at 16:21
  • No, I didn't update it. // with the second part of your answer (which was a little confusing to me) did you mean to use "[[--]]"? I've used it in both while-loops but nothing changed. :/ (Guess you've noticed I'm not a native english speaker) – Angel Sep 10 '13 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.