shell 终端会话中的环境之间有什么共同点?
我有一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
su 时获得的 shell 的 ${PPID} 将是 su 命令。因此输出
将以字母
su
开头,因此请对此进行测试。The ${PPID} of the shell you get when you su will be the su command. So the output of
will begin with the letters
su
, so test for this.我认为如果您阅读以下文章(以及您最喜欢的 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