Sort numbers using arrays in Shell Script

By: Vivek G Emailed: 1789 times Printed: 2623 times    

This shell script demonstrates the use of arrays with a script that sorts 5 numbers and prints the sorted numbers.

#
# 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

Most Viewed Articles (in Linux )

Latest Articles (in Linux)

smskannel SMS gateway run in background

Running jar files in background in ssh window

Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697

Can't locate ExtUtils/MakeMaker.pm in @INC ...

bash: make: command not found

apxs: command not found

bash: svn: command not found

make: Nothing to be done for `all'.

How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows

How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows

How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows

How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows

How to burn your CD / DVD ISO image using k3b on CentOS

Compiling and Installing software from source in Linux

Installing RPM's using the Redhat Package Manager in Linux

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

smskannel SMS gateway run in background

Running jar files in background in ssh window

Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697

Can't locate ExtUtils/MakeMaker.pm in @INC ...

bash: make: command not found

apxs: command not found

bash: svn: command not found

make: Nothing to be done for `all'.

How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows

How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows

How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows

How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows

How to burn your CD / DVD ISO image using k3b on CentOS

Compiling and Installing software from source in Linux

Installing RPM's using the Redhat Package Manager in Linux

Archived Comments

1. thanks ...
View Tutorial          By: Dipesh at 2011-09-07 17:05:21

2. how to declare Array ?? in LINUX
i m try th

View Tutorial          By: Chand at 2012-03-18 06:47:09

3. its very nice
View Tutorial          By: keerthi at 2012-06-28 17:04:36

4. hii
i have

two varirables
View Tutorial          By: niranjan at 2012-07-31 06:45:46

5. hii
i have

two varirables
View Tutorial          By: niranjan at 2012-07-31 06:47:36

6. does not work.

output:
./num

View Tutorial          By: praveen at 2012-08-30 19:04:20

7. This will give the output check it out


View Tutorial          By: shruthi at 2013-01-18 06:00:08

8. hahaha
View Tutorial          By: seshu babu at 2015-04-20 06:29:14

9. blank
View Tutorial          By: veera venkata satya naga maha veera ambika trimurthulu at 2015-04-20 06:32:54