shell终端会话中的环境之间有什么共同之处?(What is common between environments within a shell terminal session?)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
shell终端会话中的环境之间有什么共同之处?(What is common between environments within a shell terminal session?)

我有一个自定义的shell脚本,每次用户登录时都会运行,或者假定身份,它被放置在/etc/profile.d中,并执行一些基本的env变量操作。 最近我添加了一些代码,以便如果屏幕正在运行,它将重新附加它,而不需要我输入任何内容。 但是有一些问题。 如果我以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

最满意答案

你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.

更多推荐

本文发布于:2023-04-17 09:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9789385f4c03bfc0bf82ec0f8be3dfb1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   之处   终端   环境   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!