问题描述
我需要一个 Unix 命令,它将显示我的组列表,用逗号分隔.所以,groups $FT_USER
只显示列表,但我需要把它分成一个逗号分隔的列表!
I need an Unix command which will display a list of my groups separated with commas. So, groups $FT_USER
is only displaying the list, but I need to separate it into a comma-separated list!
推荐答案
请注意,这不适用于 Cygwin.实际上,只要名称中有空格的组就行不通.我认为这通常只发生在 Windows (Cygwin) 上,但会导致不正确的结果.我会很快给出一个导致问题的例子.
Note that this will not work for Cygwin. Actually, it won't work whenever there are groups with spaces in the names. I think this usually only happens on Windows (Cygwin), but it leads to incorrect results. I'll quickly give an example where it causes a problem.
$ groups $FT_USER
Users CONSOLE LOGON Authenticated Users CurrentSession LOCAL
这里的组是 {'Users', 'CONSOLE LOGON', 'Authenticated Users', 'CurrentSession', 'LOCAL'}
.相信我,或者去看看这篇文章,那里有更多细节.
The groups here are {'Users', 'CONSOLE LOGON', 'Authenticated Users', 'CurrentSession', 'LOCAL'}
. Trust me on this one, or go see this post, where there are some more details.
@Kusalananda 之前给出的命令在大多数情况下都有效,但给出了以下不正确的输出.
The command given before by @Kusalananda, which will work in most cases, gives the following, incorrect output.
$ groups $FT_USER | tr ' ' ','
Users,CONSOLE,LOGON,Authenticated,Users,CurrentSession,LOCAL
来自@SriniV 的命令将给出相同的输出.一些组名是分裂的".我们得到 ...,CONSOLE,LOGIN,...
而不是 ...,CONSOLE LOGIN,...
和 ...,Authenticated,Users,...
而不是 ...,Authenticated Users,...
The command from @SriniV will give the same output. Some of the group names are "split". We get ...,CONSOLE,LOGIN,...
instead of ...,CONSOLE LOGIN,...
and ...,Authenticated,Users,...
instead of ...,Authenticated Users,...
(我知道,我知道:你为什么使用 Cygwin?"答案:工作需要它.)
(I know, I know: "Why are you using Cygwin?" Answer: The job requires it.)
要获得我们想要的输出,我们需要使用id
命令,它需要一个 --zero
标志:
To get the output we want, we need to use the id
command, which takes a --zero
flag:
$ man id | grep -A 1 "\-\-zero"
-z, --zero
delimit entries with NUL characters, not whitespace;
然后我们可以使用@Kusalananda 所描述的 tr
或 awk
和@SriniV,分别.一个问题是,由于尾随NUL
,总是会有尾随的,
.这可以通过 sed
命令纠正,我们打印除最后一个逗号之外的所有内容,sed 's/\(.*\),/\1/'
.请注意, .*
将打印所有内容,直到最后一个逗号.\1
打印(反斜杠转义)括号中的所有内容,即 \1
将打印 .*
部分找到的 >\(.*\),
We can then use tr
or awk
as described by @Kusalananda and @SriniV, respectively. One problem is that there will always be a trailing ,
because of the trailing NUL
. This can be rectified by a sed
command where we print everything but the last comma, sed 's/\(.*\),/\1/'
. Note that the .*
will print everything until the last comma. The \1
prints everything in the (backslash escaped) parentheses, i.e. \1
will print whatever was found by the .*
part of \(.*\),
让我们用 tr
$ id --zero -Gn $FT_USER | tr '\0' ',' | sed 's/\(.*\),/\1/'
Users,CONSOLE LOGON,Authenticated Users,CurrentSession,LOCAL
现在,我们将使用 awk
$ id --zero -Gn $FT_USER | gawk -F "\0" '{$1=$1}1' OFS="," | sed 's/\(.*\),/\1/'
Users,CONSOLE LOGON,Authenticated Users,CurrentSession,LOCAL
编辑
如果你(和我一样)觉得最后的 sed
有点作弊,这里有一个替代方案:
Edit
If you feel (as I do) that the sed
at the end is a bit of a cheat, here's an alternative:
$ id -Gnz $FT_USER | awk 'BEGIN{FS="\0"; OFS=","}{NF--; print}'
Users,CONSOLE LOGON,Authenticated Users,CurrentSession,LOCAL
另请注意,无论您在哪里看到上面的 --zero -Gn
,都可以将其替换为 -Gnz
.-G
打印出当前用户所属的所有组,-n
打印名称,而不是组 ID (GID),-z
做我们在上面描述的 --zero
做的事情
Also note that, wherever you see --zero -Gn
above, you can replace it with -Gnz
. -G
prints out all of the groups to which the current user belongs, -n
prints the name, rather than the group id (GID), and -z
does what we described --zero
doing above
这篇关于来自 $FT_USER 的逗号分隔列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!