LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2008, 04:48 AM   #1
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
Bash attributes


hello all,

I was playing around with the following commands:
Code:
sort, >, >>
Firstly I created a test.txt file and I wrote the days in order. One day per line. Then I opened the terminal and I did:
Code:
$ sort test.txt
As it was expected I got:
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday
Then I did:
Code:
$ sort test.txt >> test.txt
Again as it was expected I got into the test.txt the following content:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday
Then I decided to try and overwrite the contend with only the sorted list:
Code:
$ sort test.txt > test.txt
Unexpectedly I got an empty file :p
I know I could transfer the sorted list to a new file and then overwrite the content of the test.txt file with the content of the new file.. And finally delete the new file.

But my question is why bash couldn't overwrite directly the content of the test.txt and as a result I got an empty file?

Thanks in advance
 
Old 05-14-2008, 04:54 AM   #2
technosapien
LQ Newbie
 
Registered: May 2008
Posts: 9

Rep: Reputation: 0
Quote:
Originally Posted by Cyhaxor View Post
hello all,

(snip)

Then I decided to try and overwrite the contend with only the sorted list:
Code:
$ sort test.txt > test.txt
Unexpectedly I got an empty file :p
I know I could transfer the sorted list to a new file and then overwrite the content of the test.txt file with the content of the new file.. And finally delete the new file.

But my question is why bash couldn't overwrite directly the content of the test.txt and as a result I got an empty file?

Thanks in advance

I'm not sure WHY you got the results you did, but try this:

Code:
$ sort -o text.txt test.txt
and see if that works for you. It tells sort to sort the file in-place.

- A
 
Old 05-14-2008, 05:01 AM   #3
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
worked like a charm But I still wondering why my method fail. Any technical explanation would be appreciated! Thanks again
 
Old 05-14-2008, 06:01 AM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

When you run 'sort test.txt > test.txt' , the first thing that happens is that test.txt is opened in preparation for writing. Since it's a single '>' redirection, the existing file is unlinked (rm) before this happens, so the original test.txt ceases to exist (this is called clobbering).

So in effect, you're attempting to sort an empty file.


Dave
 
Old 05-14-2008, 06:26 AM   #5
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ilikejam View Post
Hi.

When you run 'sort test.txt > test.txt' , the first thing that happens is that test.txt is opened in preparation for writing. Since it's a single '>' redirection, the existing file is unlinked (rm) before this happens, so the original test.txt ceases to exist (this is called clobbering).

So in effect, you're attempting to sort an empty file.


Dave
Wow I thought that with redirection (both > & >>) the output of a command is taken (located temporary in memory) and then passed to the file. So I expected the content to be taken and then overwrite the previous..

Thanks for the info Dave!
 
Old 05-14-2008, 07:47 AM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Quote:
Originally Posted by Cyhaxor View Post
Wow I thought that with redirection (both > & >>) the output of a command is taken (located temporary in memory) and then passed to the file. So I expected the content to be taken and then overwrite the previous..

Thanks for the info Dave!
Cool. In a command pipe (and redirection I would imagine), there's only a few kB buffer in memory (looks like 4kB in 2.6 kernels), so command output effectively 'streams' between inputs, outputs, and files.

Dave

Last edited by ilikejam; 05-14-2008 at 07:52 AM. Reason: 4kB, not 8
 
  


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
What are Attributes in Clustering paragkalra Linux - Software 0 10-01-2007 01:51 PM
Attributes - color satimis Linux - General 2 03-22-2005 10:39 PM
thread attributes iftiuk Programming 2 07-29-2004 01:41 PM
Name the attributes alaios General 5 10-29-2003 06:50 PM
file attributes binky22 Linux - Newbie 2 09-26-2003 04:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:21 PM.

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