tags:

views:

172

answers:

2

The following is a script I wrote to run an executable ./runnable on arguement/input file input. It takes standard from another file called finalfile and outputs it to a file called outfile. There are 91 lines in finalfile (i.e 91 different standard space delimited inputs) and therefore the bash script should call the ./runnable input 91 times. But, I am not sure why is it calling it only one time. Any suggestions on whats going on wrong? I am new to bash scripting and ergo not very good. Kindly be explanatory. Thanks!

#!/bin/bash

OUTFILE=outfile
(

a=0

while read line
do 

./runnable input
echo "This is line number: $a"
a='expr $a+ 1'

done<final_file

) >$OUTFILE

To clarify the finalfile looks like

_ DATA _

2,9,2,9,10,0,38

2,9,2,10,11,0,0

2,9,2,11,12,0,0

2,9,2,12,13,0,0

2,9,2,13,0,1,4

2,9,2,13,3,2,2

and so on. One line ,at a time, is the standard input. Number of lines in finalfile correspond to number of times the standard input is given. So in the above case script should run 6 times as there are six lines.

A: 
  • Run your shell script with the -x option for some debugging output.
  • Add echo $line after your while read line; do
  • Note that while read line; do echo $line; done does not read space separated input, it reads line separated input.
Zan Lynx
@Zan: Doing that confirms the fact that the script is reading only the first line of 'finalfile' and not rest. But, I want to know how to resolve this issue or what is going on thats making it do the same.
shubster
+3  A: 

I'll hazard that ./runnable seeks all the way through stdin. With no input left to read, the while loop ends after one iteration.

Reasoning: your example Works For Me (TM), substituting a file I happen to have (/etc/services) for final_file and commenting out the line that invokes ./runnable.

On the other hand, if I replace the ./runnable invocation with a one-liner that simply seeks and discards standard input (e.g., cat - > /dev/null or perl -ne 1), I get the behavior you describe.

(Note that you want backticks or $() around the call to expr.)

pilcrow