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