如何在脚本中使用newgrp,然后在脚本退出时留在该组中 [英] How do you use newgrp in a script then stay in that group when the script exits
问题描述
我正在solaris Box上运行脚本。特别是SunOS 5.7。我不是根。我正在尝试执行类似于以下内容的脚本:
newgrp thegroup<<
FOO
源.login_stuff
echo hello world
FOO
脚本运行。问题是它返回到调用过程,该过程使我进入旧组,而未获取源.login_stuff。我了解这种行为。我正在寻找的是一种保留在sub shell中的方法。现在我知道我可以放一个xterm& (请参见下文)在脚本中执行此操作,但是使用新的xterm是不可取的。
将当前的pid作为参数传递。
newgrp thegroup<<
FOO
source .login_stuff
xterm&
echo $ 1
kill -9 $ 1
FOO
我没有sg可用。 $ p $ b。另外,newgrp是必需的。
newgrp adm<< ANYNAME
#您不仅可以执行更多行。
echo这正在作为组\ $(id -gn)
ANYNAME
..将输出:
这是作为adm
组运行的pre>
请小心-确保用斜杠转义'$'。交互有点奇怪,因为它在将shell作为另一个组执行之前甚至会扩展单引号。因此,如果您的主要组是用户,而您要使用的组是 adm,则:
newgrp adm<< END
#您不仅可以做更多的行。
echo'正在作为组$(id -gn)运行'
END
..将输出:
它以组用户
的身份运行pre>
..因为'id -gn'由当前shell运行,然后发送给以adm运行的shell。
无论如何,我知道这篇文章很古老,但希望这对某人有用。I am running a script on a solaris Box. specifically SunOS 5.7. I am not root. I am trying to execute a script similar to the following:
newgrp thegroup << FOO
source .login_stuff
echo "hello world"
FOO
The Script runs. The problem is it returns back to the calling process which puts me in the old group with the source .login_stuff not being sourced. I understand this behavior. What I am looking for is a way to stay in the sub shell. Now I know I could put an xterm& (see below) in the script and that would do it, but having a new xterm is undesirable.
Passing your current pid as a parameter.
newgrp thegroup << FOO
source .login_stuff
xterm&
echo $1
kill -9 $1
FOO
I do not have sg available. Also, newgrp is necessary.
解决方案newgrp adm << ANYNAME # You can do more lines than just this. echo This is running as group \$(id -gn) ANYNAME
..will output:
This is running as group adm
Be careful -- Make sure you escape the '$' with a slash. The interactions are a little strange, because it expands even single-quotes before it executes the shell as the other group. so, if your primary group is 'users', and the group you're trying to use is 'adm', then:
newgrp adm << END # You can do more lines than just this. echo 'This is running as group $(id -gn)' END
..will output:
This is running as group users
..because 'id -gn' was run by the current shell, then sent to the one running as adm. Anyways, I know this post is ancient, but hope this is useful to someone.
这篇关于如何在脚本中使用newgrp,然后在脚本退出时留在该组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!