LinuxQuestions.org
Visit Jeremy's Blog.
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-22-2004, 10:04 AM   #1
Cruger
LQ Newbie
 
Registered: Mar 2004
Posts: 14

Rep: Reputation: 0
Question Adding numbers, break on non-numbers...


Hi agian...
2 biiiiiig problems on the same day.... maybe I should have stayed in bed this morning...
Anyways... trying to help a friend with a problem...

He's trying to write a script that adds numbers that are written in when executing the script...

What I've come up with this far is:
-----------------------------------------------------------------------------
#!/bin/bash
sum=0

for i in $*
do
sum=`expr $i + $sum`
done

echo "The sum of the numbers entered is $sum"
------------------------------------------------------------------------------

The only problem is that he want's it to break if a letter (character, non-numberic) is entered... how do I solve this?
 
Old 03-22-2004, 10:18 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Try:
Code:
for i in $*
do
okay=1
okay=`tr -s '[:alpha:]' '0'`
if [ $ok -eq 0 ] ; then
    break;
fi
sum=`expr $i + $sum`
done

Last edited by jim mcnamara; 03-22-2004 at 11:03 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
(Dail-up connection) - adding multi phone numbers per ISP medya Linux - Newbie 2 10-22-2005 12:57 PM
random numbers deveraux83 Programming 4 05-17-2005 03:26 AM
Even numbers paraiso Linux - Newbie 4 05-15-2005 04:31 PM
what do the numbers mean? drisay Slackware 1 12-14-2004 08:30 AM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 06:25 PM


All times are GMT -5. The time now is 01:16 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration