我通过脚本使用这个命令:
watch "who | egrep -i 'user1|user2|user3'"
我试图让它弹出一个新的终端并说:
“用户已登录”
我想用“&”在后台运行所有内容,但作为命令登录中的用户之一,我想让这个脚本弹出一个新终端并说这个用户已登录。
我希望它只在他们登录时发生,如果他们注销并重新登录,它就会发生。
我知道如果我在前台运行初始命令我可以看到我的“自定义”用户列表每 2 秒登录和注销但我希望在后台运行它并让新终端弹出登录的特定用户。
很抱歉我重复了一遍,但我正在尝试尽可能具体。
最佳答案
watch
可以很好地观察命令输出,但不能处理它。
我建议使用循环,它可以保存迭代之间的输出并检查差异。像这样思考:
last_output=$(tempfile)
output=$(tempfile)
while true; do
who | egrep -i 'user1|user2|user3' > $output
# check for new users logged
new_users=$(diff $last_output $output | grep '>' | cut -d ' ' -f 2)
# if there is some, throw a notification
if [ -n "$new_users" ]; then
xterm -e "echo -e 'New users logged:\n$new_users'; read -n 1" &
fi
# we save the output
mv $output $last_output
sleep 2
done
这里我使用 xterm 来发出通知,但您可以使用其他工具,如 libnotify
(提供 notify-send
)。并且因为 xterm 在执行的命令完成时停止,所以我添加了等待输入的 read -n 1
命令,但是您可以使用 sleep
使通知消失而无需用户交互。
编辑
要从文件中读取要观看的用户列表,您可以使用类似这样的方法(文件每行包含一个用户):
regex=$(tr '\n' '|' < path/to/file)
regex=${regex%?} # to remove the last '|'
关于linux - 我如何添加到此命令以通过新终端弹出一条消息来制作 "notifications"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27230547/