linux - Linux中/etc/passwd文件最后一行获取用户ID

标签 linux shell terminal scripting command

我一直在尝试以下方法:

cut -d: -f3 | last -1 /etc/passwd

last -1 | cut -d: -f3 /etc/passwd

这些语句不起作用,我不确定如何加入它们以获得我想要的结果。它只接受/etc/passwd 目录前面的当前命令。

我对 Linux 和将命令组合在一起相当陌生。

提前感谢您的帮助。

最佳答案

尝试:

cut -d: -f3 /etc/passwd | tail -1

或者:

tail -1 /etc/passwd | cut -d: -f3

注意事项

  1. 命令 last 显示最后登录用户的列表。相比之下,tail 提供文件结尾,而 tail -1 仅提供最后一行。

  2. 考虑这个命令:

    cut -d: -f3 | last -1 /etc/passwd
    

    这会运行 cut -d: -f3 但由于提供了文件名,cut 将等待您在 stdin 上提供输入。这不是你想要的。相比之下,下面的命令提供文件 /etc/passwd 作为 cut 的输入,然后选择 cut 输出的最后一行:

    cut -d: -f3 /etc/passwd | tail -1
    

关于linux - Linux中/etc/passwd文件最后一行获取用户ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458720/

相关文章:

c - 为相同的代码获取 2 个输出

android - apk 没有 INTERNET 权限。 Selendroid 需要互联网许可才能继续

c - 使用 chdir() 从终端更改目录

git - 将我的 MacBook 更新到 MacOs Mojave 后出现 ssh_dispatch_run_fatal git 错误

python - 在 linux 中使用 python 匹配多行

linux - BASH : How to disallow user input "* " , 以及我应该检查用户输入的哪种正则表达式?

bash - 带空格的 shell glob 扩展

linux - 比较两个文件并合并数据

eclipse - 如何在 Eclipse 中运行系统 shell/终端?

ruby - 如何使用 ANSI 转义序列在 bash 中捕获终端窗口的标题?