AWK命令帮助
如何编写一个 awk 命令来读取 /etc/passwd 文件,并仅打印出将 /bin/bash 程序作为默认命令 shell 的任何用户的名称?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于这是家庭作业,我不会为您编写该程序(希望其他人也不会这样做),但您需要了解以下内容:
AWK 中的默认字段分隔符是空格;
/etc/passwd
中的字段分隔符是冒号。您可以通过FS
变量或命令行中的-F
更改 AWK 中的字段分隔符。在
/etc/passwd/
中,shell 列在第 7 个字段中。好吧,在我写这么多的时间里,有两个人已经帮你做了功课。祝你好运!
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!
awk -F: '/\/bin\/bash$/{print $1}' /etc/passwd
awk -F: '/\/bin\/bash$/{print $1}' /etc/passwd
@OP,您可以使用 awk
对 bash 进行上述检查,但不限于 /bin/bash。如果您确实需要 /bin/bash,请更改它。
或者告诉你的老师你只想使用 shell
@OP, you can use awk
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