Shell 10 Process Control

Source: Internet
Author: User
Tags case statement

If is judged if
#shell#!/bin/sha=5if [[ $a > 3 ]];then    echo "$a>3"fi#写成一行if [[ $a < 6 ]];then echo "$a>3";fi

5>3
5>3

If Else
#shell#!/bin/sha=5if [[ $a > 8 ]];then    echo "$a>8"else    echo "$a<8"fi

5<8

If elif Else
#shell#!/bin/sha=5if [[ $a > 5 ]];then    echo "$a>5"elif [ $a -eq 5 ];then    echo "$a=5"else    echo "$a<5"fi

5=5

For loop
#shell#!/bin/shfor i in `seq 1 5`;do    echo $idone

1
2
3
4
5

While statement
#shella=1while [ $a -lt 5 ];do    echo "$a"    let "a++"        #或者 a=`expr $a + 1`done

1
2
3
4

Infinite Loop while using: substitution condition
#shell#!/bin/shwhile : ;do    echo "hello"done
While condition is always true
#shell#!/bin/shwhile true;do    echo "hello"done
Using a For loop
#!/bin/shfor ((;;));do    echo "hello"done
Until cycle
#shell#!/bin/sha=0until [ $a -gt 10 ]; do    echo $a    let "a++"done

0
1
2
3
4
5
6
7
8
9
10

Case is a multi-select statement, each case statement matches a value with a pattern
#shell#!/bin/shread -p "请输入的你的名次:" numcase $num in    1) echo "武林盟主"    ;;    2) echo "五岳盟主"    ;;    3) echo "华山掌门"    ;;    *) echo "回家玩去"esac
Jump out of the loop break jump out of all loops
#shellwhile :;do    read -p "请输入1到5之间的数字:" num    case $num in        1|2|3|4|5) echo "你输入的数字为$num"        ;;        *) echo "你输入的数字不在1和5之间"            break        ;;    esacdone

Stop the loop after entering 6
Please enter a number from 1 to 5:5
The number you entered is 5.
Please enter a number from 1 to 5:4
The number you entered is 4.
Please enter a number from 1 to 5:6
The number you entered is not between 1 and 5.

Continue jump out of this cycle
#shellwhile :;do    read -p "请输入1到5之间的数字:" num    case $num in        1|2|3|4|5) echo "你输入的数字为$num"        ;;        *) echo "你输入的数字不在1和5之间"            continue            echo "游戏结束"        ;;    esacdone

Continue the next loop after entering 7
Please enter a number from 1 to 5:7
The number you entered is not between 1 and 5.
Please enter a number from 1 to 5:3
The number you entered is 3.
Please enter a number from 1 to 5:6
The number you entered is not between 1 and 5.

Esac case ends with EASC, each case branched with;; To break

Shell 10 Process Control

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.