LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-23-2004, 07:40 AM   #1
tam3c36
LQ Newbie
 
Registered: Oct 2004
Posts: 1

Rep: Reputation: 0
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
 
Old 10-23-2004, 12:24 PM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 10-23-2004, 10:43 PM   #3
Ghost_runner
Member
 
Registered: Jun 2004
Location: Kansas City, MO
Distribution: openSUSE 10.2/64bit
Posts: 276

Rep: Reputation: 30
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?
 
Old 10-24-2004, 02:50 PM   #4
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
I think this will work:

cat par.file |
while read a; do
set `a.out $a`
v1=$1
v2=$2
v3=$3
done
 
Old 10-18-2009, 01:22 AM   #5
fortran01
LQ Newbie
 
Registered: Mar 2006
Distribution: Ubuntu
Posts: 8

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

Quote:
Originally Posted by /bin/bash View Post
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.
 
Old 10-18-2009, 05:06 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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
 
Old 10-18-2009, 05:44 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by /bin/bash View Post
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?
 
Old 10-18-2009, 05:48 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by mfeat View Post
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.
 
Old 10-18-2009, 07:44 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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)

Last edited by bigearsbilly; 10-18-2009 at 07:45 AM.
 
Old 10-18-2009, 11:05 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by bigearsbilly View Post
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]
 
Old 12-07-2010, 01:42 PM   #11
stdale
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 1
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?
 
1 members found this post helpful.
  


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
bash read a given line number from text bendeco13 Programming 7 08-31-2012 03:49 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 10:02 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration