It is currently Tue, 30 Nov 2021 17:55:46 GMT



 
Author Message
 Odd /bin/sh behavior on HP-UX 9.01
Bourne shell scripts on HP-UX 9.01 seem to have an odd side-effect when
you call an internal function: they lose their argument list. For example,
try running the following script:

-----------------------------------------------------------------------------
#!/bin/sh

does_nothing()
{
   echo "In function does_nothing"

echo "Arg1 = $1     Arg2 = $2     Arg3 = $3"

does_nothing

echo "Arg1 = $1     Arg2 = $2     Arg3 = $3"

-----------------------------------------------------------------------------
If you run this script (saved as testshell) on Sun, Dec, etc. machines,
you get the following:

Arg1 = a1     Arg2 = a2     Arg3 = a3
In function does_nothing
Arg1 = a1     Arg2 = a2     Arg3 = a3

However, if you run it on HP-UX 9.01, you get:

Arg1 = a1     Arg2 = a2     Arg3 = a3
In function does_nothing
Arg1 =      Arg2 =      Arg3 =

Obviously, calling a function causes the argument list to be lost. I've tried
to save the argument list and restore it with the set command, but that has
the side-effect of breaking up any arguments with embedded spaces; that is
unacceptable for the places this problem occurs. Has anyone run into this
problem, and have you found a solution? We're now porting our code to HP, and
we use functions quite a bit; to code-around the problem by not using the
argument list may be quite painful. Any help would be appreciated.

Thanks!

--
F. Jay Shields      | f...@epoch.com                                  |
Epoch Systems, Inc. |------------------------------------------------|
8 Technology Drive  | True:                                          |
Westboro, MA 01581  | my_opinion != employer's_opinion && my_opinion |



 Sun, 09 Feb 1997 21:01:19 GMT   
 Odd /bin/sh behavior on HP-UX 9.01

[script removed]

Yup. /bin/sh on hp/ux is braindead. Luckily, HP have supplied a
/bin/posix/sh that behaves more like a normal shell should.

                        /Mikko
--
 Mikko Tyo"la"ja"rvi                              E-mail: mi...@dynas.se
 DynaSoft, Dynamic Software AB                   Phone:  +46-8-615 84 27
 Address:  Box 92058, S-120 06 STOCKHOLM, SWEDEN Fax:    +46-8-641 92 00
 Visitors: Hammarby Fabriksv 13



 Mon, 10 Feb 1997 21:48:00 GMT   
 Odd /bin/sh behavior on HP-UX 9.01

Shells based on the SVR2 /bin/sh, like HP-UX sh and Ultrix sh5,
have a single array for positional parameters.  Shells based on
the SVR3 sh save and restore the positional parameters around
function calls.
--
"I believe every human has a finite number of heartbeats.  I don't intend
 to waste any of mine running around doing exercises." - Neil Armstrong

Chet Ramey, Case Western Reserve University     Internet: c...@po.CWRU.Edu



 Mon, 10 Feb 1997 22:04:50 GMT   
 Odd /bin/sh behavior on HP-UX 9.01
: Bourne shell scripts on HP-UX 9.01 seem to have an odd side-effect when
: you call an internal function: they lose their argument list. For example,
: try running the following script:

Certain version of the bourne shell do not preserve the parameters when
calling functions, and therefore your call to do_nothing with no
parameters actually clears the parameters before calling the function.

There is really no workaround to this except getting another shell.
--
Bruce Momjian                          |  830 Blythe Avenue
r...@candle.pha.pa.us                  |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)



 Tue, 11 Feb 1997 00:29:34 GMT   
 Odd /bin/sh behavior on HP-UX 9.01
Note: I changed comp.sys.hp to comp.sys.hp.hpux, because comp.sys.hp is
      obsolete since February.

  If you use a Bourne shell script, then you should expect this behaviour.
Anything else is asking for trouble. Mr. Tyolajarvi may call it
braindead, but realize that standard is better than better.



 Tue, 11 Feb 1997 22:04:43 GMT   
 Odd /bin/sh behavior on HP-UX 9.01
:   If you use a Bourne shell script, then you should expect this behaviour.
: Anything else is asking for trouble. Mr. Tyolajarvi may call it
: braindead, but realize that standard is better than better.

Huh?  The bourne shell of SVr3 and SVr4 preserve options between shell
fuction calls.  I certainly wouldn't say the HP's bourne shell behavior
is 'standard'.  As someone pointed out, the POSIX standard requires
preservation, and HP's POSIX 'sh' does.

--
Bruce Momjian                          |  830 Blythe Avenue
r...@candle.pha.pa.us                  |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)



 Fri, 14 Feb 1997 09:00:46 GMT   
 Odd /bin/sh behavior on HP-UX 9.01
Hi Bruce Momjian and all the rest of you!

In article <Cv9utB....@candle.pha.pa.us>,
concerning "Odd /bin/sh behavior on HP-UX 9.01",

I thought, functions are no standard yet. I.e. 4.3BSD sh isn't able to
manage functions at all ("sh: syntax error at line 3: `(' unexpected"
where line 3 is "nop() {").

Please undeceive me...

--
Greetinx, Dirk

    //'''
    //-oo   Some few hours of trial and debugging sometimes can save
     \ _ `           10 minutes of reading man pages.
      --
    +------------------------------+--------------------------------+
    |  d...@insel.de * DoD#220361  |   Dr. Brunthaler IItech GmbH   |
    | '90 VFR (Vital Feature: Red) | Berlin, FRG * +49.30.215081-16 |
    +------------------------------+--------------------------------+



 Fri, 14 Feb 1997 17:21:12 GMT   
 Odd /bin/sh behavior on HP-UX 9.01

  SVr3/SVr4 are not standards, but implementations. POSIX is a standard
and our POSIX shell conforms to it. However the original poster talked
about the Bourne shell, so that is what I responded to. Just for the
record: Our Bourne shell conforms to the SVID2 and XPG2 and XPG3
standards (see STANDARDS CONFORMANCE in the manpage).

  In retrospect, I think I should have said "you should *account* for this
behaviour" (I'm Dutch, so shoot me! :-)), i.e. the rule of the least
common denominator, i.e. as long as there are a few shells with this
behaviour, one should account for this behaviour. Just common sense,
standard is better than better, etc.. Furthermore, as Dirk correctly
pointed out, one should not even use functions at all in a strictly
Bourne shell script!



 Sun, 16 Feb 1997 19:08:51 GMT   
 Odd /bin/sh behavior on HP-UX 9.01

I hope this gets to you.  I have only read access to usenet so I cannot
post this myself except through a mail gateway which is unreliable.  Feel
free to post it if you want.  The message to which it replies is a week
old but I saw no answer so here goes.

-> Bourne shell scripts on HP-UX 9.01 seem to have an odd side-effect when
-> you call an internal function: they lose their argument list.  For
-> example, try running the following script:

The function does not actually destroy the shell command line args, it
just replaces the shells arguments with the function arguments, and you
didn't provide any function arguments.

-> There is really no workaround to this except getting another shell.

Untrue. If the function contains a fixed number of arguments use this:

func () {
        arg1 = $1 ; shift
        arg2 = $1 ; shift
        # and do whatever your function needs

# then invoke the function like this:

func firstarg secondarg "$@"

Passing the shells original command line to the function then shifting out the
arguments intended for the function leave you with the original shell command
line, embedded spaces intact.  Even on systems where this is unnecessary it
doesn't hurt.

If your function takes no arguments of it's own, just pass it the shell
arguments with "$@".  The function need to nothing at all about them.

If the function takes a variable number of arguments you should pass the
function arguments, followed by some delimiter, followed by "$@".  Then in
the function loop through the args (which you probably do anyway) and shift
each one out until you hit the delimiter.  Shift the delimiter out and you
are back to the original shell command line:

func () {
        while [ "$1" != "--" ]
        do #{
                echo $1
                # or whatever
                shift
        done #}
        # shift the delimiter out
        shift

# and call this way:

func hello world -- "$@"

I haven't actually bothered to run this so it may have silly syntax errors
(mixing in CSH syntax is my favorite) but you get the idea.

I'd be interested in hearing if this works for you, or if anyone else
suggested something better.

                        Don Mehrtens



 Tue, 18 Feb 1997 10:44:42 GMT   
 Odd /bin/sh behavior on HP-UX 9.01

: I hope this gets to you.  I have only read access to usenet so I cannot
: post this myself except through a mail gateway which is unreliable.  Feel
: free to post it if you want.  The message to which it replies is a week
: old but I saw no answer so here goes.

: -> Bourne shell scripts on HP-UX 9.01 seem to have an odd side-effect when
: -> you call an internal function: they lose their argument list.  For
: -> example, try running the following script:

: The function does not actually destroy the shell command line args, it
: just replaces the shells arguments with the function arguments, and you
: didn't provide any function arguments.

: -> There is really no workaround to this except getting another shell.

: Untrue. If the function contains a fixed number of arguments use this:

: func () {
:       arg1 = $1 ; shift
:       arg2 = $1 ; shift
:       # and do whatever your function needs
: }

: # then invoke the function like this:

: func firstarg secondarg "$@"

: Passing the shells original command line to the function then shifting out the
: arguments intended for the function leave you with the original shell command
: line, embedded spaces intact.  Even on systems where this is unnecessary it
: doesn't hurt.

: If your function takes no arguments of it's own, just pass it the shell
: arguments with "$@".  The function need to nothing at all about them.

: If the function takes a variable number of arguments you should pass the
: function arguments, followed by some delimiter, followed by "$@".  Then in
: the function loop through the args (which you probably do anyway) and shift
: each one out until you hit the delimiter.  Shift the delimiter out and you
: are back to the original shell command line:

: func () {
:       while [ "$1" != "--" ]
:       do #{
:               echo $1
:               # or whatever
:               shift
:       done #}
:       # shift the delimiter out
:       shift
: }

: # and call this way:

: func hello world -- "$@"

: I haven't actually bothered to run this so it may have silly syntax errors
: (mixing in CSH syntax is my favorite) but you get the idea.

: I'd be interested in hearing if this works for you, or if anyone else
: suggested something better.

:                       Don Mehrtens

Here's an example of what I use (or why I increasing code in the ksh)

================================= CUT HERE =================================
#!/bin/sh
# <vi: se ts=30 sw=4 ws :>
#set -x
###############################################################################
# futst             :  test function substitution                             #
# Created by        :  Dan Mercer,Health Sciences,737-7707,546-4003           #
# Copyright         :  Applications + Plus                                    #
# Date Created      :  Fri Sep  2 16:34:47 CDT 1994                           #
# Date Last Altered :  Fri Sep  2 16:34:47 CDT 1994                           #
###############################################################################

restore_args() { return; }

prt_stuff() {

no_args=true
ct=0
for i
    do
    no_args=false
    ct=`expr $ct + 1`
    eval echo "ARGV[$ct] = \$$ct"
    done

$no_args && echo "prt_stuff called without args"

return

# I'm too lazy to type in the args so I'll just shove them in here
set -- abc def ghi jkl "mno pqr" "tuv wxyz 123"

save=
for i
    do
    save="${save}\"${i}\" "
    done

prt_stuff "$@"

prt_stuff

prt_stuff 123 456 789

eval set -- "$save"

prt_stuff "$@"
================================= CUT HERE =================================

--
Dan Mercer                                            ClearSystems, Inc
Reply To:  damer...@mmm.com                           "The Mad Pedant"
======================================================================
All opinions expressed are my own and do not reflect the opinions of
my employer or my employer's clients,  in particular 3M Company.
All advice or software offered or presented is provided As Is with no
warranty either expressed or implied.  Follow at your own risk.
Objects in the mirror are closer than they appear.



 Wed, 19 Feb 1997 06:00:03 GMT   
 
   [ 10 post ] 

Similar Threads

1. /bin/nodename on HP-UX 9.01

2. problem NFS-mounting from Solaris 2.1 to HP-UX 9.01

3. Term 1.12, HP-UX 9.01, and good news ...

4. Term 1.12 and HP-UX 9.01

5. term 1.0.7 on hp/ux 9.01

6. Installing HP/UX 9.01 in a Seagate drive

7. tcsh 6.03 isn't expanding wildcard characters on HP-UX 9.01

8. SAM on HP-UX 9.01 has a problem


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software