control-D adventures (was: Why 'man mail' didn't help me much

L. Adrian Griffis adrian at nerds.org
Tue Apr 9 00:29:09 CDT 2002


On Mon, 8 Apr 2002, Adam Davis wrote:
> A week (?) ago I asked for help.  I was attempting to send mail w/
> attachments from a bash script to be run by crond, in order to email a
> user .tar.gz files files so that, in the event of a crash, we had copies
> of the files other than the local backups.  I got a lot of 'man mail' as
> response.  The problem with 'mail' is that it didn't deal directly with
> attachments, but I got around that.  The really big problem with 'mail'
> is that it wanted me to Ctrl-D my way out, which I was having problems
> doing from a script that wasn't supposed to be interactive.
> 
> I simplified the more difficult part and borrowed a pre-written script
> that would take file-names as arguments and automatically run all the
> requested encoding.  (You can find it at
> http://home.clara.net/dwotton/unix/mail_files.txt)
> 
> However, I was still getting trapped in a mailer that wanted me to
> Ctrl-D out.
> 
> I learned a neat little trick this weekend - echo $'x04'  ... It took
> awhile to find a file that specifically mentioned that you can, in fact,
> force in such things as Ctrl-D (the ASCII EOT character, 0x04
> hexadecimal) without being around to type Ctrl-D.  Almost every file I
> came across had ^D, and then said you had to type that in yourself...
> That's not true.
> 
> It is incredibly easy to do, once you figure out how.  Not mentioned
> anywhere, though, that I could find - emailing attachments without
> interaction.  It's certainly not mentioned in 'man mail'...

This all seems very counter-intuitive, to me.  Are you sure there's not
some other explaination for what you are seeing?  I've used the "mail"
command on a variety of different systems, and for me, it has always
been sufficient to pipe what I want in the message into the mail
command and let the normal end of file on the pipe tell mail that the
message is finished.  I've never, in 15 years of using Unix in its
various flavors, had to send the character for control-D through a
pipe to get any normal application (and mail qualifies) to recognize
the end of data.  Is there something else complicating this problem?
Is there something special about the version of "mail" you are using?

Also, you might note that when you key in control-D at a terminal,
the typical application does not generally see it.  The tty device
driver traps this character and singles an end of file (with a zero
length on the next read) to the application reading from the tty.
Pipes don't use the tty driver, so there would be nothing to covert
the control-D character to an EOF indication.  Unless "mail" puts
the tty in raw mode, it doesn't make sense for "mail" to expect to
receive control-D.  And if it attempts to put its standard input
into raw mode, and if that standard input is a pipe, the ioctl() call
for changing the mode will fail, so it seems like mail would know its
not on a tty, and still shouldn't expect control-D.

This all seems very strange to me.

Adrian




More information about the Kclug mailing list