Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cd to a directory from a script

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
IL
Can I change to another directory from a ksh/perl script, and then exit the script so my current directory will be the changed one?

I tried it with no success yet. I think it's because the script is changed the directory indeed, but inside an internal shell, so when it terminates the script it returns to the original directory.
Am I right? Is there any solution for it?

Thanks in advance,
tg.
 
Can't be done, unless you run the script inside your own login shell with the '.' (dot) command. And even then only for a shell script - not for a perl script.

$ pwd
/home/mydir
$ cat cdscript
cd /tmp
pwd
exit
$ chmod +x cdscript
$ cdscript
/tmp
$ pwd
/home/mydir
$ . cdscript
/tmp
$ pwd
/tmp

By running a perl script or a shell script without the dot command, you run a child process of your login shell process. Whatever is done with regard of the current dir in that process has no reflect on your login shell process' current dir.

HTH,

p5wizard
 
There is way to do it.

Check out my FAQ in this forums FAQ.

Unix Scripting FAQ
ksh Inter-process comm's -- faq822-3992

Using Signal and TWO-WAY pipe to cd login shell from another script

JRjr
[morning]
 
Rather than create a shell script, perhaps you could define a shell function in your .profile. Or even an alias, as in this example: thread822-689159
 
p5wizard... you said a horrible, horrible word!!! I won't repeat it, but it's the first word of your message.

This works, I tested it:
Code:
exec perl -e 'chdir ("/"); exec( "/bin/bash" );'
So, it stands to reason that this would also:
Code:
exec myscript.pl
... as long as the script changes directory and [tt]exec[/tt]s the shell again before the end.

vince@home:~/dev> exec perl -e 'chdir ("/"); exec( "/bin/bash" );'
vince@home:/>

... Oh yeah!! This is the Unix Scripting forum ...
vince@home:~> exec perl -e 'chdir ("/"); exec( "/bin/bash" );'
vince@home:~> echo '#!/bin/bash' > test.sh
vince@home:~> echo 'cd /' >> test.sh
vince@home:~> echo 'exec /bin/bash' >> test.sh
vince@home:~> chmod 755 test.sh
vince@home:~> exec ./test.sh
vince@home:/>

--
-- Ghodmode
 
... oops, that first line wasn't supposed to be there in the last quote :~/

--
-- Ghodmode
 
Well, OK so it CAN be done this way, if you can live with the idea of being kicked out of the system and have to log in again if ever there's something wrong within your perl script and it doesn't have a chance of exec-ing your shell without breaking apart...

Plus your users have to know that this script/perl program needs to be exec-ed instead of just run...

Personally I like the idea of a shell function better.


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top