Print current directory using Perl

后端 未结 7 2149
走了就别回头了
走了就别回头了 2020-12-29 05:53

I have this code to print the current directory using Perl:

use Cwd qw(abs_path);
my $path = abs_path($0);
print \"$path\\n\";

But it is di

7条回答
  •  执念已碎
    2020-12-29 06:22

    I used my script in dirs with symlinks. The script parses the path and executes commands depending on the path. I was faced with the correct determination of the current path.

    Here is example:

    root@srv apache # pwd
    /services/apache
    
    root@srv apache # readlink -f .
    /services/apache2225
    

    Cwd module disclosures path (analogue of readlink -f) http://perldoc.perl.org/Cwd.html

    root@server apache # perl -e 'use Cwd; print cwd . "\n";'
    /services/apache2225
    

    If you need to get current path like pwd, you can use $ENV{'PWD'}

    root@srv apache # perl -e 'use Cwd; print $ENV{'PWD'}."\n";'
    /services/apache
    

    Thank you.

提交回复
热议问题