Results 1 to 4 of 4

Thread: how to use loop

  1. #1
    Join Date
    Sep 2013
    Beans
    18

    how to use loop

    Hi Everyone


    Could anyone help me on how to use loop to repeat the task for 2 times,

    the code as follows


    Code:
    shirt=15
    black=13.50
    
    echo "how many shirt you want"
    read num
    echo
    echo "Please enter a choice"
    echo "1 ---> normal shirt"
    echo "2 ---> black shirt"
    read choice
    echo
    
    if [ $choice = "1" ]; then
        price=`expr $num1 \* $shirt`
        echo "$num1 * $shirt = $price"
    
    elif [ $choice = "2" ]; then
        answer=$(bc <<< "scale=2;$num*$black")
        echo "$num * $black = $answer"
    else
        echo "Invalid option"
    fi
    The output code


    Code:
    how many shirt you want
    2
    
    Please enter a choice
    1 ---> normal shirt
    2 ---> black shirt
    1
    
    2 * 15 = 30
    ictadmin@ubuntu:~/Desktop$
    Please anyone can tell me where to put loop inside my script, where it can repeat the task for 2 times or more.

    Thanks

    Taati

  2. #2
    Join Date
    Mar 2006
    Location
    Williams Lake
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: how to use loop

    Moved to programming talk.

  3. #3
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,697
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: how to use loop

    Hi Taati_Barao.

    This should point you in the right direction:
    Code:
    for i in {1..2}
    do
        echo $i;
    done
    The output would be:
    Code:
    1
    2
    Regards.

  4. #4
    Join Date
    Sep 2013
    Beans
    18

    Re: how to use loop

    Hi Papibe

    Thanks for that hint

    Taati

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •