LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2004, 08:32 PM   #1
RowanB
LQ Newbie
 
Registered: Nov 2004
Posts: 8

Rep: Reputation: 0
Could someone tell me what's wrong with my shell script please?


Hi, I'm trying to write a shell script that allows the user to enter an arbitrary number of values and then returns the sum of all the values entered. The following happens when I try to run the script. Can someone tell me what I am doing wrong please? Help appreciated!

Enter the number of numbers you want to add together
6
Enter number
1
./sum3.txt: line 6: [: too many arguments


Here is my script:

#!/bin/sh
s=1
sum=0
echo "Enter the number of numbers you want to add together"
read N
while [ $s -le $N ]
do
echo "Enter number"
read D
sum='expr $sum + $D'
s='expr $s + 1'
done
echo "Answer = $sum'
 
Old 11-11-2004, 09:31 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Some problems I see...
1) The line sum='stuff' should use back quotes instead of single quotes
2) The line s='stuff', same as above
3) the last echo line should end in a double quote to match opening quote
 
Old 11-11-2004, 09:49 PM   #3
RowanB
LQ Newbie
 
Registered: Nov 2004
Posts: 8

Original Poster
Rep: Reputation: 0
OK, that's a bit of progress. Now this is what happens:

Enter the number of numbers you want to add together
3
Enter number
4
./sum3.txt: line 6: [: too many arguments
Answer = expr 0 + 4
 
Old 11-11-2004, 10:49 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
How about this ...
Code:
#!/bin/sh
s=1
sum=0
echo "Enter the number of numbers you want to add together"
read num
while [ $s -le $num ]
do
echo "Enter number"
read stuff
sum=`expr $sum + $stuff`
s=`expr $s + 1`
done
echo "Answer = $sum"
 
Old 11-11-2004, 11:17 PM   #5
RowanB
LQ Newbie
 
Registered: Nov 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by homey
How about this ...
Code:
#!/bin/sh
s=1
sum=0
echo "Enter the number of numbers you want to add together"
read num
while [ $s -le $num ]
do
echo "Enter number"
read stuff
sum=`expr $sum + $stuff`
s=`expr $s + 1`
done
echo "Answer = $sum"
Brilliant. Thanks a lot.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration