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 :)

Shawn
  • 47,241
  • 3
  • 26
  • 60
aastha
  • 180
  • 10
  • 2
    Perl has a [`mkdir`](https://perldoc.perl.org/functions/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... – Shawn Nov 07 '22 at 04:53
  • 2
    Plus you're not stripping the newline off `$dir` before using it, so you'll get an interesting directory created. – Shawn Nov 07 '22 at 04:53
  • 4
    And always, always, always `use warnings;` and `use strict;` in perl scripts. – Shawn Nov 07 '22 at 04:55

0 Answers0