Topic 10

Date: 3/22/2017
Linux shell scripting tutorial
Linux System Administration


Looping with while and until statements

  • Script scr2.sh
    #!/bin/bash
    N=1
    while [ "$N" -le "10" ]
    do
            echo "Number $N"
            N=$[N+1]
    done
    


  • Script scr3.sh
    #!/bin/bash
    N=1 
    until [ "$N" -gt "10" ]
    do 
    echo "Number $N"; N=$[N+1] 
    done
    


    Note, common mistakes in shell scripting are usually due to incorrect syntax. For example, there should be no spaces before and after operator "="
    N=1          # correct
    N =1         # error
    N= 1         # error
    N=$[N+1]     # correct
    N =$[N+1]    # error
    N= $[N+1]    # error 
    



  • Take me to the Course Website