LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-19-2005, 05:51 AM   #1
MilkMan
LQ Newbie
 
Registered: Mar 2005
Location: Anderson, South Carolina
Distribution: Red Hat
Posts: 2

Rep: Reputation: 0
Smile Help Script Problem


Hi all, i'm new to linux and i have a problem. I have an assigment where i have to sum any amount of numbers entered on the command line.
EX: ./addany 2 3 4
Where 2 3 4 would be summed and 9 would be outputted. Also if no numbers are entered it just outputs zero. I have to write it using a for loop and a while loop. This is script by the way. I already have the for loop and the code looks like this:

#!/bin/bash

sum=0

for foo in $*
do
sum=$(($sum+$foo))
done

echo "$sum"


...and it works. But i can't get the while loop to save my life. I have tried using a counter, $*, $#, and $@, nothing works. Any help would be greatly appreciated. Thanks in advance.
 
Old 03-19-2005, 06:28 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
LQ.org is NOT here to do your homework, and it is techniaclly against the rules you have agreed to abide by. however i'll give a few tips...

1) you can use "test" (or it 's [ ] notation) to see if $* is empty
2) you can use "shift" to move the command line parameters along by a given amount.
 
Old 03-19-2005, 05:31 PM   #3
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
This is to MilkMan:

This question is unrelated to your quest but to satisfy my own curiosity.

When I was in school 50 some odd years ago and we had an assignment, the solutions to it were based on information available in a text book.

Today, what sources of information are you students given to help solve things like this?

Anyone else out of school recently care to comment?

Thanks
Dave
 
Old 03-20-2005, 03:34 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well i recently completed a degree in comuter engineering, and never once did i get someone else to do the work for me
 
Old 03-20-2005, 10:06 AM   #5
Pauli
Member
 
Registered: Feb 2004
Location: Montreal
Distribution: Gentoo/Debian
Posts: 365

Rep: Reputation: 30
You have a degree in computer engineering and can't write a simple script?
 
Old 03-20-2005, 10:21 AM   #6
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quote:
Originally posted by acid_kewpie
well i recently completed a degree in comuter engineering, and never once did i get someone else to do the work for me
As my youngest is 45 you can see by this follow up question that it has been years and years and years since I have been in or around a classroom of any level of education.

For CS courses are there standard text books adopted by the CS department or does each instructor sellect something that you would obtain from a bookstore? Such as "Teach Yourself C for Dummies".
 
Old 03-20-2005, 01:53 PM   #7
MilkMan
LQ Newbie
 
Registered: Mar 2005
Location: Anderson, South Carolina
Distribution: Red Hat
Posts: 2

Original Poster
Rep: Reputation: 0
The text book that is given is for administration of Unix servers and has no script examples. IF you will read my first post you will see the problem is two steps: To be written with a for loop and in a while loop. I solved the for loop using the Man Bash pages, and tried to use the same process but i'm having trouble figuring out with $ to use to take input off the command line. I'm not asking anyone to do my work for me, if the text book contained any programming information I wouldn't even be here, I just want to learn how, and i thought the purpose of this site was to help each other. I'm not a cheater, but i can't figure it out from the man pages. If you don't want to help fine, but don't call me a cheater.
 
Old 03-20-2005, 03:02 PM   #8
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quote:
Originally posted by MilkMan
The text book that is given is for administration of Unix servers and has no script examples.
If you took my questions as an accusation, then I apoligize. There are a lot of questions simular to yours and I was wondering why.

Is your assignment based on a question in the text book? It seems odd that you would be asked to solve such a basic problem in an advanced course. Forgive me! Just an old man rambling about the younger generations problems.

Let me give you a line that might jog your mind into the correct path.

while shift != newline
 
  


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 problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
PPPD Script problem when cron-scheduling the script andresurzagasti Linux - Networking 0 11-24-2004 02:54 PM
Script file problem (or operator problem) HELP! lostone Programming 2 05-23-2001 02:30 PM
Script file problem (or operator problem) HELP! lostone Linux - General 1 05-17-2001 06:11 PM
Script file problem (or operator problem) HELP! lostone Linux - Newbie 0 05-15-2001 07:59 PM

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

All times are GMT -5. The time now is 11:45 AM.

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