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:

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

Why must you convert it? If it is for "homework"/"classes", then good luck.

Here are a couple of hints to get you going:

  • ${#} reports the number of arguments currently available
  • shift decrements the number of available arguments

Combining those two should give you ample ammunition for a while loop.

The construct, by the way, is

while [[ ${CONDITION} ]]; do
   # stuff here
done
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.