On Mon, Apr 23, 2012 at 10:58 PM, David Korn <[email protected]> wrote:
> Subject: Re: [ast-users] sh as ksh builtin?
> --------
>
>> Hello,
>>
>> after reading a lot about ksh builtin commands was wondering if ksh
>> can have itself as built-in.
>>
>> The usecase are scripts which have many lines (sometimes a few hundred
>> or run them in loops) like sh -c 'execute something', each of them
>> creating a new process, execute a few commands and then return again
>> to the parent shell. Could ksh builtins help in such a scenario by
>> making sh itself a builtin?
>>
>> I know there are alternatives like replacing the sh -c '' lines with
>> subshells or other constructs but using builtins would require almost
>> no changes in the scripts and may be preferable in this case.
>>
>
> I am not sure what you would gain.
>
> On the other hand, ksh is written as a shared library that can
> be built into other commands.  That is how dtksh and tksh work.
>
> You could of course write a shell function that behaves like
> ksh by doing the option parsing and then used read and eval to
> do the execution.
>
> Or even simpler, you could catch  sh -c lines, with
>
>        function sh
>        {
>                if      [[ $1 == -c ]]
>                then    shift
>                        ( eval "$@")
>                else    command sh -c "$@"
>                fi
>        }
>
> but I don't know how much benefit there would be.  I addition all variables
> will be seen in the subshell, not just export variables.

Uhm... AFAIK ksh93 allows execution of scripts which are marked as
"executable" (e.g. chmod u+x scriptname) but doesn't have the '#!..."
magic thing in front. In this case the helper function could looks
like this (warning: This is an incomplete hack):
-- snip --
function sh
{
        if [[ $1 == '-c' ]] ; then
                shift
                {
                        print 'set -o xtrace'
                        printf '%s ' "$@"
                        printf '\n'
                } >/tmp/xxx.tmp
                chmod u+x '/tmp/xxx.tmp'
                /tmp/xxx.tmp
                rm '/tmp/xxx.tmp'
        else
                sh "$@"
        fi
}

sh -c 'printf "foo%s\n" bar'
-- snip --

For ksh93 becoming a builtin for itself we AFAIK need to allow more
than one |Shell_t| to exist (most of the code already allows that...
we only have to hunt-down the remainder of the global+|static|
variable usage and add some kind of "save state"+"restore state"
functions which save the parent shell state when the builtin runs and
restore the state when the child shell exists).

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) [email protected]
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)

_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to