1

Hi I am new to Perl and I am trying to change the directory in Linux by call a Perl script how my script works: It takes Input from user which directory name they want and then it cd into that directory and perform something.

#!/grid/common/bin/perl
 use File::chdir;
 use Cwd;
 print "Directory name \n";
 $dir = <STDIN>;
 $mkdir = `mkdir $dir`;
 print "creating dir";
 $newdir = "$mkdir";
 chdir( $newdir ) or die "Couldn't go inside $newdir directory, $!";

P.S I googled before asking this question and I could not find the solution tried chdir, calling bash script in perl none of them worked can you guys please help me. thanks in advance :)

3
  • 2
    Perl has a mkdir function, btw. No need to run an external program. And mkdir(1) doesn't normally have any output, meaning your $mkdir variable doesn't have anything useful... Commented Nov 7, 2022 at 4:53
  • 2
    Plus you're not stripping the newline off $dir before using it, so you'll get an interesting directory created. Commented Nov 7, 2022 at 4:53
  • 4
    And always, always, always use warnings; and use strict; in perl scripts. Commented Nov 7, 2022 at 4:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.