Stuck in a while-loop in GNU/SU (seismic unix)

1

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

Angel

Posted 2013-09-10T14:52:41.433

Reputation: 48

Answers

4

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.

nerdwaller

Posted 2013-09-10T14:52:41.433

Reputation: 13 366

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 – 2013-09-10T16:00:31.347

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 – 2013-09-10T16:02:18.467

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 – 2013-09-10T16:18:53.583

See the second part of my answer as to why that probably is. Did you update the addition in bash? – nerdwaller – 2013-09-10T16:21:46.407

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 – 2013-09-10T17:24:58.823

No, square brackets are used for comparisons, you need perens for addition. – nerdwaller – 2013-09-10T17:26:14.063

oh! Ok. Thanks. Unfortunately it doesn't work yet, this loop its getting really annoying... thanks for the comprehension. – Angel – 2013-09-10T17:46:22.540

Hi! I've just found the error (face palm moment haha)... I should have put a whitespace frame for the "=" in the while condition. // It was not necessary to quote false either true. //
I also tried the command "let" for the addition and it worked too. Thank you. :)
– Angel – 2013-09-11T14:58:51.503