1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

while loop problem

Discussion in 'Linux and Unix' started by pfm200586, Nov 9, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. pfm200586

    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
     
  2. jiml8

    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?
     
  3. pfm200586

    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
     
  4. TerryNet

    TerryNet Moderator

    Joined:
    Mar 23, 2005
    Messages:
    79,909
    First Name:
    Terry
    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?
     
  5. jiml8

    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.
     
  6. Squashman

    Squashman 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
     
  7. Sponsor

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 733,556 other people just like you!

Loading...
Similar Threads - while loop problem
  1. leopardunknown
    Replies:
    1
    Views:
    524
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1026097

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice