tags:

views:

58

answers:

4
+1  Q: 

AWK command help

How do I write an awk command that reads through the /etc/passwd file, and prints out just the names of any users who have the /bin/bash program as their default command shell?

+4  A: 
cat /etc/passwd | awk -F ":" '$7=="/bin/bash" { print $1 }'
Bear
+1 but useless use of cat! :-) `awk -F: '$7 == "/bin/bash" { print $1 }' /etc/passwd`
Greg Bacon
gbacon is right, cat is not needed.
Jay Zeng
A: 

awk -F: '/\/bin\/bash$/{print $1}' /etc/passwd

jamessan
So /bin/bash is the same as /tmp/attack/bin/bash?
Roger Pate
Good catch. Checking against the entire field, as Bear did, is a better approach.
jamessan
Since you have set delimiter to ":" , why check the entire line for bash? use $7
ghostdog74
@ghostdog74: That's exactly what I was referring to with my reply to Roger. I didn't edit my answer so as to leave consistency with the comments.
jamessan
+3  A: 

Since this is homework, I won't write the program for you (and hopefully no one else does either), but here is what you need to know:

  • The default field separator in AWK is whitespace; the field separator in /etc/passwd is a colon. You can change the field separator in AWK via the FS variable, or -F at the command line.

  • In /etc/passwd/, the shell is listed in the 7th field.

Well, in the time it took me to write this much, two people have done your homework for you. Good luck!

danben
Oops, didn't notice the homework tag when I posted.
jamessan
A: 

@OP, you can use awk

awk 'BEGIN{FS=":"}$7~/bash/{print $1}' /etc/passwd

the above checks for bash, but does not restrict to /bin/bash. If you definitely need /bin/bash, change it.

OR tell your teacher you want to use just the shell

#!/bin/bash

while IFS=":" read -r user b c d e f sh h
do
    case "$sh" in
        *bash* ) echo $user;;
    esac
done <"/etc/passwd"
ghostdog74