Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

.../bash
cdpmin=1800
cdpmax=2100
dcdp=100
cdp=$cdpmin

while [ $cdp -le $cdpmax ]
do
echo $cdp
ok=false

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

case $response in  
    n*)   
       ok=false ;;  
    *)   
       ok=true ;;  
esac   

done

cdp=$[$cdp+$dcdp]

done

Thanks a lot

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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:

ok="false"
while [[ "$ok" = "false" ]]
do
    ...
done

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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