Perl得到源文件的路徑,使用shell變量

 

爲了保證平臺兼容性,多使用Cwd取當前目錄
use Cwd;
my $dir = getcwd;
或者
my $dir = cwd;
my $dir = fastgetcwd;
它們都返回程序運行的當前路徑.

use Cwd 'abs_path';
my $abs_path = abs_path($file);
$abs_path = realpath($file);
$abs_path= fast_abs_path($file);

 

返回一個文件的真實地址,Symlink的話返回Symlink到的地址.

默認的,$ENV{PWD}總不會更新
#!/usr/bin/perl
print $ENV{PWD};        # /root/perl
chdir('/');
print $ENV{PWD};        # /root/perl

要解決這個問題,可以使用use Cwd qw(chdir):

#!/usr/bin/perl
use Cwd qw(chdir);
print "$ENV{PWD}/n";
chdir('/');
print "$ENV{PWD}/n";        # /

 

3、perl中使用shell變量

declare -x path=/user/test

 

test.pl

#!/home/niewf/test.pl

$path=$ENV{"path"};

echo "$path/n";

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章