LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2005, 09:27 AM   #1
JacksWastedLife
LQ Newbie
 
Registered: Nov 2005
Posts: 1

Rep: Reputation: 0
Begginers problem - arbitary numbers


Howdy. I am very new to Linux and am having some some difficulty with a paritcular problem. I am trying to write a script that reads in ten arbitary numbers and prints to the screen the sum of these numbers. Anyone have any ideas how I would go about doing this?
 
Old 11-16-2005, 09:45 AM   #2
gnu2tux
Member
 
Registered: Nov 2005
Location: Scotland, UK
Distribution: Ubuntu
Posts: 32

Rep: Reputation: 15
Haven't tried it but I guess (assuming the arbitary number is a random number from /dev/random) then:

#!/bin/bash

until [ $i -eq 10 ]

do
$i = /dev/random
$sum = $sum + $i
done

echo "The total is: $sum"

if you get an error, its probably my syntax is wonky with the brackets, have a quick squint at the bash/while/until manpage or the likes or on google to debug.

Cheers,

Ali Ross
 
Old 11-16-2005, 09:54 AM   #3
Y0jiMb0
Member
 
Registered: Jul 2003
Location: Valencia (Spain)
Distribution: slackware 11, FEDORA CORE 4, RHEL3, Gentoo...
Posts: 361

Rep: Reputation: 30
Code:
#!/bin/bash

until [ $i -eq 10 ]

do
$i = /dev/random
$sum = $sum + $i
done

echo "The total is: $sum"
This is slightly wrong; you must remove the $ at left hand sides, and beware the spaces in bash scripts, they have to be used when they have to, no more no less.
Moreover, you should initialize the variables, for safety.
This should work:
Code:
#!/bin/bash

sum=0
for j in 0 1 2 3 4 5 6 7 8 9
do
i=$RANDOM
sum=$[sum+i]
done
echo "The total is: $sum"
If you want to read in the numbers from command line (interactively) use the read command ("man read" for more help):
instead of
Code:
i=$RANDOM
use
Code:
read i
Regards

Last edited by Y0jiMb0; 11-16-2005 at 10:18 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
c++ prime numbers loop problem muddlnx Programming 6 08-31-2004 11:14 PM
Characterset problem?: little boxes with numbers alienDog Slackware 4 05-08-2004 08:36 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 10:18 AM
Programming for begginers josh89 Programming 7 11-02-2003 12:01 PM
magic numbers problem muneebs123 Programming 2 09-05-2003 02:33 PM


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