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=$cdpminwhile [ $cdp -le $cdpmax ]
do
echo $cdp
ok=falsewhile [ ok=false ]
do
.
.
.
echo "It's correct? (y/n)" | tr -d "\012" >/dev/tty
read responsecase $response in n*) ok=false ;; *) ok=true ;; esac
done
cdp=$[$cdp+$dcdp]
done
Thanks a lot
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 thenwhile [ "$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.467So 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