shell 终端会话中的环境之间有什么共同点?

发布于 2024-08-25 18:12:29 字数 453 浏览 1 评论 0原文

我有一个自定义 shell 脚本,每次用户登录或假定身份时都会运行,它被放置在 /etc/profile.d 中并执行一些基本的环境变量操作。最近我添加了一些代码,以便如果屏幕正在运行,它将重新连接它,而不需要我输入任何内容。但也存在一些问题。如果我以 root 身份登录,然后 su - 到另一个用户,则代码会第二次运行。当代码第一次运行时,是否可以设置一个变量来阻止代码的第二次运行?

我想向磁盘写入一些内容,但如果我开始新的终端会话,我不想阻止代码运行。这是有问题的代码。它首先尝试重新连接 - 如果由于它已经连接而失败(因为它可能处于中断的会话中),它将“收回”会话。

screen -r

if [ -z "$STY" ]; then
    exec screen -dR
fi

最终,这个错误阻止我将用户替换为另一个用户,因为一旦我这样做,它就会抢占屏幕会话并将我带回到开始的位置。相当令人沮丧

I have a custom shell script that runs each time a user logs in or identity is assumed, its been placed in /etc/profile.d and performs some basic env variable operations. Recently I added some code so that if screen is running it will reattach it without needing me to type anything. There are some problems however. If I log-in as root, and su - to another user, the code runs a second time. Is there a variable I can set when the code runs the first time that will prevent a second run of the code?

I thought to write something to the disk but then I dont want to prevent the code from running if I begin a new terminal session. Here is the code in question. It first attempts to reattach - if unsuccessful because its already attached (as it might be on an interruped session) it will 'take' the session back.

screen -r

if [ -z "$STY" ]; then
    exec screen -dR
fi

Ultimately this bug prevents me from substituting user to another user because as soon as I do so, it grabs the screen session and puts me right back where I started. Pretty frustrating

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩一世无双 2024-09-01 18:12:29

su 时获得的 shell 的 ${PPID} 将是 su 命令。因此输出

ps -o command= $PPID

将以字母 su 开头,因此请对此进行测试。

The ${PPID} of the shell you get when you su will be the su command. So the output of

ps -o command= $PPID

will begin with the letters su, so test for this.

梦晓ヶ微光ヅ倾城 2024-09-01 18:12:29

我认为如果您阅读以下文章(以及您最喜欢的 shell 的人),您就可以正确理解这一点
有关登录与配置文件的问题

I think you can get this right if you read the following post (and the man for your favorite shell)
Question about login vs profile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文