Bubble sequencing of shell programming

Source: Internet
Author: User
Tags array length

This is a shell script practiced hand program that exercises the use of a for statement

#! /bin/bashn=0while read arr[$n]  #通过键盘给数组赋值 ... The first element of the array is saved in arr[0] ... Enter indicates the input interval for the element, ctrl-d indicates that the input end   do n=$[$n +1]done              #echo ${arr[*]} #这是一个调试点 to see if the   array was successfully generated (if successful, all array elements are output sideways) len= ${#arr [*]}    #取数组长度for ((i=0;i<len;i++))    #冒泡排序算法 ... Note: The For statement is followed by the "double parenthesis" do    for ((j=0;j<len-i-1;j++)) do        t=$[$j +1]     #为了改进程序的可读性, using the variable T to represent the "arr (j+1) Element"        if [[${arr[$j]}  -gt ${arr[$t]}]]   #双方括号表示条件运算 There must be a space between the expression and-GT means greater than        #if [[${arr[$j]}  -gt ${arr[$[$j +1]}]  #这是 do not use the variable T notation, "arr (j+1) Element" is a bit less intuitive then              term=${arr[$j]}              arr[$j]=${arr[$t]}              arr[$t]= $term        fi    donedonefor ((k=0;k<len;k++))  #排序结束之后, and sequentially output array elements do    echo ${arr[$k]} Done

Script Run Result:

Description: The figure above the blue arrow is the user input (each input a number will knock the ENTER, all the digital input after the knock Ctrl+d indicates the end of the input), the blue arrow below the number is the program output (is arranged in order from small to large)

Note: Shell script, can be very easy to implement "indefinite long array" (that is, when declaring an array can not explain the length of the array, but based on the user's input "automatic" to determine the length of the array, such as user input 10 array array length is 10), it seems that C language or Java is not easy to implement this ... Indefinite long array, such things may be the advantage of interpretive language

The For statement for a shell script is divided into two types:
One is that a "walker" (that is, a for-in loop) is similar to a foreach statement in Java, where a collection is preceded by a loop, and the process of looping through the elements of a collection ... can refer to my "Shell programming 99 multiplication Table"
One is that "automaton" is similar to a for loop in C, which needs to describe the boundary and step of the loop ... This is what this article describes: the keyword for followed by a double parenthesis, in which the loop control variable is used to describe the starting condition, the termination condition, and the change of the variable (self-increment) Law ... This is almost the same as the C language for loop.

 

Bubble sequencing of shell programming

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.