9 Kasım 2020 Pazartesi tarihinde Dale R. Worley <wor...@alum.mit.edu> yazdı:
> <v...@larryv.me> writes: > >> On Nov 8, 2020, at 6:49 PM, Budi <budikus...@gmail.com> wrote: > >> > >> Need feature on while's readily flag > >> > >> $ i=;while ((i<5)) ;do let i++ ;echo $i ;done ;echo $? > >> 1 > >> 2 > >> 3 > >> 4 > >> 5 > >> 0 > >> > >> $ i=7;while ((i<5)) ;do let i++ ;echo $i ;done ;echo $? > >> 0 > >> > >> we need feature on while's ability to supply flag when it's up to the > >> end of loop block > > > > It's not clear what you're asking for. Do you want while to exit > > nonzero when it doesn't perform any loops? > > I suspect what Budi wants is more explicit control of the exit value of > the while block. Of course, as long as the block is executed at least > once, it is straightforward to control the exit value. Compare these > two examples: > > $ i=0;while ((i<5)) ;do let i++ ;echo $i; true ;done ;echo $? > > $ i=0;while ((i<5)) ;do let i++ ;echo $i; false ;done ;echo $? > > What cannot be controlled is the exit value if the block is not executed > i.e. if the initial evaluation of the condition is false. In that case, > the exit value is always zero. > > You're not saying this warrants a change to the while loop's behavior though, right? There is nothing wrong with using an extra variable to determine whether the loop body was ever reached. i=7 j=0 while ((i<5)); do let i++ echo $i j=1 done echo $j > Dale > > -- Oğuz