help with extremely basic problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers help with extremely basic problem
# 1  
Old 04-26-2008
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

# 2  
Old 04-26-2008
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.
# 3  
Old 04-26-2008
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. :/
# 4  
Old 04-27-2008
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

# 5  
Old 04-27-2008
thank you very much ruben. appreciate it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Very basic problem with fork() using c

Hi guys, I have the following code: int main(int argc, char *argv) { int pid1,pid2,i=0; pid1=fork(); i+=2; if(!pid1) i++; if(i%3) pid2=fork(); if (pid2==0) { printf("sea \n "); i-=1; } if(i>=2)... (4 Replies)
Discussion started by: pfpietro
4 Replies

2. UNIX for Dummies Questions & Answers

Basic problem with pdftotext

Hi, I have used pdftotext with good results in the past, but today for some reason I keep getting the same error message. My command is as follows: And the error message is I am using Vmware player with Ubuntu server, but I don't think that is causing this issue as I have been using... (2 Replies)
Discussion started by: Joq
2 Replies

3. UNIX for Dummies Questions & Answers

Basic if statement problem

I am using the following code: if ; then I am getting an error saying line 12: The code is supposed to examine whether the filename read into $FILE includes the string IMG*. I do have a fi later in the script, but I must be missing something obvious. Can anyone help? (3 Replies)
Discussion started by: Bengel
3 Replies

4. Shell Programming and Scripting

Expr problem and other basic problems

Hello, I am new to the Bash scripting language, and was given a tutorial page on how to setup a file. However I am trying to use cygwin to run this file and it is not working. $ vi averagetime.sh # # # echo "Enter Dictorinary File Text " read dict echo "Enter Grid Name" read grid... (13 Replies)
Discussion started by: killerqb
13 Replies

5. UNIX for Dummies Questions & Answers

Basic number checking problem

Hello all I am having problems using a bash script to read the input from the user and checking that its a valid number. I only want the user to input a maximum of a 3 number string (321 , 521 , 871 etc.). Anything longer or that includes a chararcter or symbol will display an error message. ... (8 Replies)
Discussion started by: ChrisHoogie
8 Replies

6. Shell Programming and Scripting

Basic problem

Hello Friends, I am learning Perl now. I have a small query. I have a directory Z with file name Z.txt. I would like to copy this file Z.txt to 3 new dir with new filenames as follows dir 1 1.txt dir 2 2.txt dir 3 3.txt I would like to then open 1.txt from dir 1 and edit the first... (0 Replies)
Discussion started by: ramesh54
0 Replies

7. Shell Programming and Scripting

Basic Scipting problem need help for school

How to make this script? 1. Write a portable bash shell script called change_password.bash that will prompt the user for a password. Use a series of if statements to test if: 1. The password is NOT 6 or more characters 2. The password does not contain at least 3 consecutive letters... (1 Reply)
Discussion started by: 3junior
1 Replies

8. Programming

Basic multi module problem

I am trying to learn how to use multiple modules and hearder files. I have tried a little experiment but cannot get it to work. Here is my code and compilation attempt. Any help with finding my problems appreciated. The main function (main01.c) calls a function located in another file... (9 Replies)
Discussion started by: enuenu
9 Replies
Login or Register to Ask a Question