我应该使用哪个系统命令来获得下面给出的所需输出

我应该使用哪个系统命令来获得下面给出的所需输出

我需要编写一个系统程序来模拟whow打印自登录以来的时间(格式为 HH:MM:SS)和主机名。允许命令行选项-p也打印用户正在运行的有趣进程。在主用户行下方的行上精心设计。例如。

23:45:11 vkbhardwaj-Inspiron-5558
11009 -tcsh
13032 vi a.c
13033 ps axl

我已经使用过这个命令

alias vk="awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime;hostname;pgrep -l"

但该命令会引发错误,如下所示:

awk: line 1: regular expression compile failed (missing '(')
3600):int((%3600)
awk: line 1: syntax error at or near :
vkbhardwaj-Inspiron-5558
pgrep: no matching criteria specified
Try `pgrep --help' for more information.

请尽快帮忙

答案1

alias我认为使用 shell 函数而不是更简单。

接近你想要的东西是:

vk() { lastlog -u "${USER}" | awk -v ORS="" 'END{$1="";$2="";$3="";print}' ; hostname ; if [[ $# > 0 && $1 == "-p" ]] ; then ps -u; fi;}

打印上次登录的日期和时间,而不是the time since login

相关内容