gpt4 book ai didi

linux - 在一行中使用 awk 和 stat 来打印 stat 的值

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:26 34 4
gpt4 key购买 nike

下面是我要运行的脚本。我不能在 awk 中使用 stat。

cat /etc/passwd | awk 'BEGIN{FS=":"}{print $6 }' | (stat $6 | sed -n '/^Access: (/{s/Access: (\([0-9]\+\).*$/\1/;p}' })'

我想要的输出应该是这样的:

 /root 0550 
/bin 0777
/sbin 0777

我可以使用 while 或 for.. 编写脚本,但我希望所有脚本都在一行中,没有循环。

最佳答案

这应该行得通吗?

 awk 'BEGIN{FS=":"}{cmd="stat -c\"'%a'\" "$6 ; cmd  | getline perm; close(cmd); printf "%s\t%s\n",$6,perm }' /etc/passwd

编辑 1:简要说明:

BEGIN{
FS=":" # set field separator
}
{
cmd="stat -c\"'%a'\" "$6 ; # define stat as a command w/ only perms as output
cmd | getline perm; # capture those perms for the current $6
close(cmd); # close the pipe, good practice to get into
printf "%s\t%s\n",$6,perm # print the directory name and the correlated perms
}

关于linux - 在一行中使用 awk 和 stat 来打印 stat 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53530090/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com