iic has asked for the wisdom of the Perl Monks concerning the following question:

Hello, `` and exec() only can run commands in a new child process individually. Commands in different `` or exec call can not interact. For example:
`cd /tmp`;`ls`; - "cd" command does not make sense to "ls".

I hope my daemon script can read user's input as command from the listening port and run them all in one shell. Even if user inputs "sh", I hope it can fork a bash that cover the default csh and then user can run their following commands in the bash.

I have been pursuing the solution for a long time. Do you have any good ideas to help to implement it?

Thanks a lot!
iic

Replies are listed 'Best First'.
Re: perl script like a "shell proxy"
by Fletch (Bishop) on Nov 07, 2007 at 16:39 UTC
      Thanks.

      yup, it's a command filter since the Export Control..to instead of the common telnet.

      They are all not standard perl modules. I need some low level solutions...
Re: perl script like a "shell proxy"
by moritz (Cardinal) on Nov 07, 2007 at 16:44 UTC
    I don't really know how to do it, but at least I know how it's called, a REPL, read-eval-print-loop.

    Perhaps searching for that might reveal some ideas. Perhaps the source of Devel::REPL is inspiring as well.

Re: perl script like a "shell proxy"
by KurtSchwind (Chaplain) on Nov 07, 2007 at 17:16 UTC
    While you can't do:
    `cd /tmp`;`ls`
    And expect the ls to be an ls of /tmp, you can use a semicolon all in one escape. `cd /tmp;ls` or system('cd /tmp;ls');. Those would be in 1 subshell.
    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re: perl script like a "shell proxy"
by cdarke (Prior) on Nov 07, 2007 at 17:33 UTC
    ls(1) runs in a child process, it is an external program. This is the case whether you run it from bash or perl. cd is a shell built-in, and does not create a child process. It alters the current directory, which is then inherited by any child processes. I am not sure what you intend to do - running built-ins in the same shell is fine, just redirect the shell's STDIN using open with a pipe, then print the commands to the pipe handle. Not sure what you will achieve running external programs this way though (to be honest I'm not sure why you want to use a shell at all).

    Maybe this is semantics. A subshell is a shell mechanism where a separate environment is created, but a child process is not fork'ed if all the commands are built-ins. Think of it as an evnironment stack - changes are lost when the stack is collapsed at the end of the subshell.
Re: perl script like a "shell proxy"
by superfrink (Curate) on Nov 07, 2007 at 17:03 UTC
    You could write the commands to a file and execute the file.
    print `/bin/bash /tmp/command_file_123 2>&1`;
Re: perl script like a "shell proxy"
by cdarke (Prior) on Nov 08, 2007 at 11:31 UTC
    Even if user inputs "sh", I hope it can fork a bash that cover the default csh and then user can run their following commands in the bash.

    "sh" runs Bash as a Bourne shell look-alike, you do not get full bash. sh is often a symbolic link to bash, but bash checks how it was called and acts accordingly (not uncommon).

    Easiest was to override csh is to place "exec bash" in .login.