LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: read every line in the files and use the line as parameters as another program (https://www.linuxquestions.org/questions/programming-9/bash-read-every-line-in-the-files-and-use-the-line-as-parameters-as-another-program-246291/)

tam3c36 10-23-2004 07:40 AM

BASH: read every line in the files and use the line as parameters as another program
 
I am a file name par.file

200 200 0 0 900
199 200 0 0 900
100 200 0 0 900

I want to use a bash to read it line by line and then use that line to call a program, a.out.

a.out 200 200 0 0 900
a.out 199 200 0 0 900
a.out 100 200 0 0 900

and then save the results of each calling of a.out into three variable

what can my bash should like?

I have tried other example in the web, but still have problems
thx

/bin/bash 10-23-2004 12:24 PM

Welcome to LinuxQuestions.

Well there are probably many other better ways to do this but here's my version.

#!/bin/bash
exec 3< data
while read <&3
do echo "The number is $REPLY"
a.out "$REPLY"
done
exec 3>&-

Now what it does is to open the file data and reads it line by line, sends that to a.out and finally closes the file. NOTE: the default variable for read is REPLY.

HTH.

Ghost_runner 10-23-2004 10:43 PM

could add a line EOF at the end of his file, then do while !EOF to make it so as many lines as he had would not matter?

mfeat 10-24-2004 02:50 PM

I think this will work:

cat par.file |
while read a; do
set `a.out $a`
v1=$1
v2=$2
v3=$3
done

fortran01 10-18-2009 01:22 AM

This is helpful especially when the statements inside the loop redirect something to stdin fooling `read`.

Quote:

Originally Posted by /bin/bash (Post 1251740)
Welcome to LinuxQuestions.

Well there are probably many other better ways to do this but here's my version.

#!/bin/bash
exec 3< data
while read <&3
do echo "The number is $REPLY"
a.out "$REPLY"
done
exec 3>&-

Now what it does is to open the file data and reads it line by line, sends that to a.out and finally closes the file. NOTE: the default variable for read is REPLY.

HTH.


bigearsbilly 10-18-2009 05:06 AM

try this:

Code:

cat par.file | while  read $line;do

  set `a.out $line`
  echo "1=$1 2=$2 3=$3@
done

remember set will clobber your original command line parameters.

also if your file is called code.c
make code will create code rather than a.out

catkin 10-18-2009 05:44 AM

Quote:

Originally Posted by /bin/bash (Post 1251740)
Code:

#!/bin/bash
exec 3< data
while read <&3
do echo "The number is $REPLY"
a.out "$REPLY"
done
exec 3>&-


That looks good.

The effect of double quotes in a.out "$REPLY" is to pass the entire line from file data as a single argument to a.out. If that is not what is required, remove the double quotes.

Regards "and then save the results of each calling of a.out into three variable", where do they appear and what is their format? Are they three whitespace-separated words on stdout? Anything on stderr? Exit status?

catkin 10-18-2009 05:48 AM

Quote:

Originally Posted by mfeat (Post 1253920)
cat par.file |
while read a; do
set `a.out $a`
v1=$1
v2=$2
v3=$3
done

The variables v1 to v3 will not be set outside the loop because the "cat | while" pipeline results in bash running the while in a sub-shell. When the sub-shell terminates, variables set within it are lost.

bigearsbilly 10-18-2009 07:44 AM

not true catkin,
observe...

Code:

billy@foghorn:~$ while read x;do set `date`; done

billy@foghorn:~$ echo $5 $4 $3 $2 $1
BST 13:43:46 18 Oct Sun

it's only read variables this is true for.
A big failing in bash IMHO (I prefer korn)

catkin 10-18-2009 11:05 AM

Quote:

Originally Posted by bigearsbilly (Post 3723709)
not true catkin,
observe...

Code:

billy@foghorn:~$ while read x;do set `date`; done

billy@foghorn:~$ echo $5 $4 $3 $2 $1
BST 13:43:46 18 Oct Sun

it's only read variables this is true for.
A big failing in bash IMHO (I prefer korn)

It is only when the while is in a pipeline and not the first command in the pipeline that the problem arises:
Code:

c:~$ echo '1
> 2
> 3' | while read x;do set `date`; done
c:~$ echo $5 $4 $3 $2 $1
[no output]


stdale 12-07-2010 01:42 PM

I have tried this and it works, but as soon as I try to call a function from inside my reading loop it only every reads the first line.
The file it is reading in is formated like:
touch /tmp/t1
touch /tmp/t2
touch /tmp/t3

Here is the code that I have
Code:

run_cmd_file() {
 client=$1
  while read line
  do
  echo "+ $line"
  chr=${line:0:1}
  case $chr in
    "#" )  # Currently we ignore commented lines
        ;;
    *  )
        run_cmd $client $line
        ;;
  esac
  done < $2
}

when I run this, it will read in the first line of the file "touch /tmp/t1" but doesn't read in the rest. But if i do this code:

Code:

run_cmd_file() {
 client=$1
  while read line
  do
  echo "+ $line"
  chr=${line:0:1}
  case $chr in
    "#" )  # Currently we ignore commented lines
        ;;
    *  )
        echo "client[$client] line[$line]"
        ;;
  esac
  done < $2
}

it runs fine. I checked my other functions and none of them are calling exit or anything and when I run it with bash -x, bash shows me that the second time it hits the while loop (aka after handling first line and reading in second) it exits. Anyone have any idea while it would be acting this way?


All times are GMT -5. The time now is 04:39 PM.