如何找出父shell的子壳体内的用户? [英] How to find out the user of parent shell inside a child shell?

查看:82
本文介绍了如何找出父shell的子壳体内的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设父shell的用户是。如果你运行像须藤-u酒吧-i ,那么你在与用户子shell 。所以,我怎么能找出孩子壳内父shell的用户?也就是说,在上述情况下,我怎么能知道父shell的用户是

更新结果
我的理解是,一些黑客,就有可能实现这一目标用 PS 。不过,我会更感兴趣的是少哈克和更标准的方式,如果可能的话。

更新2 结果
从答案的帮助下,我终于祭出了以下解决方案:

 #从父shell,用户是foo
$ sudo的-u酒吧-i家长= foo的#从子shell,显示母公司用户foo
$ PSü-p $ PPID | grep的家长|切-d = -f2


解决方案

您可以使用 $ PPID 变量来协助您的命令或两个一起:

 #!/斌/庆典
USER =`PSü-p $ PPID | AWK'{打印$ 1}'|尾-1`
回声$ USER

Suppose the user of parent shell is foo. If you run something like sudo -u bar -i, then you are in a child shell with user bar. So, how can I find out the user of the parent shell inside the child shell? Namely, in the case above, how can I know that the user of parent shell is foo?

update
I understand that, with some hack, it is possible to achieve this using ps. However, I would be much more interested in a less hacky and more standard way, if all possible.

update 2
With help from the answers, I finally resorted to the following solution:

# from parent shell, user is foo
$ sudo -u bar -i PARENT=foo

# from child shell, show parent user foo
$ ps u -p $PPID | grep PARENT | cut -d= -f2

解决方案

You can use the $PPID variable to assist you along with a command or two:

#!/bin/bash
USER=`ps u -p $PPID | awk '{print $1}'|tail -1`
echo $USER

这篇关于如何找出父shell的子壳体内的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆