ksh alias ignored in autoloaded function

ksh alias ignored in autoloaded function

Post by blaz » Sat, 17 Jun 2006 09:47:34



Hello

I have problem using alias inside autoloaded function,
it can be illustrated with the following example:

there is function definition file ./foo

function foo {
    msg

Quote:}

... and the main script ./main

#!/bin/ksh
alias msg='print Hello!'
FPATH=.
autoload foo
foo

when executing main script, there is error:
./main: foo[2]: msg: not found [No such file or directory]

what's wrong with this example?

It does't work under AT&T ksh (linux.i386 binary),
but works fine when use pdksh

blazk

 
 
 

ksh alias ignored in autoloaded function

Post by Janis Papanagno » Sat, 17 Jun 2006 11:04:28



> Hello

> I have problem using alias inside autoloaded function,
> it can be illustrated with the following example:

> there is function definition file ./foo

> function foo {
>     msg
> }

> ... and the main script ./main

> #!/bin/ksh
> alias msg='print Hello!'
> FPATH=.
> autoload foo
> foo

Write a function instead of using an alias...

   function msg
   {
     print "Hello!"
   }

In a very limited context alias is fine for interactive shell use;
for scripting it makes more problems (more or less subtle) than is
of help, and it is not really necessary to use it here.

Quote:

> when executing main script, there is error:
> ./main: foo[2]: msg: not found [No such file or directory]

> what's wrong with this example?

> It does't work under AT&T ksh (linux.i386 binary),
> but works fine when use pdksh

Alias behaviour has changed from ksh88 to ksh93 (pdksh was -partly-
based on ksh88 functionality); for example, in ksh93 it is not
possible any more to export aliases to external scripts. I have no
ksh88 at hand so I cannot tell whether the behaviour you expect was
present in ksh88.

Janis

- Show quoted text -

Quote:> blazk


 
 
 

ksh alias ignored in autoloaded function

Post by blaz » Sat, 17 Jun 2006 17:17:14


Quote:> In a very limited context alias is fine for interactive shell use;
> for scripting it makes more problems (more or less subtle) than is
> of help, and it is not really necessary to use it here.

I'm writing a function for reporting errors, it prints error message
and i would like to print also the name of the calling script/function
and line number where the error occured.

i hoped it would be possible to wrap an actual error reporting function
with an alias that additionally passes ${0##*/} and $LINENO  to this
actual function

is it possible to do it in a similar way but without using alias?

blaz

 
 
 

ksh alias ignored in autoloaded function

Post by Janis Papanagno » Sat, 17 Jun 2006 20:14:23



> I'm writing a function for reporting errors, it prints error message
> and i would like to print also the name of the calling script/function
> and line number where the error occured.

> i hoped it would be possible to wrap an actual error reporting function
> with an alias that additionally passes ${0##*/} and $LINENO  to this
> actual function

> is it possible to do it in a similar way but without using alias?

If I understand correct you want it for "cosmetical" reasons, to avoid
passing ${0##*/} and $LINENO explicitly as arguments to the function.
You could do a preprocessing that instruments your scripts with that
additional code. (Similar solutions in the C language also use the cpp
preprocessor for such tasks.)

I haven't the book at hand but I think that Rosenblatt used some method
for passing line numbers in his shell de* example; you might want
to have a look at it. But AFAIR he also uses some form of preprocessing.

Janis

 
 
 

ksh alias ignored in autoloaded function

Post by Jon LaBadi » Mon, 19 Jun 2006 03:40:46




>> Hello

>> I have problem using alias inside autoloaded function,
>> it can be illustrated with the following example:

>> there is function definition file ./foo

>> function foo {
>>     msg
>> }

>> ... and the main script ./main

>> #!/bin/ksh
>> alias msg='print Hello!'
>> FPATH=.
>> autoload foo
>> foo

> Write a function instead of using an alias...

>   function msg
>   {
>     print "Hello!"
>   }

> In a very limited context alias is fine for interactive shell use;
> for scripting it makes more problems (more or less subtle) than is
> of help, and it is not really necessary to use it here.

>> when executing main script, there is error:
>> ./main: foo[2]: msg: not found [No such file or directory]

>> what's wrong with this example?

>> It does't work under AT&T ksh (linux.i386 binary),
>> but works fine when use pdksh

> Alias behaviour has changed from ksh88 to ksh93 (pdksh was -partly-
> based on ksh88 functionality); for example, in ksh93 it is not
> possible any more to export aliases to external scripts. I have no
> ksh88 at hand so I cannot tell whether the behaviour you expect was
> present in ksh88.

Works in Solaris 9 ksh88 but not ksh93.
 
 
 

ksh alias ignored in autoloaded function

Post by blaz » Mon, 19 Jun 2006 10:30:09


Quote:> Alias behaviour has changed from ksh88 to ksh93 (pdksh was -partly-
> based on ksh88 functionality); for example, in ksh93 it is not
> possible any more to export aliases to external scripts. I have no
> ksh88 at hand so I cannot tell whether the behaviour you expect was
> present in ksh88.

seems that in ksh93 it is impossible to use aliases inside
autoloaded functions at all (except aliases that are compiled into the
shell, for example 'autoload' works:)

Source code of the function loader  src/cmd/ksh93/sh/path.c: funload()
suggests that alias expanding is explicitly turned off when function is
read
("sh_onstate(SH_NOALIAS)" line). I wonder why such restriction was
made...

on the other hand, ksh93 manual says:
"...Shell functions are read in and stored internally.
Alias names are resolved when the function is read...."
http://www.cs.princeton.edu/~jlk/kornshell/doc/man93.html#Aliasing
this is strange...

blaz

 
 
 

ksh alias ignored in autoloaded function

Post by blaz » Mon, 19 Jun 2006 10:50:24


Quote:

> If I understand correct you want it for "cosmetical" reasons, to avoid
> passing ${0##*/} and $LINENO explicitly as arguments to the function.

yes, exactly
this is funny - the same solution is used in ksh93 source test scripts,
for example
'err_exit' function and corresponding 'err_exit' alias in
src/cmd/ksh93/tests/alias.sh
(but autoloaded functions are not involved, so it works...)

blaz

 
 
 

1. ksh silently ignores function if mistakenly not autoloaded

Suppose I have a ksh function "myfunc" in a file "myfunc".
In a ksh script which invokes the function "myfunc", I have set the FPATH to
include the directory (on Solaris 2.6) where the file "myfunc" lives.

But I carelessly forgot to autoload the function "myfunc".

My ksh script motors past the call to the function:

    stuff=$( myfunc )

 without a single word of complaint.  Why is this?  Not even a bad status in
$? to help me.
Of course, I should carefully check whether function "myfunc" returned
anything, on its STDOUT, to my variable $stuff . . .

Is there any "set" option I can use to have ksh warn me about unset
functions, or is there just no way for ksh to guess that "myfunc" is a
function call in the first place because I did not autoload it?

I already start the script with
    set -o nounset
but this did not catch my error.

Thank-you,
Clyde

2. Free OSR504 and VisionFS

3. ksh: autoloading aliases?

4. iomem ends at ~0UL, not 0xffffffff

5. Passing Variables to Autoload Functions in ksh

6. Has anyone looked at the McAfee virus scanner for unix?

7. Bash alias ignored inside of function

8. Diamond Viper 330 under Linux

9. ksh: functions and aliases

10. ksh question: alias vs. functions

11. How does one include a / character in alias or function name in ksh on SVR4?

12. Korn's Response - ksh autoload quirk

13. ksh head ache - here doc within a ksh function