1.11M Members

Command line help please

 
0
 

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?

 
0
 

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!

 
0
 

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??

 
0
 

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

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

That should answer both of your open questions.

 
0
 

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

 
0
 

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

 
0
 

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.

 
0
 

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

Question Answered as of 2 Years Ago by JeoSaurus and L7Sqr
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: