-5

我想创建一个 UID 和用户名的报告,格式类似于:

9142   jdoe
9158   jsmith
75488  msteel

等等等等

来自 Unix 中的“id”命令,在我们的系统上产生如下结果:

% id jdoe
uid=9142(jdoe) gid=101(users) groups=101(users)

此外,如果用户名不存在:

% id asdasdasd
id: asdasdasd: No such user

我希望结果类似于(使用破折号而不是不存在的 UID):

----   asdasdasd
9142   jdoe
9158   jsmith
75488  msteel

非常感谢您的帮助!谢谢!

4

2 回答 2

1
id $1 > temp
if [ $? -eq 0 ]
then
    cat temp | awk -F' ' '{print $1}'|awk -F'=' '{print $2}'|sed 's/(/ /g'|sed 's/)//g' >> output.txt
else
    echo "------" $1 >> output.txt
fi
rm temp
于 2012-11-28T10:19:38.030 回答
1

它不漂亮,但是:

sed -e 's/^uid=//;s/).*//;s/(/ /;s/^id:/----/;s/:.*//' | column -t

示例输出:

(id; id root; id daemon; id foobar ) 2>&1 \
| sed -e 's/^uid=//;s/).*//;s/(/ /;s/^id:/----/;s/:.*//' \
| column -t

501   atomlinson
0     root
1     daemon
----  foobar
于 2012-11-28T05:37:11.053 回答