在 perl 中找不到路径
Path not found in perl
我想获取指定目录中文件所有者的名称(循环中的 2 个仅用于测试,反正我的 /media 中只有 2 个文件)。所以,如果用户的输入是“/media”,我得到以下输出:
sh: 2: /kuba: not found
sh: 2: /sf_Shared: not found
root:kuba
看来,该脚本有效,但无论如何都显示“未找到”。
my $directory =<STDIN>; #"/media";
my @ls = qx(ls $directory);
chomp @ls;
my @fow;
for(my $i = 0; $i < 2; $i++ )
{
$fow[$i] = qx(stat -c '%U' $directory/$ls[$i]);
#say $fow[$i];
}
chomp @fow;
print "$fow[0]:$ls[0]\n";
顺便说一句,当我删除用户的输入并将 $directory 声明为“/media”时,它工作得很好。
使用 chomp 函数从用户输入中删除结尾的换行符 (\n)。
my $directory = <STDIN>; #"/media";
chomp($directory);
为什么不使用 perl 指令来代替 shell 调用,这会占用大量资源?
use strict;
use warnings;
my $dir = shift || die "Provide a directory name";
my($file, $owner);
while( glob("$dir/*") ) {
$file = $_;
$owner = getpwuid((stat($_))[4]);
write;
}
$~ = 'STDOUT_BOTTOM';
write;
exit 0;
format STDOUT_TOP =
+----------------------------------------------------------+-----------------+
| File | User |
+----------------------------------------------------------+-----------------+
.
format STDOUT =
| @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<<< |
$file, $owner
.
format STDOUT_BOTTOM =
+----------------------------------------------------------+-----------------+
.
我认为最简单的方法是这样的:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use File::stat;
# Default to the current directory if one isn't given on
# the command line.
# Note: "defined-or" operator so we don't fail on a
# directory called "0"
my $dir = shift // '.';
# Use \Q...\E around the directory name to make life easier
while (my $file = glob("\Q$dir\E/*")) {
say $file;
# File::stat changes the behaviour of stat() and makes
# it far easier to use
my $uid = stat($file)->uid;
my $user = getpwuid($uid);
# Simple output option :-)
say "$file / $user";
}
我想获取指定目录中文件所有者的名称(循环中的 2 个仅用于测试,反正我的 /media 中只有 2 个文件)。所以,如果用户的输入是“/media”,我得到以下输出:
sh: 2: /kuba: not found sh: 2: /sf_Shared: not found root:kuba
看来,该脚本有效,但无论如何都显示“未找到”。
my $directory =<STDIN>; #"/media";
my @ls = qx(ls $directory);
chomp @ls;
my @fow;
for(my $i = 0; $i < 2; $i++ )
{
$fow[$i] = qx(stat -c '%U' $directory/$ls[$i]);
#say $fow[$i];
}
chomp @fow;
print "$fow[0]:$ls[0]\n";
顺便说一句,当我删除用户的输入并将 $directory 声明为“/media”时,它工作得很好。
使用 chomp 函数从用户输入中删除结尾的换行符 (\n)。
my $directory = <STDIN>; #"/media";
chomp($directory);
为什么不使用 perl 指令来代替 shell 调用,这会占用大量资源?
use strict;
use warnings;
my $dir = shift || die "Provide a directory name";
my($file, $owner);
while( glob("$dir/*") ) {
$file = $_;
$owner = getpwuid((stat($_))[4]);
write;
}
$~ = 'STDOUT_BOTTOM';
write;
exit 0;
format STDOUT_TOP =
+----------------------------------------------------------+-----------------+
| File | User |
+----------------------------------------------------------+-----------------+
.
format STDOUT =
| @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<<< |
$file, $owner
.
format STDOUT_BOTTOM =
+----------------------------------------------------------+-----------------+
.
我认为最简单的方法是这样的:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use File::stat;
# Default to the current directory if one isn't given on
# the command line.
# Note: "defined-or" operator so we don't fail on a
# directory called "0"
my $dir = shift // '.';
# Use \Q...\E around the directory name to make life easier
while (my $file = glob("\Q$dir\E/*")) {
say $file;
# File::stat changes the behaviour of stat() and makes
# it far easier to use
my $uid = stat($file)->uid;
my $user = getpwuid($uid);
# Simple output option :-)
say "$file / $user";
}