linux - perl 输出奇怪的 `pwd`

标签 linux perl unix ubuntu pwd

我有一个非常简短的教程 perl 脚本:

#!/usr/bin/perl
print "The date is ",`date`;
print "The date is `date`",".\n";
$directory=`pwd`;
print "\nThe current directory is $directory.";

和输出:

The date is Sat Jul  2 17:04:58 PDT 2011
The date is `date`.

The current directory is total 20
-rwxr-xr-x 1 yan yan 433 2011-07-02 15:58 36
-rwxr-xr-x 1 yan yan 313 2011-07-02 16:29 43
-rwxr-xr-x 1 yan yan 116 2011-07-02 16:51 45
-rwxr-xr-x 1 yan yan 149 2011-07-02 16:53 46
-rwxr-xr-x 1 yan yan 145 2011-07-02 17:02 47

但是如果我只运行 pwd 我会得到:

yan@ubuntu:~/bin/blackperl$ pwd
/home/yan/bin/blackperl

对这里的谜团有什么合乎逻辑的解释吗?非常感谢!

最佳答案

我也无法解释这个谜团,但你可以尝试使用 Perl Core 模块 Cwd而不是 pwd 命令:

use warnings;
use strict;
use Cwd;
my $dir = getcwd();
print "$dir\n";

一个优点是 Cwdpwd 更便携。

另请参阅:UNIX 'command' equivalents in Perl

关于linux - perl 输出奇怪的 `pwd`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560301/

相关文章:

linux - 如何执行jar文件中的.sh文件

perl - 在 mojolicious 中将参数传递给 redirect_to 并在目标 Controller 中使用它们

perl - 如何在 Perl Moose OOP 中动态调用对象?

UNIX - 基于权限的 grep 文件

linux - 在多个文件上运行命令并保持相同的名称

linux - 允许特定 IP 的 MongoDB 远程访问

linux - : Swapping in Paging (memory allocation)

perl 确定 URL SSL 证书 key 长度

bash - 排序变量

linux - 在 bash 脚本中连接输入