0

I have a file containing a list of process is input1 file

proc1
proc2 b
proc3 a

I use the following command to read the file and pass on the content of the file to a variable and then do something

for in in cat `input1`;do 
echo $i
done

It works fine for entries like proc1. But the problem is that it assigns only the proc2 to the variable $i and the also assign b to to $i etc etc .. How can I pass the entire proc2 b to variable $i with the space in between ????

Thanks

theuniverseisflat
  • 861
  • 2
  • 12
  • 19

1 Answers1

0
$ while IFS= read i; do echo $i; done <input1
proc1
proc2 b
proc3 a


$ IFS=$'\n'; for i in $(cat input1); do echo $i; done
proc1
proc2 b
proc3 a

Update: From the comments, it appears that you want to pass the name of a file from an outer loop to an inner loop which will read from the file. In that case:

for fname in input1 input2
do
    while IFS= read i
    do 
        echo $i
    done <"$fname"
done

Or, if the file names are being read from a file called filelist:

while IFS= read fname
do 
    while IFS= read i
    do
        echo 1=$i
    done <"$fname"
done <filelist
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Hi John .. I'm actually reading two files.. So I have a for I loop withing a for I loop .. so I'm passing the name of the file from one for lopp to the next .. Not sure how I can use this command in my script – theuniverseisflat Apr 11 '14 at 21:42
  • also I have the whole thing bundled in a sub shell in ( – theuniverseisflat Apr 11 '14 at 21:43
  • in short I cannot use the – theuniverseisflat Apr 11 '14 at 21:46
  • @theuniverseisflat See the update. Also, to keep your question alive, you should update the question to talk about the loop-within-loop so that it is clear to whoever that your question is different from the claimed duplicate. – John1024 Apr 11 '14 at 21:50
  • @theuniverseisflat Also, clarify where the subshell is being used. – John1024 Apr 11 '14 at 21:52
  • I did try the second command u put and it works ... but not in my script - outside of the script it works fine. I'm passing the output as a variable and do a ssh and it gets all messed up thanks – theuniverseisflat Apr 11 '14 at 21:56
  • @theuniverseisflat You need to update the question with the actual details of what you are doing rather than revealing it piece by piece. – John1024 Apr 11 '14 at 22:00