2

How to write a script which will receive a list of parameters and output the number that is the largest. If no parameters are supplied, output an error message.

I wrote the following code to check if no parameters are supplied, output an error message.

#!/bin/bash
if [ "$#" -eq  "0" ]
then
  echo "No arugments supplied"
else
  echo "$# Parameter"

But I dont know how to continue...

melpomene
  • 84,125
  • 8
  • 85
  • 148
Alex
  • 91
  • 1
  • 10
  • You could use `sort` and `head` to sort the list in descending order and grab the first item, have you tried that? – mikeb Oct 10 '15 at 15:26
  • What is different if it's homework or not? Who love homework :) – Alex Oct 10 '15 at 15:28

3 Answers3

2

Keep a current max. Loop over the input, updating max if necessary. At the end you'll have the global maximum.

Untested:

#!/usr/bin/bash
if [ $# -eq 0 ]; then
    echo "Usage: $0 NUMBERS" >&2
    exit 1;
fi

max="$1"
shift
while [ $# -gt 0 ]; do
    if [ "$1" -gt "$max" ]; then
        max="$1"
    fi
    shift
done

echo "$max"
melpomene
  • 84,125
  • 8
  • 85
  • 148
1

Use sort -n (numeric) and -r (reverse) and then just pick the first line of the output -- like

#!/bin/bash
if [ "$#" -eq  "0" ]
then
  echo "No arugments supplied"
else
  echo "$# Parameter"
  for i in $*; do echo ${i}; done | sort -nr | head -1
fi  

Now the only problem you are facing is when the the input (the arguments) are not numbers -- but you didn't say anything about what should happen then.

Soren
  • 14,402
  • 4
  • 41
  • 67
  • n * log n complexity. You can do better. – jub0bs Oct 10 '15 at 16:25
  • @Jubobs Complexity depends on the algorithm `sort` uses. It could be O(1). – melpomene Oct 10 '15 at 16:30
  • @melpomene A comparison-based sorting algorithm cannot be faster than n * log n. – jub0bs Oct 10 '15 at 16:31
  • @Jubobs We're only inspecting a fixed prefix of the result, so it doesn't have to sort the whole list. – melpomene Oct 10 '15 at 16:32
  • @melpomene I don't understand your last comment. If the OP wants to find the maximum of all command-line arguments, s/he needs to inspect all n of them. This is what you do in your [own answer](http://stackoverflow.com/a/33055668/2541573). – jub0bs Oct 10 '15 at 16:34
  • @Jubobs Yes, by passing them to sort. But we don't inspect all of sort's result. (Btw, my "could be O(1)" above is a typo; I meant "could be O(n)".) See http://stackoverflow.com/questions/12057658/lazy-evaluation-and-time-complexity. – melpomene Oct 10 '15 at 16:37
  • @melpomene Interesting link (thanks), but I doubt any implementation of POSIX `sort` relies on lazy evaluation. – jub0bs Oct 10 '15 at 16:42
  • @Jubobs The laziness is implicit in the use of pipes (`|`): `head` will stop reading after the first line and exit, which will terminate `sort` with `SIGPIPE` if it tries to produce more output afterwards. It only depends on whether `sort` will output anything before it has finished sorting everything, which in turn depends on the algorithm it uses. – melpomene Oct 10 '15 at 16:51
  • 1
    @Jubobs -- the "complexity" is actually low -- it is one line and not complex at all -- the efficiency (as in big-O notation) may not be the best, however nothing written in a shell script is expected to be the most efficient code -- when it comes to shell scripts simplicity should always win. – Soren Oct 10 '15 at 16:54
  • @melpomene Thanks for your explanation. – jub0bs Oct 10 '15 at 16:55
1

Here's a pseudocode you could implement:

  • save the first param in a variable called max
  • loop over the params
    • if the param is greater than max, update max
  • print max

Here's an example loop that prints all parameters:

for num; do
    echo $num
done

And here's an example of comparing values:

if (( num > max )); then
    echo $num is greater than $max
fi

This should be more than enough help to complete your homework.


Well since others have already gave you the actual solution, here's mine too:

#!/bin/bash

if (( $# == 0 )); then
    echo "No arugments supplied"
    exit 1
fi

max=$1
for num; do
    if (( num > max )); then
        max=$num
    fi
done

echo $max
janos
  • 120,954
  • 29
  • 226
  • 236