bash shell nested for loop

后端 未结 3 1146
轻奢々
轻奢々 2021-01-31 03:04

I want to write a nested for loop that has to work in the bash shell prompt. nested for loop in Single line command.

For example,

for i in a b; do echo         


        
3条回答
  •  再見小時候
    2021-01-31 04:00

    #!/bin/bash
    # loop*figures.bash
    
    for i in 1 2 3 4 5  # First loop.
    do
        for j in $(seq 1 $i)
        do
            echo  -n "*" 
        done
        echo 
    done
    echo
    # outputs
    # *
    # **
    # ***
    # ****
    # *****
    
    for i in 5 4 3 2 1 # First loop.
    do
        for j in $(seq -$i -1)
        do
            echo  -n "*" 
        done
        echo 
    done
    
    # outputs
    # *****
    # ****
    # ***
    # **
    # *
    
    for i in 1 2 3 4 5  # First loop.
    do
        for k in $(seq -5 -$i)
        do
            echo -n ' '
        done
        for j in $(seq 1 $i)
        do
            echo  -n "* " 
        done
        echo 
    done
    echo
    
    # outputs
    #     * 
    #    * * 
    #   * * * 
    #  * * * * 
    # * * * * * 
    
    for i in 1 2 3 4 5  # First loop.
    do
        for j in $(seq -5 -$i)
        do
            echo  -n "* " 
        done
        echo 
        for k in $(seq 1 $i)
        do
            echo -n ' '
        done
    done
    echo
    
    # outputs
    # * * * * * 
    #  * * * * 
    #   * * * 
    #    * * 
    #     *
    
    
    exit 0
    

提交回复
热议问题