Reputation: 69
DIR1: a/b/c/d DIR2: e/f/g/h
now I execute a Perl Script in DIR1 and chdir to DIR2(to use data in DIR2), after the script is executed I want to go back to DIR1. Is it default, if not then what command should be used.
Thanks in advance.
Upvotes: 3
Views: 3944
Reputation: 91
As per i get your question your purpose is first go to some directory like /a/b/c then execute some stuff as per requirement then next step is go to another directory like /e/f/g & execute some stuff.so i write below code as per your need.
$dir1 = "/home";
# This changes perl directory and moves you inside /home directory.
chdir( $dir1 ) or die "Couldn't go inside $dir directory, $!";
print "Your first location is $dir1\n";
#Execute your stuff in dir1
$dir2 = "/home/prasad";
# This changes perl directory and moves you inside /home directory.
chdir( $dir2 ) or die "Couldn't go inside $dir directory, $!";
print "Your second location is $dir2\n";
#Execute your stuff in dir2
Upvotes: 0
Reputation: 198324
Your question is not very clear. There are two different things I think you may be asking about. If I understand your setup, you do this:
$ cd a/b/c/d
$ perl stuff.pl
stuff.pl
does chdir("e/f/g/h")
. Now you want to go back to a/b/c/d
. Correct?
You want to go back after the script is done. Since the script is executed in a subprocess, when you exit the subprocess you will be back where you were before you started the script. Nothing special needs to be done.
You want to go back before the script is done. In this case, you can remember the current directory:
#!/usr/bin/env perl
# remember the directory
my $curdir = `pwd`;
chdir("e/f/g/h");
# ...
# return back:
chdir($curdir);
# ...
Upvotes: 1