[comp.mail.mush] Problem setting headers to output from shell commands

dylan@ibmpcug.co.uk (Matthew Farwell) (09/21/90)

Ok. This may seem a silly question, but I have RTFM'd, and I can't see
an easy + simplistic answer.

I want to include the output from a shell command in a header.
Say I wanted to produce a header like

X-My-Date: Fri Sep 21 09:53:09 BST 1990
where
this would be the output from something approaching
my_hdr X-My-Date: `sh date`

How would I go about it?
What I have tried is using a shell function as a workaround, in the hope
of someone providing me a better solution.

mush()
{
	tmpfile=/tmp/mu.$$
	echo my_hdr X-My-Date: `date` > $tmpfile
	/usr/local/bin/mush -C -F $tmpfile $*
	rm $tmpfile
}

Even this doesn't work properly. It adds the extra header as long as you
enter the shell before you start sending mail. I've tried things like
testing is_sending, but that doesn't seem to work either.

Any ideas? I'm running Mush 7.1.2 under Sco Xenix 2.3.1. Here is my .mushrc
-----
if hdrs_only
	exit
endif
set ask
set autoedit
set autoinclude
set autombox
set autosign
set complete
set dot
set edit_hdrs
set folder=~/mail
set history=30
set hostname=ibmpcug.co.uk
set in_reply_to='%i from %n'
set indent_str=>
set mbox=~/mail/mbox
set pager=internal
set pre_indent_str='In message dated %W %N %M, %f writes:'
set realname='Matthew Farwell'
set record=+outgoing
my_hdr X-Bogus-Mail-Header: Strawberry shortcake
my_hdr X-Coffee-Cup: Sun Microsystems
# some aliases here...
bind '=' macro '(~/mail/savelists\n'
------

Dylan.

p.s. `date` is only used as an example. I want a generic way of doing things.
-- 
Matthew J Farwell                 | Email: dylan@ibmpcug.co.uk
The IBM PC User Group, PO Box 360,|        ...!uunet!ukc!ibmpcug!dylan
Harrow HA1 4LQ England            | CONNECT - Usenet Access in the UK!!
Phone: +44 81-863-1191            | Sun? Don't they make coffee machines?

schaefer@ogicse.ogi.edu (Barton E. Schaefer) (09/22/90)

In article <1990Sep21.091000.11589@ibmpcug.co.uk> dylan@ibmpcug.co.uk (Matthew Farwell) writes:
} Ok. This may seem a silly question, but I have RTFM'd, and I can't see
} an easy + simplistic answer.
}
} I want to include the output from a shell command in a header.

That's because mush doesn't support a text-substitution `command` mechanism.
Backquoted commands substitute message lists, just as "pipes" in mush pass
message lists rather than text.

} Say I wanted to produce a header like
} 
} my_hdr X-My-Date: `sh date`
} 
} How would I go about it?

That one is fairly easy, actually:

cmd sh_hdr "sh 'echo my_hdr \!:1 ` \!:2-$ ` > $tmpdir/h$$'; source $tmpdir/h$$"

this of course assumes you have set tmpdir somewhere, use /tmp if not.
You can fool with the quoting to get $tmpdir expanded at execute time
rather than at cmd-definition time if you want.  Usage example:

sh_hdr X-My-Date: date

The major drawback of this is that you can't use it in a .mushrc file
(because the \! positional references are not recognized when sourcing
a file, to avoid complaints about alias addresses having ! characters).
In the .mushrc, you'll have to perform the expansion yourself, e.g.

# Generate an extra date header
if is_sending
    sh "echo my_hdr X-My-Date: `date` > $tmpdir/h$$"; source $tmpdir/h$$
endif

Rich Burridge, are you reading this?  Why didn't I think of this when
you were trying to integrate mush with faces?  Oh, well ....

} p.s. `date` is only used as an example. I want a generic way of doing things.

The above is as close to generic as it gets at the moment.  I posted a
mush script called "bq" (for backquote) a while ago which does this in
a slightly more generalized manner, but I don't have a copy handy.  You
could also try:

cmd bq "sh 'echo \!* > $tmpdir/bq$$'; source $tmpdir/bq$$"

which would be used like

bq my_hdr X-My-Date: `date`
bq set hostname=`hostname`

However, that would only work for very simple commands because of quoting
problems.  For example,

bq set date=`date`

would not work; using my current date as an example, it would set date
to "Fri", and create variables named "Sep", "21", "10:35:26", "PDT", and
"1990", which is clearly not what you want. ;-}
-- 
Bart Schaefer						schaefer@cse.ogi.edu

rock@warp.Eng.Sun.COM (Bill Petro) (09/22/90)

schaefer@ogicse.ogi.edu (Barton E. Schaefer) writes:

>In article <1990Sep21.091000.11589@ibmpcug.co.uk> dylan@ibmpcug.co.uk (Matthew Farwell) writes:

>The above is as close to generic as it gets at the moment.  I posted a
>mush script called "bq" (for backquote) a while ago which does this in
>a slightly more generalized manner, but I don't have a copy handy.  You
>could also try:

Here it is:


#!/bin/sh
# bq for mush
#
if [ $# -lt 2 ]
then
    echo bq: too few arguments
    echo 'usage: bq variable command [args ...]'
fi 1>&2
out=$HOME/.mushbq
var=$1
shift
val=`eval "$@" | tr "\012'" ' "'`
echo set $var = "'$val'" > $out



--
     Bill Petro  {decwrl,hplabs,ucbvax}!sun!Eng!rock
"UNIX for the sake of the kingdom of heaven"  Matthew 19:12

schaefer@ogicse.ogi.edu (Barton E. Schaefer) (09/22/90)

In article <142877@sun.Eng.Sun.COM> rock@warp.Eng.Sun.COM (Bill Petro) writes:
} 
} Here it is:
} 
} #!/bin/sh
} # bq for mush
} #
} if [ $# -lt 2 ]
} then
}     echo bq: too few arguments
}     echo 'usage: bq variable command [args ...]'
} fi 1>&2
} out=$HOME/.mushbq
} var=$1
} shift
} val=`eval "$@" | tr "\012'" ' "'`
} echo set $var = "'$val'" > $out

Thanks, Bill, but you forgot what needs to go in .mushrc to support this:

    cmd bq 'sh bq \!* ; source ~/.mushbq'

The script creates a "set" command and puts it in a specific file from
which mush can "source" it.  The "tr" does a little processing to remove
single quotes from the output of the command, so that wrapping the output
in single quotes in the "set" command is certain to work.
-- 
Bart Schaefer						schaefer@cse.ogi.edu