我怎样才能可靠地获取 AIX 中文件的所有者?通过可靠,我不想解析ls
.在 Linux 上,我只会执行 a stat --printf=%U foo
,但我正在 AIX 6.1 和 7.1 上工作。我知道我可以这样做,但由于AIX 上istat
没有选项,我仍然必须使用 和 来处理输出,因此不太理想。换句话说,如何仅使用 AIX 的核心实用程序来模拟 Linux?--printf
istat
grep
awk
stat --printf=%U foo
答案1
这是我不久前编写的一个脚本,目的是为了在 AIX 中获得类似 stat(1) 的实用程序。刚刚添加%U!我发现使用 -c 选项更有用,它的行为与 --printf 略有不同。包括一个方便的 perl 统计数组的本地副本作为注释块。
#!/usr/bin/env perl -w
# emulate GNU coreutils stat command in a limited way
# -- only implemented a subset of the stat() options
use strict;
use Getopt::Std;
our $opt_c;
getopts('c:') or die "Usage: $0 [ -c (%n %i %u %g %s %U %X %Y %Z) ] file ...";
# default format is empty (not useful, but avoids 'undef' errors later)
$opt_c |= '';
for (@ARGV) {
my @s = stat;
next unless @s; # silently fail on to the next file
my $p = $opt_c; # make a copy of the format string to mangle for each file
# mangle and print
$p =~ s/%n/$_/g;
$p =~ s/%i/$s[1]/g;
$p =~ s/%u/$s[4]/g;
$p =~ s/%g/$s[5]/g;
$p =~ s/%s/$s[7]/g;
$p =~ s/%U/getpwuid($s[4])/eg;
$p =~ s/%X/$s[8]/g;
$p =~ s/%Y/$s[9]/g;
$p =~ s/%Z/$s[10]/g;
print "$p\n";
# 0 dev device number of filesystem
# 1 ino inode number
# 2 mode file mode (type and permissions)
# 3 nlink number of (hard) links to the file
# 4 uid numeric user ID of file's owner
# 5 gid numeric group ID of file's owner
# 6 rdev the device identifier (special files only)
# 7 size total size of file, in bytes
# 8 atime last access time in seconds since the epoch
# 9 mtime last modify time in seconds since the epoch
# 10 ctime inode change time in seconds since the epoch (*)
# 11 blksize preferred block size for file system I/O
# 12 blocks actual number of blocks allocated
}