Question

Write a Unix shell script that takes three integers and sorts them from largest to smallest....

Write a Unix shell script that takes three integers and sorts them from largest to smallest. An example execution is as follows (assume the name of the script is shs): % shs 3 9 4 The order is: 9 4 3 % shs -1 0 3 The order is: 0 -1 -3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM

# !/bin/bash
declare num=(3 9 4)
# This will print the array before sorting
echo "Original Numbers in array:"
for (( i=0; i<=2; i++ ))
do
echo ${num[$i]}
done
# Now lets do the Sorting of numbers
for (( i=0; i<=2; i++ ))
do
for (( j=$i; j<=2; j++ ))
do
if [ ${num[$j]} -gt ${num[$i]} ]; then #Just using a sorting technique with O(n^2)
t=${num[$i]}
num[$i]=${num[$j]}
num[$j]=$t
#swapping the array to get desired result
fi
done
done
# Printing the sorted array
echo -e "\nSorted Numbers in Descending Order
Order:"
for (( i=0; i <= 2; i++ ))
do
echo ${num[$i]}
done

OUTPUT OF THE PROGRAM

This is the running code..It may cause problem due to indentation because i have pasted here from my file.....HOPE YOU WILL FIND THIS HELPFUL... if you have any queries regarding this let me know.

Add a comment
Know the answer?
Add Answer to:
Write a Unix shell script that takes three integers and sorts them from largest to smallest....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that inputs ten (10) integers and then sorts them in order from largest...

    Write a program that inputs ten (10) integers and then sorts them in order from largest to smallest in the programming language called Perl. You do not need to error check the input; you can assume the user enters integers. You can select the sorting algorithm of your choice, but you must implement this algorithm yourself. You cannot use a built-in sorting function. [15 points] Below is an example of a sample program run: Unsorted: 10, 4, 23, 99, 7,...

  • In UNIX: write a shell script that takes a FileName.awk as an argument. The shell script...

    In UNIX: write a shell script that takes a FileName.awk as an argument. The shell script then uses that file name to run your awk script and sort the results alphabetically, by student name. data.csv Student,Catehory,Assignment,Score,Possible Chelsey,Homework,H01,90,100 Chelsey,Homework,H02,89,100 Chelsey,Homework,H03,77,100 Chelsey,Homework,H04,80,100 Chelsey,Homework,H05,82,100 Chelsey,Homework,H06,84,100 Chelsey,Homework,H07,86,100 Chelsey,Lab,L01,91,100 Chelsey,Lab,L02,100,100 Chelsey,Lab,L03,100,100

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • Unix code problem Write a shell script that does the following : Read many lines of...

    Unix code problem Write a shell script that does the following : Read many lines of text from the standard input device (keyboard) and copy them to the standard output. Precede each output line with a line number Explain all steps

  • UNIX QUESTION Write an argument-less shell script "Sum4.csh", which reads four integer values from the keyboard,...

    UNIX QUESTION Write an argument-less shell script "Sum4.csh", which reads four integer values from the keyboard, computes their sum, then displays the sum in expression format. EXAMPLE: % Sum4.csh Enter the first number: 4 Enter the second number: 7 Enter the third number: 3 Enter the fourth number: 11 RESULT: 4 + 7 + 3 + 11 = 25

  • NIT1202 Operating Systems UNIX Script You are required to write a Bourne Again Shell Script (bash)...

    NIT1202 Operating Systems UNIX Script You are required to write a Bourne Again Shell Script (bash) to manage a menu driven program. When executed the user should be presented with a menu with 4 (four) options: Print the following user information: the home directory, path, userid , login shell together with the current date and time. Ask a user a range and it displays eight random number between the range. [Range difference must be 100. Example Enter a range: 200...

  • Unix classes Write a bash script that take in three integer numbers from the command line...

    Unix classes Write a bash script that take in three integer numbers from the command line and print out the sum and the average. Name the script awesum.sh hb1170uxb4:~$ ./awesum.sh 100 200 300 sum: 600 average: 200 hb1170uxb4: ~S

  • write a program in x86 assembly language that sorts 10 integers entered from the keyboard and displays them in order

    write a program in x86 assembly language that sorts 10 integers entered from the keyboard and displays them in order

  • Write a script, shuffle.sh which generate. Unique integers and prints them out sorted in ascending order,...

    Write a script, shuffle.sh which generate. Unique integers and prints them out sorted in ascending order, and then shuffled. The count of the integers is given as a command-line argument. The shell variable RANDOM generates a random integer upon evaluation. In other words echo $ random prints a random integer. You may assume that the command line argument is a valid positive integer. Use only shell constructs and the array structure. In other words, don’t use commands such as sort,...

  • USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file,...

    USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file, and print (user’s choice of printers) identical form letters to each recipient. Either E197, E-199, E-194 printers or to project2.output. Prompt the user for their choice. 2) The script and output file are due: per Moodle timeline. Please upload your fully functioning script (project2.sh) your data file (project2.input) and your output file (project2.output) into Moodle. 3) Requirements of the script. a. You can use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT