0

I need to create a menu screen in Python of all the users in the system, and after the system displays the list you choose the user you want and then another menu should pop-up.

So far I have:

os.system("cut -d: -f1 /etc/passwd")    
chose = str(raw_input("Select user from this list > "))

How can I make the second list appear after I choose a user?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Kulyuly
  • 43
  • 4
  • It depend on what you are using. – Kenly Jan 31 '16 at 15:33
  • Do you mean, are you trying to get the output of `os.system()`? If so, [this question](http://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on) would be helpful. – Remi Guan Jan 31 '16 at 15:35
  • I mean that after lets say the list of users I have is root, user1, user2 etc.. after I choose lets say user2 a new list of options needs to pop up with new commands like show user groups or show user id etc... – Kulyuly Jan 31 '16 at 15:42
  • 1
    Why don't you just parse `/etc/passwd` in Python? – GingerPlusPlus Jan 31 '16 at 15:45
  • [Python script to list users and groups](http://stackoverflow.com/q/421618/3821804) – GingerPlusPlus Jan 31 '16 at 15:49
  • Which `second list`? – Kenly Jan 31 '16 at 15:56
  • The command should look like this: 1) List of all users When I choose this list another list pops up: 1)user1 2)user2 and so on, if I pick user 1 a new list of options should appears like 1)show user groups 2) show user id and so on and I don't know how to write a command that will connect me between the user list to the user id and groups options list. p.s I'm on debian os writing in python for those who asked – Kulyuly Jan 31 '16 at 16:26

1 Answers1

0

You can use pwd and grp modules.
We use grp to get user group.

import pwd, grp

user = ''
users = pwd.getpwall()
i=0
for p in users:
    print str(i) + ')' + str(p[0])
    i+=1


chose = int(raw_input("Select user from this list > "))
user = users[chose]
print
print '1) show user groups \n2) show user id'
print 
chose = int(raw_input("Your choice > "))

if chose == 1:
    print grp.getgrgid(user.pw_gid).gr_name
else:
    print user.pw_uid
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • Thanks that solved half of my problem now I only need to figure out how to tell the program to open another list in relation to the user I chose. like: 1) show user groups 2)show user id 3)show user alias and so on – Kulyuly Jan 31 '16 at 16:46