Using getent passwd {1001..} as a subprocess

huangapple go评论110阅读模式
英文:

Using getent passwd {1001..} as a subprocess

问题

我正在编写一个Python脚本,通过Ubuntu中的“useradd”子进程创建33个用户。之后,我希望使用另一个子进程,最好是getent,来打印出已添加的用户。我想要添加{1001..1033},以便打印时省略不必要的信息,只显示基于UID的用户,但不知道如何做。

我曾尝试编写“subprocess.run(['getent','passwd'],{1001..1033})”,但这立即在Pycharm中引发了警告。没有“{1001..1033}”时,子进程能正常工作,但会打印大量冗余信息。

编辑:多亏了leviant pied的帮助,问题已解决。

英文:

I am writing a Python script that creates 33 users through the "useradd" subprocess in Ubuntu. After that I wish to print out the added users by using another subprocess, perferably getent. I want to add {1001..1033} so that the print omits the uneeded information and only shows the users based on UID but do not know how to do it.

I had tried writing "subprocess.run(["getent", "passwd"], {1001..1033})", but that immediately sends of warning signs in Pycharm. Without the "{1001..1033}" the subprocess works, but prints walls of redundant information.

EDIT: Solved thanks to leviant pied.

答案1

得分: 1

这应该可以工作:

英文:

This should work:

>>> subprocess.run(['getent', 'passwd'] + list(map(str, range(1001, 1033 + 1))))
CompletedProcess(args=['getent', 'passwd', '1001', '1002', '1003', '1004', '1005', '1006', '1007', '1008', '1009', '1010', '1011', '1012', '1013', '1014', '1015', '1016', '1017', '1018', '1019', '1020', '1021', '1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029', '1030', '1031', '1032', '1033'], returncode=2)

huangapple
  • 本文由 发表于 2023年8月4日 21:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定