1833059 Members
2574 Online
110049 Solutions
New Discussion

Script

 
SOLVED
Go to solution
Carlo Henrico_1
Regular Advisor

Script

I am trying to do a cd in my session with a script.

i.e. in an script have:

cd /oracle/app/product

but after it is run, I am back to the original directory.

What am I doing wrong?
Live fast, die young - enjoy a good looking corpse!
7 REPLIES 7
John Palmer
Honored Contributor

Re: Script

The cd only affects the script that you called - it's a separate process.

If you want to influence your current script then you'll have to 'dot' the subscript:-

.
Regards,
John
Carlo Henrico_1
Regular Advisor

Re: Script

Sorry, I'm still lost.

'dot' the script?
Live fast, die young - enjoy a good looking corpse!
Kofi ARTHIABAH
Honored Contributor

Re: Script

Carlo:

that is because you program gets run as a child process to your shell, so when the program exits, the parent process (your shell) does not remember anything.
you may have to use "exec" to spawn your process. or:

# . your_script

the . (dot) reads your script into your current shell and so it will work.
nothing wrong with me that a few lines of code cannot fix!
Kofi ARTHIABAH
Honored Contributor

Re: Script

yes Carlo, from the command prompt, type . (dot) followed by the full path to your scriptname
nothing wrong with me that a few lines of code cannot fix!
Andreas Voss
Honored Contributor
Solution

Re: Script

Hi,

for your purposes you can use the alias function (posix/korn shell).
Ie: alias orap="cd /oracle/app/product"
Then if you type orap you will change to the specified directory.
To make it permanent insert the alias line into your $HOME/.profile

Regards

federico_3
Honored Contributor

Re: Script



Do like this:

example: -> the script is /tmp/test_cd

#!/bin/ksh

cd /tmp

# . /tmp/test_cd

I hope this helps
John Palmer
Honored Contributor

Re: Script

If you have a script called for instance 'sss', when you type sss the script runs as a child process of your current shell. Any changes to the environment that are made in sss (such as cd) only affect that process.

You can however run a script within your current shell by typing:-
. sss
This causes your shell to read sss and obey the commands itself. You see it used a lot to condition shell environments for a particular purpose. For example '. oraenv' is used to setup the environment variables etc in an Oracle environment.
I'ts commonly called 'dotting' a script.

For more details (and a better explanation) do 'man sh-posix' and search for Special Commands.

Regards,
John