gpt4 book ai didi

How can I check if I exit back to the ps terminal?(我如何检查我是否可以退出到PS终端?)

转载 作者:bug小助手 更新时间:2023-10-26 21:02:07 27 4
gpt4 key购买 nike



The question may sound a bit strange.

这个问题听起来可能有点奇怪。


The situation is as following:

情况如下:



  • I execute the command "wsl" in the powershell console. My default
    distro starts or get restored.

  • I execute some commands, if I need root rights, I switch to "sudo
    -i". Then I exit and get back to previous user. If I exit one time more (maybe accidently) I return back to powershell environment. Or I
    even exit accidently the console completely. This behaviour I don't
    like, but this isn't the point.


Now I want to somehow record when I leave the last session in Linux. I tried it the other way around by adding a command in wsl.conf:

现在,我想以某种方式记录下我在Linux中离开最后一个会话的时间。相反,我在wsl.conf中添加了一个命令:


command=touch /tmp/test.xyz

Unfortunately, that doesn't help, as the command apparently only runs once after "wsl --shutdown" (at least I think so).
My idea was to remove the temp file after my script is done with it; to use it like a switch.

不幸的是,这没有帮助,因为该命令显然只在“wsl --shutdown”之后运行一次(至少我是这么认为的)。我的想法是在我的脚本完成后删除临时文件;像开关一样使用它。


Now my new idea is to use a "bash_logout" script. But I have no idea how I could check if the current user is the first (or last if I exit the current).
Commands like

现在我的新想法是使用“bash_logout”脚本。但我不知道如何检查当前用户是否是第一个(如果我退出当前用户,则是最后一个)。命令,如


w
who
users

seems only show the current user not any "inherits".

似乎只显示当前用户,而不显示任何“继承”。


And something like

像这样的东西


exit & su

Seems not to be working.

似乎不管用。


So how can I check if my current console session would be the last if I would exit it?


Maybe I'm missing something obvious, if so, I'm sorry. I try to get better with linux.

也许我错过了一些明显的东西,如果是这样的话,我很抱歉。我试着用Linux变得更好。


更多回答
优秀答案推荐

If you want a programmatic way of checking try examining the output from the following:

如果您想要一种程序化的检查方式,请尝试检查以下命令的输出:


echo -e "User: $USER\nSUDO_USER: $SUDO_USER\nLOGNAME: $LOGNAME\nlogname: $(logname)"

ECHO-e“用户:$USER\nSUDO_USER:$SUDO_USER\nLOGNAME:$LOGNAME\nLOGNAME:$(LOGNAME)”


Start with your own user, then change around via sudo and su etc., depending on the Linux distro, sudo version, and potentially the shell (bash, zsh, etc.) you may get different results then I do. I've also observed SUDO_USER not get populated on some older server distributions.

从您自己的用户开始,然后通过sudo和su等更改,这取决于Linux发行版、sudo版本以及可能的外壳(bash、zsh等)。你可能会得到和我不同的结果。我还观察到SUDO_USER在一些较旧的服务器发行版上不会被填充。




On a fairly recent distribution this produced:

在最近的一次分发中,产生了以下结果:


If thedude logs in as the first user: the SUDO_USER environment variable is empty; both USER and LOGNAME environment variables have a value of thedude; logname command output is thedude:

如果DUD以第一个用户身份登录:SUDO_USER环境变量为空;USER和LOGNAME环境变量的值都为THUDU;LOGNAME命令输出为TUDU:


User: thedude
SUDO_USER:
LOGNAME: thedude
logname: thedude

If thedude then uses sudo -i to change to root:

如果该用户随后使用sudo-i更改为根用户:


User: root
SUDO_USER: thedude
LOGNAME: root
logname: thedude

Then from root uses su walter to become walter:

然后从根使用苏·沃尔特变成沃尔特:


User: walter
SUDO_USER: thedude
LOGNAME: walter
logname: thedude

but if it was instead of su walter it was sudo su walter to become walter, stacking a second sudo command SUDO_USER gets overwritten with root, but logname still has a value of thedude, showing it is the first login:

但如果不是su walter,而是sudo su walter成为walter,堆叠第二个sudo命令SUDO_USER会被root覆盖,但logname仍然有thedude的值,表明它是第一个登录名:


User: walter
SUDO_USER: root
LOGNAME: walter
logname: thedude



I use the etckeeper project with .bash_logout to force a commit any time /etc has been altered with the commit message of:

我将etcKeeper项目与.bash_logout一起使用,以便在任何时候使用以下提交消息更改/ETC时强制提交:


Session exit commit:
* User: $USER
* SUDO_USER: $SUDO_USER
* LOGNAME: $LOGNAME
* logname: $(logname)

but for you, this could be a comparison like:

但对你来说,这可能是这样的比较:


if [[ "${USER}" = "${LOGNAME}" ]] && [[ "${USER}" = "$(logname)" ]]; then
# First user and last to logout of host. Do something.
touch /tmp/test.xyz
fi

更多回答

$LOGNAME seems to work. I'm not sure about uppercase lowercase and the if comparison. I'll try this out more closely. Maybe I have further questions.

$LOGNAME似乎起作用了。我不确定大写、小写和if的比较。我会更仔细地试一试。也许我还有更多的问题。

After extensive investigation, I'm wondering how you managed to get $logname (/bin/logname) to produce output? After a pretty stupid hack (sudo logname) I managed it too. During my research I came across this post: github.com/microsoft/WSL/issues/3085 Otherwise, I am very satisfied. It took quite a bit of effort, including using "bash_rc.local" and ditching "/tmp" in favor of something permanent. Unfortunately, otherwise no output is generated if you close the ps terminal with the x.

经过广泛的调查,我想知道您是如何让$logname(/bin/logname)生成输出的?在一次相当愚蠢的黑客攻击(Sudo Logname)之后,我也成功了。在我的研究过程中,我偶然看到了这篇帖子:githorb.com/microsoft/wsl/Issues/3085,除此之外,我非常满意。这需要付出相当大的努力,包括使用“bash_rc.local”,去掉“/tmp”而使用永久性的代码。遗憾的是,如果您用x关闭ps终端,则不会生成任何输出。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com