ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to make a script that accepts ten numbers and displays the highest number entered, so far i have the following:
#!/bin/bash
echo " please enter ten numbers"
echo "enter num 1"
read num1
echo "enter num 2"
read num2
echo "enter num 3" #im only doing 3 numbers now to test it first
read num3
if [ $num1 -gt $num2 -a gt $num3 ;; $num2 -gt $num1 -a $num3 ;; $num3 -gt $num1 -a -gt $num2 ]
then
echo " $num1 is the highest number"
elif
echo " $num2 is the highest number"
else
echo " $num3 is the highest number"
fi
# or what else could i do?
A better solution is to store all values in some array and then use
the selection sort algorithm.
Scan all valuses to find the smallest value, exchange that value with the value in the first position. Then scan the list again(but do not include the first value) to find the smallest value then exchange it with the value in the second position and the continue until the end of the array.
It sounds like homework for sure, but I guess it is ok to give pointers, as long as it serves that he learns somehting.
In this spirit:
Unix/Linux has a tools approach to problems, which means that there are lot of small programms which can be added together (with pipes for example) to solve more complex problems, in this case I would advise that you look at sort(1).
#!/bin/bash
echo "Please enter ANY amount of numbers, each on their own line."
echo "Press CTRL-D when done."
echo
HIGHEST=$(cat | sort -n -r | head -1)
echo "Highest number you entered = $HIGHEST"
echo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.