Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 15 of 15
Like Tree1Likes
I added "#!/bin/bash" and it doesn't help I think the subtlety lies in how "while read line" works maybe there is associated buffer which may affected by the script?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    May 2012
    Posts
    85

    I added "#!/bin/bash" and it doesn't help
    I think the subtlety lies in how "while read line" works
    maybe there is associated buffer which may affected by the script?

  2. #12
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    yes, it seems normal to me too.

    I don't know.

  3. #13
    Just Joined!
    Join Date
    May 2012
    Posts
    85
    it is very strange
    I insert null lines between every two lines in the input_file
    and I notice that
    for the ”while read line“
    the reading sequnce is
    the first line
    the third line
    the second line
    the fourth line


    what's wrong, totally in a mess!!!

  4. #14
    Just Joined!
    Join Date
    May 2012
    Posts
    85
    I modified the script a bit
    Code:
        while read line 
        do  
          echo "read line is $line" 
          line=`echo ${line}|awk '{print $1}'`  
    
          index=$((index+1)) 
          
          if((index%2==0));then 
            echo "before continue line is $line"  
            continue 
          fi   
    
          echo "index is $index and now modify html page with $line" 
          ...
          ...
          done< infile
    the content of infile is:
    Code:
        1OrMcP2CdV4 180 
         
        1Wp33RG2XaA 180 
          
        21zUUJ04ovI 180 
          
        2pIqUhaDMLg 180 
          
        2WRU4NUJSVc 180 
        ...
        ...
    in the infile, there are alternate null lines, which are diliberately added by myself to deal with the even lines reading problem

    I run the script and the output is:
    Code:
        read line is 1OrMcP2CdV4 180
        index is 1 and now modify html page with 1OrMcP2CdV4
      
    
        read line is 1Wp33RG2XaA 180
        before continue line is 1Wp33RG2XaA
    
    
        read line is 
        index is 3 and now modify html page with 
     
    
        read line is 21zUUJ04ovI 180
        before continue line is 21zUUJ04ovI
    
        read line is 
        index is 5 and now modify html page with
    then I modified the null line, like
    Code:
        1OrMcP2CdV4 180 
        Axxx
        1Wp33RG2XaA 180 
        Axxx
        21zUUJ04ovI 180 
        Axxx 
        2pIqUhaDMLg 180 
        Axxx  
        2WRU4NUJSVc 180 
        ...
        ...
    the output is interesting:

    Code:
        read line is 1OrMcP2CdV4 180
        index is 1 and now modify html page with 1OrMcP2CdV4
        
        read line is xxx
        before continue line is xxx
    
    
        read line is 1Wp33RG2XaA 180
        index is 3 and now modify html page with 1Wp33RG2XaA
    
        read line is xxx
        before continue line is xxx
    
        read line is 21zUUJ04ovI
        index is 5 and now modify html page with 21zUUJ04ovI 
     
    
        read line is 21zUUJ04ovI 180
        before continue line is 21zUUJ04ovI
    OK, it reads in order now, but the even lines "Axxx" are read as "xxx"
    but after around 20 loops, the order starts to be in a mess again!!!
    interesting and weird, isnt it?



    this problem is really weird and I'm really at a loss on how to deal with it!
    Last edited by esolve; 11-03-2012 at 12:03 AM.

  5. #15
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    149
    I've had trouble with shell "read" command loops not getting all the data I thought they should. I've never seen pieces of a line get lost, so I don't know exactly what you might be dealing with. But I've seen loops terminate before all the source data was read and I generally avoid constructs like "while read whatever; do commands...; done <input" as a result. I've had better luck with piping in the data with things like "cat input | while read whatever; do commands...; done" even though it seems like the same thing. Maybe because there's an external process on the other side of the pipeline it's treated differently? No idea, but it's been more reliable for me.

    But in your case there might be an even simpler solution, provided I understand the script. It looks like you just want the first word in the line from a quick read. So something like this should work and avoid reading from a file at the end of the loop completely.

    Code:
    url_list="$(awk '{ print $1; }' input)"
    
    for line in $url_list
    do
    ....
    done
    That way if something in the loop is screwing up the data streams it won't matter since the loop isn't doing any I/O to set the next url.
    Last edited by cnamejj; 11-03-2012 at 12:16 AM. Reason: typo in command, grammar problems, general user stupidity...
    watael likes this.

Page 2 of 2 FirstFirst 1 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •