Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unexpected vi behavior

Status
Not open for further replies.

michaelvv

MIS
Jan 17, 2003
70
US
In AIX, after deleting all the lines from a file and saving, I and up with a one byte file containing a new line character. I expected to end up with a zero byte file, the same as in Linux or Solaris. This behavior is causing ‘test –s’ not to produce the desired results.
 
If you want a zero byte file, "cp /dev/null filename" is the way to go.

vi is, at its heart, a line editor. Its concept of an empty file is one with a single, empty line. Even if you create a zero byte file by other means, if you open it in vi and immediately write it without making any edits, you'll see that it now has a newline in it.

If you really need to do it with an editor, install vim. Vim will allow you to truly delete everything in the file, including that last newline.

- Rod


IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

A Simple Code for Posting on the Web
 
Thanks Rod, but I'm not sure I totally agree. Of all the UNIX flavors that I have worked with (System V, SCO, Solaris, Linux and AIX), only AIX behaves this way. I am porting an application from Solaris and fortunately it is not a major inconvenience to add code to truncate the files where need be. Installing vim is not an option, these are production servers and I do not have 'root' access. Even if I did, obtaining all the approvals would be a major headache.
 
Thanks for the correction, michaelvv.

I'll bet my first encounter with vi and empty files was on AIX and I've avoided it since.

Here's an even odder behaviour: ex, which according to "ls -il" is the exact same executable as vi, doesn't append the newline to an empty file and also completely empties a file with ":1,$d".

- Rod
 
Rod,

That is absolutely bizzare! I had never used ex by itself, only from within vi. Not that I didn't trust you, but I had to see it for myself, and it is exactly like you say it is! Just for the heck of it, I also tried view, edit and vedit. edit also saved an empty (zero byte) file.

It is not exactly clear, but do you work for IBM? Are you in a position to suggest that they 'fix' it?

Thanks,
Mike
 
Mike,

Nope, I don't work for IBM.

Since vi has behaved that way since at least AIX 3.2.5, I'm guessing they're stuck keeping it "bugwards" compatible at this point.

- Rod
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top