Queue commands to last subshel even if one of them launches new shell

0

I want to automate something writing simple script launching consecutively few scripts one after another. And then setting some aliases at the top shell.
Problem: few of these commands launch new shell. And all commands after that one are launched in previous shell. Is there anyway to queue these commands coming after that one in "top" shell?
Simple Example:

#!/usr/bin/ksh
#normally this ksh is executed deep inside other script i cannot modify
ksh
echo test

Is there any way to print "test" on shell just started by ksh command? Normally test is printed in previous shell - becomes available after "exit" command Thanks for any help.

Aleksander Fular

Posted 2013-05-23T12:27:27.330

Reputation: 103

Your use of "top shell" is slightly confusing. You mean the last subshell that you started, right? – Uwe – 2013-05-23T13:03:03.123

yes, thats correct. – Aleksander Fular – 2013-05-23T13:45:57.193

Answers

0

Four alternatives, but each of them has a drawback:

  1. Make sure that the commands that you want to execute in the ksh are written to the file mycmds and start the ksh in the following form:

    cat mycmds /dev/tty | ksh -i
    

    Drawback: You lose the command-line editing features of ksh.

  2. Make sure that the commands that you want to execute in the ksh are written to the file /tmp/mycmds$$; set the environment variable MYCMDS to /tmp/mycmds$$ before starting ksh -l; and add the following to your $HOME/.profile:

    if [ -n "$MYCMDS" ] ; then
        . "$MYCMDS"
        MYCMDS=
        export MYCMDS
    fi
    

    Drawback: Everybody who uses this program has to modify his $HOME/.profile.

  3. Organize the entire thing in such a way that the commands you want to execute are written to a file mycmds which is then sourced via . mycmds in the initial shell. Put all that into a function definition.

    Drawback: Rather than starting a new shell, you modify the environment and/or alias definitions of the initial one. Everybody who uses this program has to use ksh.

  4. Make sure that the commands that you want to execute are written to the file mycmds and start a bash in the following form:

    bash -i --init-file mycmds
    

    Drawback: You get a bash instead of a ksh.

Uwe

Posted 2013-05-23T12:27:27.330

Reputation: 1 043

lshmm, the result is almost what i want when i use : ( cat mycmds /dev/tty ) | ksh (without the -i ). But the prefix before.. dont know how this is called before -> "you can type here" no longer appears – Aleksander Fular – 2013-05-23T14:57:40.300

It's called "prompt". Without -i or -l, a ksh that reads from a pipeline is not an interactive one (man ksh), which means in particular that there is no prompt. – Uwe – 2013-05-23T14:59:20.443

By the way, the parentheses around cat mycmds /dev/tty are useless. Originally, I had two commands here; after deleting one of them I should also have deleted the parentheses. – Uwe – 2013-05-23T15:04:44.987

Hmm, ok. However when i use the -i. that shell window is "stuck" it does not accept commands anymore. or should i say it doesnt responds anymore. And without the -i option ( no longer interactive ) my .profile aliases arent visible in this new shell. – Aleksander Fular – 2013-05-23T15:16:05.897

You're right about the problems with -i, and the version without this option doesn't work well either. I'd go for method 2 or 4 (2 is essentially an emulation of the bash --init-file option.) – Uwe – 2013-05-23T18:53:00.633

Ok, so I worked this out! Within my script, at the end of it I split current terminal (e.g xterm &). And I Launch it using cat myScript | ksh. Everything works great now! thanks. The new terminal that opens has everything set, and the old one is disposable. – Aleksander Fular – 2013-05-24T06:44:19.230