Sponsored Content
Full Discussion: Read numbers from file
Top Forums Shell Programming and Scripting Read numbers from file Post 302805451 by Don Cragun on Friday 10th of May 2013 05:49:45 AM
Old 05-10-2013
Quote:
Originally Posted by vish6251
I am trying to make a script to read marks from a file then find out how many of them are above 40 (passing marks). However my script is getting stuck at "read num". I dont understand whats the problem. Any help will be much appreciated.

Code:
#!/bin/bash
set -x
count=0; countP=0;  PASSMK=40
max=-1;  min=999999
tot=0;   avg=0

read num #read the first input number
while [ $num -ge 0 ]; do
  tot=$(($tot+$num))
  if [ $num -gt $max ]; then
    max=$num
  fi
  if [ $num -lt $min ]; then
    min=$num
  fi

  count=$(($count+1))
  if [ $num -ge $PASSMK ]; then
    countP=$(($countP+1));
  fi

  read num #read the next input number
done

if [ $count -gt 0 ]; then
  avg=$(($tot/$count))
fi
printf "entries\tpasses\tmax\tmin\tavg\n"
printf "%d\t%d\t%d\t%d\t%d\n" "$count" "$countP" "$max" "$min" "$avg"

You said you want the read commands to read from a file, but you haven't specified any file. Therefore, both read commands will read from the standard input for your bash script. In other words, the script is waiting for you to type in values to be processed.

If you want to choose a different file each time you run the script, you can redirect the input of your script when you run it, as in:
Code:
script_name < input_file

If you always want the read command in your script to read from the same file every time you run the script, change the line marked in red in your script above with:
Code:
done < input_file

This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

3. Shell Programming and Scripting

Help with addition of 2 numbers that are read from a file

I am trying to add free and used memory (so that i can compute percentage used)of remote nodes using shell script. I use the openssh-server,expect tool and ssh script. 1)login.txt (info of nodes): ip1|username|password ip2|username|password . . . 3)sshlogin.sh #!/bin/bash ... (1 Reply)
Discussion started by: marmik1903
1 Replies

4. 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

5. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

6. Shell Programming and Scripting

Read in numbers from a datafile

Hi, I want to be able to read numbers from many files which have the same general form as follows: C3H8 4.032258004031807E-002 Phi = 1.000000E+00 Tau = 5.749E+00 sL0 = 3.805542E+01 dL0 = 1.514926E-02 Tb = 2.328291E+03 Tu = 3.450E+02 Alpha = ... (3 Replies)
Discussion started by: lost.identity
3 Replies

7. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

8. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

9. Programming

Read in numbers from console won't stop at newline.

Hello, I have snippet code from Lippman's <<C++ primer>>. The program is to convert regular decimal (0 ~ 15) numbers to basic hexdecimals. The instruction tells the program will execute by hitting newline at the end. When I tried to run the compiled program, hitting ENTER did not work as... (3 Replies)
Discussion started by: yifangt
3 Replies

10. UNIX for Beginners Questions & Answers

Read a file with decimal numbers in bash

Hello, I have the following script while read id fraction do sambamba -h -f bam -t 10 --subsampling-seed=50 -s $frac ${id}.bam -o ${id}.out.bam done < fraction.txt where fraction.txt has two columns (id,fraction) and 50 rows I am unable to run this as bash is not able to read the second... (2 Replies)
Discussion started by: nans
2 Replies
fribidi_set_reorder_nsm(3)					Programmer's Manual					fribidi_set_reorder_nsm(3)

NAME
fribidi_set_reorder_nsm - set marks reordering on or off SYNOPSIS
#include <fribidi.h> fribidi_boolean fribidi_set_reorder_nsm(fribidi_boolean state); PARAMETERS
fribidi_boolean state New state to set. DESCRIPTION
This function is used to turn non-spacing marks reordering on or off. Reordering non-spacing marks is the act of placing non-spacing marks (bidi class NSM) after their base character in a right-to-left resolved context. If your rendering engine expects non-spacing marks always after the base character in the memory representation of the visual string, you need this option on. An example of where people may need it off is when rendering in the console when non-spacing marks cannot be applied on top of the base character. This flag is on by default. This function is deprecated and only used with other deprecated functions. RETURNS
The new marks reordering status. SEE ALSO
fribidi_charset_to_unicode(3), fribidi_unicode_to_charset(3), fribidi_parse_charset(3), fribidi_shape_arabic(3), fribidi_get_par_direc- tion(3), fribidi_get_par_embedding_levels(3), fribidi_reorder_line(3), fribidi_get_bidi_type(3), fribidi_get_bidi_types(3), fribidi_get_bidi_type_name(3), fribidi_debug_status(3), fribidi_mirroring_status(3), fribidi_set_mirroring(3), fribidi_reorder_nsm_sta- tus(3), fribidi_log2vis_get_embedding_levels(3), fribidi_get_type(3), fribidi_get_type_internal(3), fribidi_remove_bidi_marks(3), fribidi_log2vis(3), fribidi_join_arabic(3), fribidi_get_joining_type(3), fribidi_get_joining_types(3), fribidi_get_joining_type_name(3), fribidi_get_mirror_char(3), fribidi_shape_mirroring(3), fribidi_shape(3) GNU FriBidi 0.19.2 30 January 2006 fribidi_set_reorder_nsm(3)
All times are GMT -4. The time now is 12:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy