#
# Declare the array of 5 subscripts to hold 5 numbers
#
declare nos[5]=(4 -1 2 66 10)
#
# Prints the number befor sorting
#
echo "Original Numbers in array:"
for (( i = 0; i <= 4; i++ ))
do
echo ${nos[$i]}
done
#
# Now do the Sorting of numbers
#
for (( i = 0; i <= 4 ; i++ ))
do
for (( j = $i; j <= 4; j++ ))
do
if [ ${nos[$i]} -gt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
#
# Print the sorted number
#
echo -e "\nSorted Numbers in Ascending Order:"
for (( i=0; i <= 4; i++ ))
do
echo ${nos[$i]}
done
Linux / PRACTICAL TUTORIAL
Sort numbers using arrays in Shell Script
Advertisement
This shell script demonstrates the use of arrays with a script that sorts 5 numbers and prints the sorted numbers.
From the community
No archived comments on this tutorial.