unix - Awk can't re-create a file it deleted? -
echo bla | awk 'begin{fname="foo.txt"} {print $0 >>fname; print "rm -f " fname | "/usr/bin/ksh"; close("/usr/bin/ksh"); print $0 >>fname}' after command has executed, should end file "foo.txt", right ?
it doesn't work. tried system("/usr/bin/rm -f " fname) remove file, bash instead of ksh, linux, hp-ux, cygwin, fflush(""),... doesn't work ! seems after file deleted, awk can't write file same name anymore.
looks bug in awk, or missing (big time !) ?!
awk keeping file open. try closing it:
echo bla | awk 'begin{fname="foo.txt"} { print $0 >>fname print "rm -f " fname | "/usr/bin/ksh" close("/usr/bin/ksh") close( fname ) print $0 >>fname }' awk opens file first time write it, , keeps file open. when link removed filesystem, awk still has file open doesn't use "foo.txt" name access it. closing file, force awk @ filesystem again , create non-extant link "foo.txt".
for benefit of readers not understand distinction between link , file, try following:
$ rm -rf /tmp/foo; mkdir /tmp/foo; cd /tmp/foo # start clean directory $ touch foo.txt; ln foo.txt bar.txt # create file 2 links $ # run original awk script (without closing file) $ cat bar.txt you see line "bla" twice in bar.txt. reason bar.txt , foo.txt both links same file. awk opens file , writes line it, deletes link foo.txt, writes line file. when awk terminates, link foo.txt has been deleted, file still there , accessible through link bar.txt. if bar.txt deleted, filesystem notice link count file has dropped 0 , file deleted.
Comments
Post a Comment