A short python script i wrote to fetch users who are using bash. below is script. I don’t know why its not working. Please share your ideas as to how this program is working inside.I am beginner in python and looking for lectures on data structures and algorithms. any answers are welcome. Thanks
f = open("/etc/passwd") mainshell = '/bin/bash' for line in f: field = line.split(:) shell = field[-1] user = field if shell = mainshell: print(user)
edit: I am getting no output. I tried to fetch values of variable shell and users and that exactly what i need but somehow if block is not working. Its not giving any error but just not working.
You are almost there with your script. But there are 2 reasons why its not working.
ifstatement is not doing a string comparison. You should be using
- You compare the string
shell, but you assume
shelldoes not contain whitespaces. But the string will probably look like this
/bin/bash(notice the whitespaces at the end?). This can be removed with
shell = field[-1].strip()
I think then your program should work fine 🙂
Answered By – Haller Patrick