[comp.mail.mush] Putting command text in a mush variable

comp-mail-mush@srhqla.uucp (06/14/89)

From: "Barton E. Schaefer" <island!ucbcad!cse.ogc.edu!schaefer>

How many times have you wished you could do something like this in mush?

	mush> set date = `date`
	mush> echo $date
	Mon Jun 12 13:20:15 PDT 1989

Well, here's a simply apalling hack to (almost) allow you to do that.
Before I tell you how to set this up, let me give a quick example of how
it is used, and say something about its limitations.

I call this "bq" for `b'ack`q'uote, because it simulates the backquote
mechanism of sh/csh (in fact, it USES the backquote mechanism of sh).
The syntax is

	bq variable unix-command

where "variable" is the name of the variable you want to set, and
"unix-command" is the command you want to run.  If "unix-command"
involves any pipes, be sure to escape each `|' character with a
backslash.

So, to do the simple example with "date" above, you would type:

	bq date date

which sets $date to the current date.  A more useful example:

	bq known_hosts uuname

sets $known_hosts to all of your system's uucp neighbors.

Now, about the limitations.  The most obvious one is that all newlines
generated by the "unix-command" get changed into spaces.  A less obvious
but possibly more annoying one is that the multiple levels of shell
interpretation make it very hard to get spaces or either kind of quote
mark into the arguments of the command.  You just sort of have to play
with it and see what happens.  Also, there is NO WAY to get a single (')
quote mark into the variable, regardless of what goes into the arguments.
Another annoying one is that mush can't use cmd aliases in the .mushrc
file, so you can't use bq in this way from there -- but I'll tell you how
to get around that one.  Finally, mush can only handle about 1000
characters of output from the command, so be careful.

Oh, yeah, and even if "unix-command" fails, "variable" will be set to
the empty string.

So how do you set this up?  First, add to your .mushrc file the line:

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

If you have the variable $unix set, you can omit the "sh", but I
recommend leaving it anyway.  It should be obvious from this how you
have to use bq to set things in .mushrc -- you type out the whole thing:

	# Get the known_hosts automatically
	sh bq known_hosts uuname
	source ~/.mushbq

Once you have .mushrc all arranged, unpack this archive and put the "bq"
script in a directory accessible from your $PATH.  If your OS doesn't
understand the #! convention, change the first line to a `:'.

# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by ogccse!schaefer on Mon Jun 12 13:23:04 PDT 1989
# Contents:  bq
 
echo x - bq
sed 's/^@//' > "bq" <<'@//E*O*F bq//'
#!/bin/sh
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
@//E*O*F bq//
chmod u=rwx,g=rx,o=rx bq
 
exit 0

# End of shell archive

That's all there is to it.  Have fun ....

BTW, the much-delayed patch #4 will be coming out as soon as Dan and I
resolve some last-minute details.

-- 
Bart Schaefer           "And if you believe that, you'll believe anything."
                                                            -- DangerMouse
CSNET / Internet                schaefer@cse.ogc.edu
UUCP                            ...{sequent,tektronix,verdix}!ogccse!schaefer