Write a shell script that will accept any number of command line interger parameters a d do the following....I have to use a if statment for the lowest and highest values???


Determind the lowest and highest values?

Determind the sum of all parameters entered?

Recommended Answers

All 8 Replies

Hi bossman5000!

What have you tried so far? I can think of a few ways to do those operations.

One of the simplest ways to get the lowest/highest values is to use 'sort'.

There are a few ways to do the math as well. You can use something like 'bc', or it might be more efficient to use the bash built-in.

The fun part is accepting "any number of command line interger parameters". For that, you'll probably need to determine the number of arguments ($#), and loop through them.

Show us some code, let us know which parts are challenging you, and we can probably help work through it!

the low and high values??? what u Gromit think??


for num in $*

do

if [ num -lt lowest}
then
echo " The low number is : $num"
fi
if { $num -gt high ]
then echo "High num is: $num"
fi

The sum of all parameters entered??
how do i get the sum of all numbers??

for the low and high variable...do i hold a number in them...

VALUE=100
VALUE=$((VALUE + 20))

That should answer both of your open questions.

I dont understand.....where do i VALUE=100VALUE=$((VALUE + 20))at??

I dont understand.....where do i put VALUE=100VALUE=$((VALUE + 20))at?? Do i put them in my if statemnets

Hi Bossman5000!

L7Sqr was answering your question about how to store a number in a variable, which is really the first step that you need to know for the operations that you're trying to do here.

Personally, I'd use a quick and dirty temporary file for something like this, but you could also easily put your command line arguments into an array, and do a bubble sort, like in the example here: http://tldp.org/LDP/abs/html/arrays.html

One of the simplest ways to do this, however, would be to write your command line integer parameters to a temporary file and then 'sort' the file and use 'head' and 'tail' to get the lowest and highest values.

Then to sum it all up, you could loop through the file, adding each number as you go, or use 'awk' to do it in a single line.

I hope this helps. Is this for homework? It sounds like you might want to go through the bash scripting guide to get more familiar with some of the basic operations.

I see what u saying Gromit, but all i need is if statements...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.