The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extremely New gss UNIX for Dummies Questions & Answers 1 12-19-2007 03:15 PM
extremely new to unix, need help s2pids2pid UNIX for Dummies Questions & Answers 1 11-06-2007 10:04 PM
Basic Scipting problem need help for school 3junior Shell Programming and Scripting 1 10-24-2007 07:38 AM
Basic multi module problem enuenu High Level Programming 9 05-28-2007 04:43 PM
extremely headache yatno UNIX for Dummies Questions & Answers 5 04-05-2001 06:16 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 6
help with extremely basic problem

Alright i'm having problems with this loop. Basically once the script is ran a parameter is required. Once entering the parameter it displays that in LIMIT. Alright so now the problem, I need my loop to ask my user for a number, and if that number is less than the limit then sum the input values until it reaches the limit.

example output of what i need

LIMIT = 50
please enter a number: 10
I = 10 SUM = 10
please enter a number: 15
I = 15 SUM = 25
please enter a number: 20
I = 20 SUM = 40
please enter a number: 15
done!

this is the code i have so far, the SUM PART is messed up and i dont know how to exit the loop and display done once it reached the limit.

Code:
#!/bin/sh

if [ $# -ne 1 ]; then
    echo "usage: help.sh LIMIT"
exit
fi

LIMIT=$1

echo "LIMIT =" $LIMIT

read -p"Please enter a number: " I

echo "I =" $I "SUM =" $I

while [ $I -le $LIMIT ]
do

read -p"Please enter a number: " X

echo "I =" $X "SUM =" `expr $I + $X`

done
Reply With Quote
Forum Sponsor
  #2  
Old 04-26-2008
Registered User
 

Join Date: Aug 2007
Posts: 21
Buddy,

I am still not able to figure out the requirement. But if you want to come out of the loop then give the input greater than 50 because this is the LIMIT which you have set in the beginning. The while loop will check this input value with the LIMIT value and since the value is greater than 50 so it wont go inside the loop again.
For displaying "done", you can use echo or print command after the while loop.

If this is not what you want then please help me understand the requirement.
Reply With Quote
  #3  
Old 04-26-2008
Registered User
 

Join Date: Mar 2008
Posts: 6
Alright well this is what i need.

To run the script.
help.sh
it will display that you need to specify a parameter which is limit
so in this case.
help.sh 50

LIMIT = 50
please enter a number: 10
I = 10 SUM = 10
please enter a number: 15
I = 15 SUM = 25
please enter a number: 20
I = 20 SUM = 45
please enter a number: 15
I = 15 SUM = 60
done!

So once entering the limit, it will prompt you to enter a number until you reach that limit, and once reaching that limit it displays done. I cant get the sum to add up correctly, this is what i'm getting.

help.sh 50
LIMIT = 50
please enter a number: (enter number 10)
I = 10 SUM = 10
please enter a number: (enter number 15)
I = 15 SUM = 25
please enter a number: (enter number 20)
I = 20 SUM = 30 (should be 45)
etc.

Its just adding 10 to I each time and displaying it in SUM. :/
Reply With Quote
  #4  
Old 04-27-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 216
The variables are not used correctly, so give this a try instead :

Code:
#!/bin/sh
#set -x

if [ $# -ne 1 ]; then
    echo "usage: help.sh LIMIT"
exit
fi

LIMIT=$1
echo "LIMIT =" $LIMIT

SUM=0

while [ $SUM -lt $LIMIT ]
 do
    read -p "Please enter a number: " I
    SUM=`expr $SUM + $I`
    echo "I = $I and SUM = $SUM"
 done
Reply With Quote
  #5  
Old 04-27-2008
Registered User
 

Join Date: Mar 2008
Posts: 6
thank you very much ruben. appreciate it.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0