This commit is contained in:
2022-11-22 18:03:47 +01:00
parent aa8cd49cb6
commit 9734d2f21f
2 changed files with 20 additions and 11 deletions

View File

@@ -21,8 +21,9 @@ def parse_passwd_lines(line: str) -> "Dict of passwd details":
parts = line.split(':') parts = line.split(':')
userdict = { userdict = {
"username" : parts[0], "username" : parts[0],
"uid" : parts[2], "uid" : int(parts[2]),
"gid" : parts[3], "gid" : int(parts[3]),
"realname" : parts[4].split(',')[0],
"gecos" : parts[4], "gecos" : parts[4],
"home" : parts[5], "home" : parts[5],
"shell": parts[6] "shell": parts[6]
@@ -38,23 +39,33 @@ def build_userlist(lines) -> "List of user dicts":
def print_userlist_sorted_by_username(userlist): def print_userlist_sorted_by_username(userlist):
#ruft für jedes Element key(elem) also e im Lambda-Ausdruck ist ein Element der Liste user dict #ruft für jedes Element key(elem) also e im Lambda-Ausdruck ist ein Element der Liste user dict
for user in sorted(userlist, key=lambda e: e['username'].lower()): for user in sorted(userlist, key=lambda e: e['username'].lower()):
print( print("{:5} {:32} {}".format(
user['username'],
user['uid'], user['uid'],
user['gecos'], user['username'],
user['realname'],
user['home'], user['home'],
user['shell'] user['shell']
) )
)
def print_userlist_sorted_by_uid(userlist): def print_userlist_sorted_by_uid(userlist):
pass for user in sorted(userlist, key=lambda e: e['uid']):
print( "{:5} {:32} {}".format(
user['uid'],
user['username'],
user['realname'],
user['home'],
user['shell']
)
)
def main(): def main():
output_functions = { output_functions = {
'username': print_userlist_sorted_by_username, 'username': print_userlist_sorted_by_username,
'uid': print_userlist_sorted_by_uid, 'uid': print_userlist_sorted_by_uid,
} }
outfunc = output_functions['username'] outfunc = output_functions['username']
outfunc = output_functions['uid']
lines = read_file("/etc/passwd") lines = read_file("/etc/passwd")
userlist = build_userlist(lines) userlist = build_userlist(lines)
outfunc(userlist) outfunc(userlist)

View File

@@ -17,6 +17,4 @@ rstrip schneidet \n ab
schneidet hinten was ab bei einem String schneidet hinten was ab bei einem String
list
mit helpdir