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?
+1 but useless use of cat! :-) `awk -F: '$7 == "/bin/bash" { print $1 }' /etc/passwd`
Greg Bacon
2010-01-26 19:34:03
gbacon is right, cat is not needed.
Jay Zeng
2010-01-26 19:37:29
Good catch. Checking against the entire field, as Bear did, is a better approach.
jamessan
2010-01-26 21:07:28
Since you have set delimiter to ":" , why check the entire line for bash? use $7
ghostdog74
2010-01-26 23:39:09
@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
2010-01-27 02:53:37
+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 theFS
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
2010-01-26 19:34:47
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
2010-01-26 23:45:01