Shell finds the maximum value in an array element

Source: Internet
Author: User

Tag: Shell finds the maximum value in an array element

How does the shell compare the maximum values in all the elements in an array? Such as:

array= (111 222 333 444 555 999 888 777 666) How to find the biggest element 999?

Idea: Set a value max=${array[0]} that is the first element of the array, then use a For loop, take the first element with the second .... When an element such as 3rd is larger than array[0, the value of element 3rd is assigned to Max and the last output is Max!

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/87/51/wKioL1fdJi3D1-eeAABQh5MHT8Q367.png "title=" 99.PNG "alt=" Wkiol1fdji3d1-eeaabqh5mht8q367.png "/>

Text as follows

#!/bin/bash

#Author: Wangergui email:[email protected] date:2016-09-17

#Release 1.0

#Function find maxnum from array

Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:~/bin

Export PATH

Declare-a Array

array= (111 222 333 444 555 999 888 777 666)

Max=${array[0]}

For I in ${!array[@]};d o


if [[${max}-le ${array[${i}]}]];then


Max=${array[${i}]}


Fi


Done


Echo ${max}



This article from the "Linux-related technology" blog, declined to reprint!

Shell finds the maximum value in an array element

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.