We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Command line help please

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?

3
Contributors
8
Replies
3 Days
Discussion Span
1 Year Ago
Last Updated
9
Views
Question
Answered
bossman5000
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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!

Gromit
Posting Whiz in Training
224 posts since Sep 2008
Reputation Points: 50
Solved Threads: 34
Skill Endorsements: 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??

bossman5000
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

bossman5000
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
VALUE=100
VALUE=$((VALUE + 20))

That should answer both of your open questions.

L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

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

bossman5000
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

bossman5000
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Gromit
Posting Whiz in Training
224 posts since Sep 2008
Reputation Points: 50
Solved Threads: 34
Skill Endorsements: 0

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

bossman5000
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Gromit and L7Sqr

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0765 seconds using 2.66MB