如何在多行而不是单行输出上打印 linux 组名 [英] How to print linux group names on multiple line instead of single line output

查看:19
本文介绍了如何在多行而不是单行输出上打印 linux 组名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过 getent、组命令、id -Gn $user 和一些 sed 组合,但我认为我无法实现,因此联系了其他程序员.
我希望能够打印这个:groups abc123
输出abc123 : devops 构建测试设计

I have tried getent, group command, id -Gn $user and some sed combination but I don't think I am able to achieve hence reaching out to fellow programmers.
I want to be able to print this : groups abc123
Output abc123 : devops build test design

预期输出
组:
- 开发运维
- 构建
- 测试
- 设计

Expected Output
groups:
- devops
- build
- test
- design

推荐答案

据我所知,您正在尝试将用户的组转换为 yaml 数组,尝试使用:

From what I see, you are trying to convert the groups of your user to an yaml array, try to use:

echo "groups:" ; for i in $(id -Gn myuser);do echo "  - $i" ;done

groups:
  - users
  - lp
  - vboxusers
  - kvm

你也可以使用:

echo "groups: [ $(groups myuser | sed -e 's/.+s+:s+(.+)/1/g' -e 's/(s+)/, /g') ]"

groups: [ myuser, lp, vboxusers, kvm ]

这篇关于如何在多行而不是单行输出上打印 linux 组名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆