英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论