Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to make perl affect my shell

by jatill (Beadle)
on Sep 14, 2005 at 15:19 UTC ( [id://491978]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I want to run a perl script that takes an argument. When the script is finished, I want to be in a different directory than I started. Is that possible? I'm willing to use any additional unix toys such as aliases or shell scripts if necessary.

Replies are listed 'Best First'.
Re: How to make perl affect my shell
by Hue-Bond (Priest) on Sep 14, 2005 at 15:33 UTC

    Yes, but not in the same shell process that you "came from". Your Perl script would need to spawn another shell after chdir'ing to the new directory. You could also have a modified environment if you wanted to.

    --
    David Serrano

Re: How to make perl affect my shell
by jatill (Beadle) on Sep 14, 2005 at 15:37 UTC
    Looks like I found an answer to my own question. I just needed to set up my alias with a \!^ to pass along the param to perl. ex: alias warp 'cd `warp.pl \!^`' and then have the perl script print out the destination directory.
Re: How to make perl affect my shell
by samizdat (Vicar) on Sep 14, 2005 at 15:39 UTC
    If you have your program exec a new shell, you will end up there.
    #!/usr/bin/perl use strict; # idiot use warnings; # pruf chdir '/home/dwilde'; exec '/usr/local/bin/bash';
    As has been pointed out, you will be in a #new# shell.
Re: How to make perl affect my shell
by zigdon (Deacon) on Sep 15, 2005 at 07:30 UTC
    The way I've done it is I have an alias in the shell that sources a file that's created by the perl script:
    apocalypse$ which c c is an alias for . ~/bin/mychdir $ cat ~/bin/mychdir # ~/bin/finddir $1 $2 $3 $4 $5 $6 $7 $8 $9 cd "`cat $HOME/.targetdir`"
    The benefit is that it stays within the same shell, which means my history and background jobs are all still there.

    -- zigdon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://491978]
Approved by ww
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.