HP3000-L Archives

April 2001, Week 4

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Mark Bixby <[log in to unmask]>
Reply To:
Mark Bixby <[log in to unmask]>
Date:
Sat, 28 Apr 2001 14:56:56 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (85 lines)
Hi HP3000-L,

I just came up with a clever way to make the same script file executable from
either the CI or the POSIX shell using strictly native semantics without having
to resort to things like ":XEQ SH.HPBIN.SYS -c" or callci.

Consider the following script file (and note that case and spacing and line
layout REALLY matters):

anyparm parms= 2>/dev/null

if hpacctcap=hpcidepth
then
#
# We're being executed by the POSIX shell
#
echo "shell parameters: $@"
exit 0
fi
else
comment
comment We're being executed by the MPE CI
comment
if "!parms" = "2>/dev/null" then
  echo CI parameters omitted
else
  echo CI parameters: !parms
endif
endif

Create this file on your e3000 and call it "BOTH".  Now see how you can run it
equally well from both the CI or the shell:

:BOTH
CI parameters omitted
:BOTH hi there
CI parameters: hi there

Note that the CI example assumes BOTH is findable via my HPPATH variable.

:xeq sh.hpbin.sys -L
########################################################################

               MPE/iX Shell and Utilities (A.50.02)
    COPYRIGHT (c) Hewlett-Packard Company 1992, All Rights Reserved.

########################################################################


BHAWK1:/BIXBY/PUB$ BOTH
shell parameters:
BHAWK1:/BIXBY/PUB$ BOTH hi there
shell parameters: hi there

Note that the shell example assumes BOTH is findable via my PATH variable.

There are two key pieces of magic here.  First, the ANYPARM statement.  The CI
requires that you declare your parameters, and you have to do it at the
beginning of the command file with ANYPARM or PARM.  But the POSIX shell will
error out on ANYPARM and print an error message.  So we sneakily make the
ANYPARM default value a redirect of stderr to /dev/null to suppress the error
output.  The MPE CI will just parse this as the default parameter value.

The other piece of magic is the if statement:

if hpacctcap=hpcidepth
then

When the CI executes this, the answer is false, and so it will skip ahead
looking for an else or endif.  But when the shell executes this, instead of a
comparison, it is parsed as a variable assignment (the variable hpacctcap is
assigned a value of the string "hpcidepth") which has a true result, and so
drops down into the then clause.

Note that while the CI is skipping ahead looking for an endif, it will get
excited by any leading ifs or elses in the shell code.  But you can prevent
that by writing ";if" and ";else" instead which makes those statements
invisible to the CI, but yet still gets parsed correctly by the shell.
--
[log in to unmask]
Remainder of .sig suppressed to conserve scarce California electrons...

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2