você pode usar o tipo com o interruptor inverso:
echo -e "$n1\n$n2\n$n3\n$n4\n$n5" | sort -rn
Estou tentando criar um script que receberá 5 números e, em seguida, classificá-los do maior para o menor. Até agora, isso é o que eu tenho:
#!/bin/bash
clear
echo "********Sorting********"
echo "Enter first number:"
read n1
echo "Enter second number:"
read n2
echo "Enter third number:"
read n3
echo "Enter fourth number:"
read n4
echo "Enter fifth number:"
read n5
A melhor maneira de programar esta tarefa.
#!/bin/bash
# put number names into array
number_names_arr=(first second third fourth fifth)
# use loop, because code duplication is a bad practice.
# It repeats five times all commands it have inside.
for ((i = 0; i < 5; i++)); do
# Prompt line
echo "Enter ${number_names_arr[i]} number"
# read inputted number into array
read -r numbers_arr[i]
done
echo "Output:"
# pass the content of array to the sort command
printf '%s\n' "${numbers_arr[@]}" | sort -rn
Isso deve funcionar:
#!/usr/bin/env bash
array=("${@}")
while [[ "${1++}" ]]; do
n=${1}
<<< "${n}" grep -P -e '^(([+-]?)([0-9]+?)(\.?)(([0-9]+?)?))$' > '/dev/null' \
|| { echo "ERROR: '${n}' is not a number"; exit 1; }
shift
done
printf '%s\n' "${array[@]}" | sort -rg
Exemplo:
$ myscript.sh 12 -45 2 -27.75 2.2 0 +25 100 2.15
100
+25
12
2.2
2.15
2
0
-27.75
-45