Regarding 'for' loop in KornShell

后端 未结 4 1244
南旧
南旧 2020-12-20 03:56

Is there a way to implement the following using \'for\' in KornShell (ksh)? Here is the C equivalent:

for(i=1;i<20;i++)
{
    printf(\"%d\",i);
}
<         


        
4条回答
  •  忘掉有多难
    2020-12-20 04:16

    ksh93 supports the C-like (( ...;...; ...)):

    for ((i=1;i<20;i+=1)); do
        printf "%d " $i
    done && print
    

    This will produce:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    Heck, even the old syntax (using '{' ... '}' instead of 'do ... done' will work):

    for((i=1;i<20;i+=1))
    {
       printf "%d " $i
    } && print
    

    In older shells, you can still get the same effect with

    i=1 && while ((i<20)); do
        printf "%d " $i
        ((i+=1))
    done && print
    

提交回复
热议问题