0

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[0]
    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.

3
  • 1
    Please be specific on what is not working i.e. do you get no output or an error etc. A test data sample might help too.
    – 0buz
    Sep 6, 2021 at 15:22
  • i have edited my question
    – Umesh417
    Sep 6, 2021 at 15:26
  • Something I noticed is you've got if shell = mainshell instead of if shell == mainshell.
    – 0buz
    Sep 6, 2021 at 15:29

1 Answer 1

0

You are almost there with your script. But there are 2 reasons why its not working.

  1. Your if statement is not doing a string comparison. You should be using == not =
  2. You compare the string mainshell with shell, but you assume shell does 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 :)

1
  • there was a trailing white space that was causing if block to do nothing. Thank you very much.
    – Umesh417
    Sep 6, 2021 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.