From owner-freebsd-questions@FreeBSD.ORG Tue Sep 14 02:01:35 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 234C916A4CE for ; Tue, 14 Sep 2004 02:01:35 +0000 (GMT) Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.192]) by mx1.FreeBSD.org (Postfix) with ESMTP id AE14343D39 for ; Tue, 14 Sep 2004 02:01:34 +0000 (GMT) (envelope-from jez.hancock@gmail.com) Received: by mproxy.gmail.com with SMTP id 77so655359rnl for ; Mon, 13 Sep 2004 19:01:33 -0700 (PDT) Received: by 10.38.73.3 with SMTP id v3mr1814328rna; Mon, 13 Sep 2004 19:01:33 -0700 (PDT) Received: by 10.38.78.44 with HTTP; Mon, 13 Sep 2004 19:01:33 -0700 (PDT) Message-ID: <7b3c7f0b04091319012ccbd6b1@mail.gmail.com> Date: Tue, 14 Sep 2004 03:01:33 +0100 From: Jez Hancock To: Ryan Sommers In-Reply-To: <50164.208.4.77.15.1095096715.squirrel@www2.neuroflux.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <50164.208.4.77.15.1095096715.squirrel@www2.neuroflux.com> cc: freebsd-questions@freebsd.org Subject: Re: Mail from a shell script? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Jez Hancock List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Sep 2004 02:01:35 -0000 On Mon, 13 Sep 2004 11:31:55 -0600 (MDT), Ryan Sommers wrote: > > First problem I ran into. I'm attempting to send the mail via `which > mail`. I first was going to attempt to concatenate the message via > "message=${message}$'\n'" However, this strips the newlines > out of the variable (both sh and bash). I then tried using the < redirection in bash/sh and same thing happened. > I believe this happens because the default input field separator (the environment variable $IFS) is set to space by default and so newlines are just 'squashed'. Effectively all newlines are stripped out. As a test you can try this: -snip- #!/bin/sh message="Hi! This is a test. Another line. Last line." # OFS="$IFS" # IFS="" echo "$message" # IFS="$OFS" -snip- If you run the script once with the commented lines, you should see the same problem you've encountered already. Newlines are replaced with spaces. However if you then remove the comments so that the $IFS variable is unset before echoing the message (first saving the original value to $OFS) and run it, the script should display the lines as intended, newlines intact. Finally the last commented line resets $IFS to it's original value - script execution can be messed up later if you don't do this. > My next thought was to open an fd through which to pipe output since the > shells support it. However, it seems they only support opening a file for > read/write, not a pipe. > > So, my ultimate question is, is there any way to send an email from a > shell script without creating a "wrapper script" that pipes the output of > one script into the mail program. Ie script1 contains only "script2 | mail > ". You can always try this as well to find scripts that contain the kind of code you want to emulate: file /usr/local/bin/* | grep Bourne | cut -f1 -d: | xargs grep mail which gives you a list of bourne shell scripts residing in /usr/local/bin that contain reference to the string 'mail' - those scripts that might contain mailer code. Running this now I remember checking out the 'flea' script before for an example before - another is the freebsd problem report script - send-pr. I think those scripts cat the message content out to a temporary file first and then pipe that file back to the sendmail command - or some variation on that theme. -- Jez Hancock - System Administrator / PHP Developer http://munk.nu/ http://jez.hancock-family.com/ - Another FreeBSD Diary http://ipfwstats.sf.net/ - ipfw peruser traffic logging