Stay in direcotory after finished program

Hello everybody,

I’ve tried to write a program / script (“my_prog”) that changes the
working directory.
The problem is: the “my_prog” program runs on a different shell. when it
dies, my shell stay on the same directory it was before…

What shell i do in order to make my shell to change dir?

Thanks in advanced,

Matan Lior.

David V. <davidv@elisra.com> wrote:

Hello everybody,

I’ve tried to write a program / script (“my_prog”) that changes the
working directory.
The problem is: the “my_prog” program runs on a different shell. when it
dies, my shell stay on the same directory it was before…

What shell i do in order to make my shell to change dir?

In general, a child can not change anything about its parent’s environment,
including environment variables and current working directory.

When writing a shell script, if you use the . command to run the subshell,
that says to include and run the shell script as commands in the current
shell process, which would include enabling that script to change its
“parent”'s directory. A program can not do this.

(Well, that is not quite true, ezfm, a file manager from SJF software,
does do this – what it does is use some of the special dev_* functions
like dev_insert_chars() to make the parent shell think that the user has
actually typed that command into the input stream of the shell. IMO, this
is a hack.)

If you’re looking at this for interactive use, you’ve got a couple of choices:
– always use . to invoke the script
– instead of creating a script, create a shell function, defined in
somewhere common, such as the /etc/profile if you want everyone to
have it, or in the local startup file (where the ENV variable points,
commonly .shrc or .kshrc) and define the function there, something
like:
function my_prog {
cd /home/work/my_prog
}
– define an alias that will do something similar and put it in a similar
place


If looking at the choice between function and alias – a simple cd
command like I gave above would be better as an alias, but if you need
more complex work (two or more lines of code to figure the correct directory)
you are better off with a function.

-David

QNX Training Services
dagibbs@qnx.com

On 19 Jun 2001 15:21:39 GMT, David Gibbs <dagibbs@qnx.com> wrote:

David V. <> davidv@elisra.com> > wrote:
Hello everybody,

I’ve tried to write a program / script (“my_prog”) that changes the
working directory.
The problem is: the “my_prog” program runs on a different shell. when it
dies, my shell stay on the same directory it was before…

What shell i do in order to make my shell to change dir?

In general, a child can not change anything about its parent’s environment,
including environment variables and current working directory.

When writing a shell script, if you use the . command to run the subshell,
that says to include and run the shell script as commands in the current
shell process, which would include enabling that script to change its
“parent”'s directory. A program can not do this.

(Well, that is not quite true, ezfm, a file manager from SJF software,
does do this – what it does is use some of the special dev_* functions
like dev_insert_chars() to make the parent shell think that the user has
actually typed that command into the input stream of the shell. IMO, this
is a hack.)

If you’re looking at this for interactive use, you’ve got a couple of choices:
– always use . to invoke the script
– instead of creating a script, create a shell function, defined in
somewhere common, such as the /etc/profile if you want everyone to
have it, or in the local startup file (where the ENV variable points,
commonly .shrc or .kshrc) and define the function there, something
like:
function my_prog {
cd /home/work/my_prog
}
– define an alias that will do something similar and put it in a similar
place


If looking at the choice between function and alias – a simple cd
command like I gave above would be better as an alias, but if you need
more complex work (two or more lines of code to figure the correct directory)
you are better off with a function.

-David

QNX Training Services
dagibbs@qnx.com

If you want to cd to a computed directory, i. e., my_prog
determines a target directory that depends on parameters, date/time,
pressure, whatever, just communicate the result somehow and do the cd
afterwards. With stdout, it would be "cd my_prog param1 ...".

ako