while loop problem

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

pfm200586

Thread Starter
Joined
Oct 22, 2011
Messages
6
Hello everyone,

I have this little for loop that I want to convert to a while loop. It accepts unknown number from the command line parameters and then add them all up and display the sum, here is the for loop:

HTML:
sum=0

for x in $*
do 
sum=`expr $sum + $argument`
done

echo $sum
it runs perfectly, any suggestions how to convert it to a while loop?

Thank you

Regards
 

jiml8

Guest
Joined
Jul 2, 2005
Messages
2,634
Looks like homework to me.

I'll give you a hint. You use a while loop so that the loop occurs while some condition is satisfied. Now, what is the condition that must be satisfied for this while loop to keep running? It is that the user enters a number, right?
 

pfm200586

Thread Starter
Joined
Oct 22, 2011
Messages
6
the user enters all the parameters at once. in this for loop all of the parameters is stored in the $* variable so it is easy for the loop to go through all of them, but i cannot understand how to do it in a while loop.



thanks
 

TerryNet

Terry
Moderator
Joined
Mar 23, 2005
Messages
81,423
I'm way too rusty and forgetful to give any more than a possible hint. Isn't there a variable that tells the number of parameters that were specified? Or something that flags the last one?
 

jiml8

Guest
Joined
Jul 2, 2005
Messages
2,634
OK. Your total number of arguments is given by $#. So you can set up a counter starting at 1 and increment that counter until it is greater than the number of arguments. You convert the counter value into the name of the argument you want to look at, and you'll have it.

Take a look at how parameter expansions work. Pay attention to the discussion of indirection.

There is also an obscure brace expansion syntax that allows you to start at a specified point in an array and proceed for a specified number of elements. This works as well as a parameter expansion but you'll have a much harder time finding any information about it.

You can avoid the counter if you wish by looking at each command line variable, after you've obtained it by the appropriate expansion, and continue until you find an empty string for the variable.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Been a very long time since I have done bash scripting but I would assume you could do something like this. Can't remember all the syntax.
Code:
While [ -n $1 ]
do
     sum=`expr $sum + $1`
     shift
done
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top