bash 在 Linux shell 脚本中,如何打印数组的最大值和最小值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13634429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:01:52  来源:igfitidea点击:

In Linux shell script how do i print the largest and smallest values of a array?

bashsh

提问by BobbyT28

I dont really understand much about arrays but i need to know how to find and print the largest and smallest values of an array. The array is predefined by a read command, the user will be prompted to enter n amount of integers.

我不太了解数组,但我需要知道如何查找和打印数组的最大值和最小值。该数组由读取命令预定义,用户将被提示输入 n 个整数。

How would i assign the read input to an array and find and display the largest and smallest values of the array?

我如何将读取输入分配给数组并查找并显示数组的最大值和最小值?

Is there a way to test the array elements to see if they are all integers?

有没有办法测试数组元素以查看它们是否都是整数?

#!/bin/bash

read -a integers

biggest=${integers[0]}
smallest=${integers[0]}

for i in ${integers[@]}
do
     if [[ $i -gt $biggest ]]
     then
        biggest="$i"
     fi

     if [[ $i -lt $smallest ]]
     then
        smallest="$i"
     fi
done

echo "The largest number is $biggest"
echo "The smallest number is $smallest"

回答by sampson-chen

The general idea is to iterate through the array once and keep track of what the maxand minseen so far at each step.

一般的想法是遍历数组一次并跟踪每一步到目前为止看到的maxmin看到的内容。

Some comments and explanations in-line (prefixed by #)

一些评论和解释(以 为前缀#

# This is how to declare / initialize an array:
arrayName=(1 2 3 4 5 6 7)

# Use choose first element of array as initial values for min/max;
# (Defensive programming) - this is a language-agnostic 'gotcha' when
# finding min/max ;)
max=${arrayName[0]}
min=${arrayName[0]}

# Loop through all elements in the array
for i in "${arrayName[@]}"
do
    # Update max if applicable
    if [[ "$i" -gt "$max" ]]; then
        max="$i"
    fi

    # Update min if applicable
    if [[ "$i" -lt "$min" ]]; then
        min="$i"
    fi
done

# Output results:
echo "Max is: $max"
echo "Min is: $min"

回答by Gilles Quenot

Try this if you need to compare(signed or not) INTegers:

如果您需要比较(签名与否)INTegers ,请尝试此操作

#!/bin/bash

arr=( -10 1 2 3 4 5 )

min=0 max=0

for i in ${arr[@]}; do
    (( $i > max || max == 0)) && max=$i
    (( $i < min || min == 0)) && min=$i
done

echo "min=$min
max=$max"

OUTPUT

输出

min=-10
max=5

EXPLANATIONS

说明

回答by gniourf_gniourf

A funny way using sort:

使用排序的一种有趣方式:

if you have an array of integers, you can use sortto sort it, then select the first and last elements to have the min and max elements, as in:

如果你有一个整数数组,你可以用sort它来排序,然后选择第一个和最后一个元素来包含最小和最大元素,如下所示:

{ read min; max=$(tail -n1); } < <(printf "%s\n" "${array[@]}" | sort -n)

So if you want to prompt user for say 10 integers, check that the user entered integers and then sort them, you could do:

因此,如果您想提示用户输入 10 个整数,请检查用户是否输入了整数,然后对它们进行排序,您可以这样做:

#!/bin/bash

n=10
array=()

while ((n));do
   read -p "[$n] Give me an integer: " i
   [[ $i =~ ^[+-]?[[:digit:]]+$ ]] || continue
   array+=($i)
   ((--n))
done

# Sort the array:
{ read min; max=$(tail -n1); } < <(printf "%s\n" "${array[@]}" | sort -n)
# print min and max elements:
echo "min=$min"
echo "max=$max"