Find the answer to your Linux question:
Results 1 to 3 of 3
his, this is some quirk or something. i have this line in a bash script: Code: cat $sortfile | sort -nr > $sortfile $sortfile is some text file with lines ...
  1. #1
    Just Joined! clickalot's Avatar
    Join Date
    Nov 2009
    Location
    Erlangen, Germany
    Posts
    37

    Question [SOLVED] sometimes works, sometimes doesn't !?

    his,

    this is some quirk or something.

    i have this line in a bash script:
    Code:
    cat $sortfile | sort -nr > $sortfile
    $sortfile is some text file with lines like:number.some_text ...

    i don't understand why this works erratically...

    i changed to:
    Code:
    cat $sortfile | sort -nr > $sortfile-2
    and now it seems ok

    what should i be learning here?...

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by clickalot View Post
    what should i be learning here?...
    That you mess up the file by writing to it while reading from it this way.

    Sort doesn't write to the new file before it received EOF from the input. So that's not the problem. But nevertheless the file is created by the shellt right when the command starts.

    Try it out. Open two terminals and take a random text file "test.txt" with some content in it. Now type "cat | sort -n > test.txt" in the second terminal. The command waits for input from the keyboard until you press CTRL+D.
    But the file "test.txt" is erased already so as to make it ready to receive the data yet to come, as you can check on terminal 1. So sort would get input from a file already empty.

    "sort -onr $file $file" should do the job.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined! clickalot's Avatar
    Join Date
    Nov 2009
    Location
    Erlangen, Germany
    Posts
    37
    Thanks GNU-Fan!

    now its clearer.

Posting Permissions

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