Shell Loop statement

Source: Internet
Author: User
Tags pear

One, for-done (not very common)

1. Grammar

For variable in name table do    ... done

2. Script Sample Code

#!/bin/shfor num in 1 2 3 4 5do    echo "number is $num" done

3. Execute script

[Email protected]]# sh hello.sh

4. Implementation results

The number is 1 the number is 2 the number is 3 the number is 4 the number is 5

Second, select (Not very common)

1. Grammar

Select variable in list do    ... done

2. Script Sample Code

#!/bin/shselect fruit in "apple" "Pear" "peach"    do Breakdoneecho "you chose $fruit"

3. Implementation results

1) Apple 2) Pear 3) Peach #? 1   //This place requires you to input your choice is Apple

Third, case

1. Grammar

Case variable in    string 1)    command list 1;;    String 2)    command List 2;;    *)    Command list 3esac

2. Script Sample Code

#!/bin/shread opcase $op    in a)    echo "you selected a";;    b)    echo "you selected B";;    *)    echo "Error" Esac

Four, while

1. Grammar

While [condition] //Note space do    ... done

2. Script Sample Code

#!/bin/shnum=1while [$num-le]do   num= ' expr $num + 1 ' doneecho $num

Shell Loop statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.