开发者

AWK command help

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


cat /etc/passwd | awk -F ":" '$7=="/bin/bash" { print $1 }'


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!


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


@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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜