[comp.unix.questions] Overriding Parameter Expansion?

gph@hpsemc.HP.COM (Paul Houtz ) (08/23/88)

  
   Does any one know if it is possible for a program or function to
   tell whether the shell (especially csh or ksh) has expanded a 
   parameter into multiple file names?

   I know that the it is designed to do this, but I need to eliminate the
   possibility.

   For example:

     myfunc () {
       echo $1
       echo $2
       echo $3
       }

    If I type in myfunc file1 file2 file3, then the results are
      file1
      file2
      file3

    If I type in myfunc * file2 file3
  
      then the results are
       first_file_in_directory
       second_file_in_directory
       third_file_in_directory 

   It would be nice if I could at least know that expansion has 
   occurred in parameter 1.    

   Note, I do NOT want to turn off parameter expansion (set -f) 
   in my shell.  I also do NOT want to quote the parameters, e.g.,
   I do not want to say myfunc "*" file2 file3.

   Finally, something to consider:

   Since there are commands that will do the filename expansion for you,
   doesn't it seem contrary to the basic idea of UNIX that this expansion
   is done for you automatically by the shell?   Normally unix does not 
   get in my way, but in this example it does.  I wouldn't mind if I had 
   to expand my parameters myself.  I think I should be able to invoke
   a function and have access to the raw parameters with a shell script.
   (and more than 9, too.)  Oh well. 
 

lvc@cbnews.ATT.COM (Lawrence V. Cipriani) (08/25/88)

In article <810026@hpsemc.HP.COM>, gph@hpsemc.HP.COM (Paul Houtz ) writes:
>    Does any one know if it is possible for a program or function to
>    tell whether the shell (especially csh or ksh) has expanded a 
>    parameter into multiple file names?

A UNIX(tm) program cannot determine how it's argument list was generated.
I don't know about csh.  For ksh you can use a combination on an alias and
a function to control filename generation.  For example:

	alias myfunc='set -f;_myfunc'
	function _myfunc
	{
		trap 'set +f' EXIT	# not sure of the exact syntax here
		...whatever...
	}

>    It would be nice if I could at least know that expansion has 
>    occurred in parameter 1.    

You'll have to be careful to check if * expands to * when there are no
matching filenames.

>    Note, I do NOT want to turn off parameter expansion (set -f) 
>    in my shell.  I also do NOT want to quote the parameters, e.g.,
>    I do not want to say myfunc "*" file2 file3.

This turns it off during the function execution and then turns it back on
when the function completes.  Good enough?

>    Since there are commands that will do the filename expansion for you,
>    doesn't it seem contrary to the basic idea of UNIX that this expansion
>    is done for you automatically by the shell?

I like it being in the shell primarily for consistency and secondarily
for efficiency.

>    Normally unix [sic] does not 
>    get in my way, but in this example it does.  I wouldn't mind if I had 
>    to expand my parameters myself.  I think I should be able to invoke
>    a function and have access to the raw parameters with a shell script.
>    (and more than 9, too.)  Oh well. 

With ksh you can access up to 99 arguments, and can get access to the 'raw'
parameters as I have shown.

-- 
Larry Cipriani, AT&T Network Systems, Columbus OH, cbnews!lvc lvc@cbnews.ATT.COM