ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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".
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.