我想写一个 bash 脚本,它接受两个参数作为输入(day hostname
)并输出在当天登录的所有用户的 username
当前月份 和来自 hostname
的地址。
到目前为止,我已经编写了这段代码:
#!/bin/bash
#parameter correction checking
if [ ! $# -eq 2 ]
then echo "You have input the wrong number of parameters!"
fi
#Current month checking
CurrentMonth=`date | cut -d' ' -f2`
#This will contain the abbreviation of the current month: e.g "Mar"
#$2 -> IP/hostname; $1 -> day (of the current month)
last | grep '$2' | grep '$CurrentMonth $1' | cut -d' ' -f1
#here the two greps will get the lines that check out our requirements, and the cut will return only the username
我做错了什么?
如果我将 CurrentMonth
变量替换为 Mar
并将 $2
替换为实际的 IP 地址
最佳答案
单引号 (''
) 之间的所有内容都将被视为文字文本,并且不会扩展变量。而是对包含变量的字符串使用双引号 (""
)。
请注意,如果您使用 bash -x yourscript
启动它或将 shebang 更改为 #!/bin/bash -x,您始终可以调试 bash 脚本并获得显示的扩展命令
。
您的脚本的工作版本如下所示:
#!/bin/bash
#parameter correction checking
if [ ! $# -eq 2 ]
then echo "You have input the wrong number of parameters!"
fi
#Current month checking
CurrentMonth=`date | cut -d' ' -f2`
#This will contain the abbreviation of the current month: e.g "Mar"
#$2 -> IP/hostname; $1 -> day (of the current month)
last | grep "$2" | grep "$CurrentMonth $1" | cut -d' ' -f1
#here the two greps will get the lines that check out our requirements, and the cut will return only the username
注意事项:
last
默认打印一个缩写的用户名,您可能需要添加 -w
标志来打印全名。
另请注意,您可以使用 last
s 内置函数来按时间过滤,而不是 grep
:
-s, --since time
Display the state of logins since the specified time. This is useful, e.g., to easily determine who was logged in at a particular time. The option is often combined with --until.
-t, --until time
Display the state of logins until the specified time.
TIME FORMATS
The options that take the time argument understand the following formats:
┌────────────────────┬────────────────────────────────────────────┐
│ │ │
│YYYYMMDDhhmmss │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│YYYY-MM-DD hh:mm:ss │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│YYYY-MM-DD hh:mm │ (seconds will be set to 00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│YYYY-MM-DD │ (time will be set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│hh:mm:ss │ (date will be set to today) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│hh:mm │ (date will be set to today, seconds to 00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│now │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│yesterday │ (time is set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│today │ (time is set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│tomorrow │ (time is set to 00:00:00) │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│+5min │ │
├────────────────────┼────────────────────────────────────────────┤
│ │ │
│-5days │ │
└────────────────────┴────────────────────────────────────────────┘
关于linux - 查找在特定日期从特定计算机登录的所有用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71627405/