How to take set of numbers?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to take set of numbers?
# 1  
Old 05-06-2011
Error How to take set of numbers?

I have to take a list of numbers from the keyboard and not by passing arguments. How will I read a set of numbers in such a way that I can use any number I wish to operate upon. Is there any specific command to do so. As said before I dont want to pass the numbers as arguments from command line.
Example:take a list of numbers from the keyboard and add them.
# 2  
Old 05-06-2011
Use read
bash code:
  1. while true; do
  2. read -p "Enter a number (leave blank to finish) : " N
  3.     [ -z "$N" ] && break
  4.     ((Total+=N))
  5. done
  6. echo "Sum of entered numbers : $Total"
  7. exit 0
NOTE: There isn't any check for a valid number
# 3  
Old 05-06-2011
Thanks for the reply. It works! :-D
# 4  
Old 05-06-2011
Code:
#!/bin/sh
echo -n "enter list of numbers (seperated by space) -"
read nums
echo $nums | awk '{for(i=1;i<=NF;i++){tot+=$i}} END {print tot}'

invocation
Code:
sh readnumber.sh
enter list of numbers (seperated by space) -2 4 6 8
o/p
20

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

awk - set numbers [ 1 ... n] from the 6 line

Hi, i have a file, where measurement-data is stored in the first column. The file has also a header of 5 lines. I want to set counting up numbers in front of any particular measurement-value; should start at the 6. line with starting number 1. i try to solve it with ... awk 'NR > 6 { print... (6 Replies)
Discussion started by: IMPe
6 Replies

3. Shell Programming and Scripting

awk script to filter the numbers which are around the set value

Hi All, I have one sensor output(over the same) for a set value of 20. Time(in Sec), Data 1, 16 2, 20 3, 24 4, 22 5, 21 6, 20 7, 19.5 8, 20 9, 20.5 10, 20 11, 20 12, 19.5 Here we can see like after 5 sec of time the data value reaches to 20+-0.5 range. So I... (7 Replies)
Discussion started by: ks_reddy
7 Replies

4. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

5. Shell Programming and Scripting

Compute the median of a set of numbers with AWK?

Is there a way in awk to compute the median of a set of numbers in a file in the following format. 34 67 78 100 23 45 67 (3 Replies)
Discussion started by: Lucky Ali
3 Replies

6. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

7. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

8. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies

9. Programming

How to set constrain on random numbers in c

Hi, I am currently trying to generate multiple random numbers in C for different variable:- die1=1+(rand()%5); die2=1+(rand()%5); die3=1+(rand()%5); die4=1+(rand()%5); But I need to contrain the total of die1, die2,die3 and die4 to be 5 as well. If i insert die1+die2+die3+die4=5, i do... (6 Replies)
Discussion started by: ahjiefreak
6 Replies
Login or Register to Ask a Question