Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Posts
    1

    Unanswered: Error with a variable and a loop

    I'm writing a basic shell script to just read a file from the bottom line up. It will read each line, line by line then it will execute the line from the file. I currently am just trying to have it print out each line and I am getting an error running my program.

    I am getting an error on line 5 which is my while loop like. My count value is currently 306 and I get an error saying: ./rebuildall.sh[5]: 306: not found.

    Any ideas. My code is below. I know I am missing something simple.

    Thank you

    Code:
    #!/bin/sh                               
    #echo $1                                
    count=`wc -l test.out | cut -c1-8`      
    count=`expr $count + 0`                 
    while ($count >= 0)                     
    do                                      
      #echo $file_length                    
      line=`head -$count test.out | tail -1`
      echo $line                            
      count = `expr $count - 1`             
    done

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    I think it should be like this:

    Code:
    while [ "$count" -ge "0" ] ;
    Also, don't leave spaces between "count", "=", and "`expr..."

  3. #3
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    also..... a bit different approach - reversing the file 'in-place':

    sed -n '1!G;h;$p' test.out
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by vgersh99
    sed -n '1!G;h;$p' test.out
    Nice trick! May end up with the stack overflow with a large file though.

Posting Permissions

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