如何才能可靠地获取AIX中文件的所有者?可靠地说,我不想解析的输出ls
。在Linux上,我只会做一个stat --printf=%U foo
,但是我正在使用AIX 6.1和7.1。我知道我可以做到istat
,但是由于--printf
AIX上没有选项istat
,我仍然必须使用grep
和来调整输出awk
,因此并不理想。换句话说,如何stat --printf=%U foo
仅使用AIX的核心utils来模拟Linux ?
这是我前一段时间编写的脚本,目的是在AIX中获得类似于stat(1)的实用程序。刚刚添加了%U!我发现使用-c选项更加有用,该选项的行为与--printf略有不同。包括perl的stat数组的便捷本地副本作为注释块。
#!/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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句