[comp.unix.wizards] Positional Parameter Settin in function

gph@hpsemc.HP.COM (Paul Houtz) (06/06/89)

In my ksh, the "set" command doesn't seem to work within a function:

As a shell command  I do the following:

    $ set `date`
    $ echo $4
    16:31:00

Now if I declare a function:

    $ foo () {
     /    set `date`
     /    }
 
    $ foo
    $ echo $4
    16:31:00

As you can see, the old `date` is still in the positional parameters, otherwise
the seconds part of the date should have changed by now.  

Is this a bug in ksh?  Is it a feature?   Is there some explanation of this
feature?

Thanks for any help you can give.


Paul Houtz
HP Technology Access Center
10670 N. Tantau Avenue
Cupertino, Ca 95014
(408) 725-3864
hplabs!hpda!hpsemc!gph 
gph%hpsemc@hplabs.HP.COM
    

lml@cbnews.ATT.COM (L. Mark Larsen) (06/07/89)

# In article <570024@hpsemc.HP.COM> gph@hpsemc.HP.COM (Paul Houtz) writes:
#
# In my ksh, the "set" command doesn't seem to work within a function:
# 
It works in a function - it just sets the positional parameters for
*that* function - not the current shell.

# As a shell command  I do the following:
(example deleted)
# 
# Is this a bug in ksh?  Is it a feature?   Is there some explanation of this
# feature?

This is not a bug but the way functions are supposed to work.  To modify
the positional parameters of the current shell by executing some shell
script, use ". filename [args]" (reads the file and executes it in the
current environment).  Note that Bourne shell does not allow arguments
and that in ksh, passing arguments in this manner is the same as setting
the positional parameters of the current shell.

cheers,
-lml

vinoski@apollo.COM (Stephen Vinoski) (06/08/89)

In article <570024@hpsemc.HP.COM> gph@hpsemc.HP.COM (Paul Houtz) writes:
>
>In my ksh, the "set" command doesn't seem to work within a function:
>
>    $ set `date`
>    $ echo $4
>    16:31:00
>
>Now if I declare a function:
>
>    $ foo () {
>     /    set `date`
>     /    }
> 
>    $ foo
>    $ echo $4
>    16:31:00
>
>As you can see, the old `date` is still in the positional parameters, otherwise
>the seconds part of the date should have changed by now.  

In ksh, positional parameters are not shared between the function and its
caller.

Try "The Kornshell Command and Programming Language", Morris I. Bolsky and David
G. Korn, Prentice-Hall, 1989, ISBN 0-13-516972-0.  It's a great book.


-steve
-- 
  Steve Vinoski       ## Apollo, a Subsidiary of HP ## ARPA: vinoski@apollo.com
  (508)256-6600 x5904 ## Chelmsford, MA  01824      ## UUCP: ...!apollo!vinoski
  "...no hardware designer should be allowed to produce any piece of hardware
   until 3 software guys have signed off for it."   -Andy Tanenbaum

allbery@ncoast.ORG (Brandon S. Allbery) (06/11/89)

As quoted from <570024@hpsemc.HP.COM> by gph@hpsemc.HP.COM (Paul Houtz):
+---------------
| In my ksh, the "set" command doesn't seem to work within a function:
| Is this a bug in ksh?  Is it a feature?   Is there some explanation of this
| feature?
+---------------

Feature.  Positional parameters are local to a function; as "set" sets
positional parameters, its effect is lost when the function exits.

Under (System V) /bin/sh, positionals are global:  "set" works the way you
expect, but calling a function with arguments destroys the shell's global
parameters -- which has caused me considerable annoyance at times.

I prefer the ksh implementation.

++Brandon
-- 
Brandon S. Allbery, moderator of comp.sources.misc	     allbery@ncoast.org
uunet!hal.cwru.edu!ncoast!allbery		    ncoast!allbery@hal.cwru.edu
      Send comp.sources.misc submissions to comp-sources-misc@<backbone>
NCoast Public Access UN*X - (216) 781-6201, 300/1200/2400 baud, login: makeuser