You are not logged in.

#1 2012-04-17 20:12:39

Mine
Member
Registered: 2011-10-07
Posts: 20

Bash, awk and file descriptors

Hi guys,
I'm thinking about this piece of command lines:

[user@host ~]$ echo "first word" > test
[user@host ~]$ cat test
first word
[user@host ~]$ { rm ./test && awk -F' ' '{ print $2,$1 }' > ./test; } < ./test
[user@host ~]$ cat test
word first
[user@host ~]$ { awk -F' ' '{ print $2,$1 }' > ./test; } < ./test
[user@host ~]$ cat test
[user@host ~]$ ls -l test
-rw-r--r-- 1 user user 0 17 apr 22.06 test

I wonder why:

{ rm ./test && awk -F' ' '{ print $2,$1 }' > ./test; } < ./test

works as I expect (the file "test" have words in reverse order), while instead:

{ awk -F' ' '{ print $2,$1 }' > ./test; } < ./test

give a file "test" empty, with size 0.
Why and how the leding "rm" make the difference?

Thanks smile


Mine
Only those who have the courage to take a penalty miss them. (R. Baggio)

Offline

#2 2012-04-17 20:56:42

zorro
Member
Registered: 2011-11-18
Posts: 47

Re: Bash, awk and file descriptors

The first example results in the inode of ./test changing, whilst the second maintains the same inode. Try ls -i

Although the file is deleted with rm, the redirect < still has a handle to the original inode and continues to read from it.

Without the rm, the redirect > overwrites the data at that inode, hence no data to process.

Last edited by zorro (2012-04-17 20:57:42)

Offline

#3 2012-04-17 21:52:15

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Bash, awk and file descriptors

zorro wrote:

The first example results in the inode of ./test changing, whilst the second maintains the same inode. Try ls -i

Although the file is deleted with rm, the redirect < still has a handle to the original inode and continues to read from it.

Without the rm, the redirect > overwrites the data at that inode, hence no data to process.

Thanks for this explanation. OP made me curious.

Offline

#4 2012-04-18 17:59:48

Mine
Member
Registered: 2011-10-07
Posts: 20

Re: Bash, awk and file descriptors

Thank you zorro, now it is more clear!
About this I find a useful link: http://backreference.org/2011/01/29/in- … -of-files/

/dev/zero wrote:

OP made me curious.

/dev/zero, what do you mean with "OP"?


Mine
Only those who have the courage to take a penalty miss them. (R. Baggio)

Offline

#5 2012-04-18 19:00:16

bohoomil
Member
Registered: 2010-09-04
Posts: 2,376
Website

Re: Bash, awk and file descriptors

OP


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#6 2012-04-19 09:37:57

Mine
Member
Registered: 2011-10-07
Posts: 20

Re: Bash, awk and file descriptors

bohoomil wrote:

AHAHAH... you're very amusing.
Op has many meanings, what of the thoundas I find on internet is the meaning /dev/zero was thinking?
OP=OPerating system
OP=Original Poster
OP=Operator Access
(and so on...)

Less sarcasm (offsite) and more pragmatism, please.


Mine
Only those who have the courage to take a penalty miss them. (R. Baggio)

Offline

#7 2012-04-19 14:05:40

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Bash, awk and file descriptors

Mine wrote:
bohoomil wrote:

AHAHAH... you're very amusing.
Op has many meanings, what of the thoundas I find on internet is the meaning /dev/zero was thinking?
OP=OPerating system
OP=Original Poster
OP=Operator Access
(and so on...)

Less sarcasm (offsite) and more pragmatism, please.

http://netforbeginners.about.com/od/int … -Is-OP.htm

Happy ? tongue

Offline

#8 2012-04-19 17:39:09

Mine
Member
Registered: 2011-10-07
Posts: 20

Re: Bash, awk and file descriptors

Yes, first for explanation and second for /dev/zero comment, which I can now understand.
I'm not a forums "habitue", so the question smile


Mine
Only those who have the courage to take a penalty miss them. (R. Baggio)

Offline

Board footer

Powered by FluxBB