I’m trying to write a shell program that, given an unsorted list of numbers (one per line, any sign, any size), returns the largest number given one argument or returns the smallest number given another argument. That is, if I type “myProgram 1 list”, it outputs the largest number, but if I type “myProgram 0 list”, it outputs the smallest number.
I think I know how to determine which number to output (switch case with the first argument?) but I have no idea how to go about finding the largest/smallest number in the list. Is there some simple way to go about finding this number? Could anyone give me a few tips? Thanks.
We don’t want to spoil all the fun, do we? You didn’t ask for a solution, you just asked for tips. So here we go:
min=""xtomin(What ifminis empty?)min=xminAll you have to do is translate this to sh, and also the opposite for
max.There may be easier ways, but this is the simplest pure-sh algorithm I can think of.